Skip to main content
manishk88663184
Participating Frequently
August 7, 2019
Question

Query for creating a pdf file from book file using extendScript

  • August 7, 2019
  • 1 reply
  • 1016 views

Hi All,

I am facing an issue while creating the pdf from the book file. I have a book file which consists of multiple chapters some are in the same folder(where book file is present ) and some are in a different folder. I have a cross-reference for every chapter in the book file. But when I run the extends script to create a pdf then pdf created successfully but the cross-reference of those chapters which are in the different folders is broken. And Cross-reference of those chapters which are in the same folders is working normally.

Could anyone suggest me what to change in my script to take care of this cross-reference

Please find my script below.

Thanks,
Manish

--------------------------BookToPdf.jsx---------------------------------------------

var book = app.ActiveBook;

//book.BookDontUpdateReferences=0;

  var n =0;

  var i=3;

     if(!book.ObjectValid())

          {

               book = app.FirstOpenBook;

               //book.BookDontUpdateReferences=0;

          }

      

                       if(book.ObjectValid())

                              {

                                    app.ActiveBook = book;

                                    var comp = book.FirstComponentInBook;

                                    var name=comp.Name;

                               //     alert("Importing Variables from"+ "\n"+ name);

                                    var FactsFile =OpenFile(comp.Name);

                                    while(comp.ObjectValid())

                                           {

                                                           if(FactsFile.ObjectValid())

                                                                {

                                                                           var FormatFlags = Constants.FF_UFF_VAR;

                                                                           var doc = comp.NextComponentInBook;

                                                                           var docFile =OpenFile(doc.Name);

                                                                           doc = app.ActiveDoc;

                                                                           doc.SimpleImportFormats(FactsFile, FormatFlags);

                                                                 }

                                                                            n++;

                                                                            comp = comp.NextBookComponentInDFSOrder;

                                            }

                                }

              

                      if(book.ObjectValid())

                               {

                                

                                   app.ActiveBook = book;

                                   var comp = book.FirstComponentInBook;

                                   var rev = comp.NextComponentInBook;

                                   var revname=rev.Name;

                                //   alert("Importing PageLayout and ColorDefinition from" + "\n"+ revname);

                                    var FactsFile =OpenFile(rev.Name);

                                           

                                            for (var i = 3; i <=n-1; i++)

                                                   {

                                                            if(comp.ComponentType == Constants.FV_BK_FILE)

                                                               if(FactsFile.ObjectValid())

                                                                    {

                                                                   

                                                                        var FormatFlags = Constants.FF_UFF_PAGE|Constants.FF_UFF_COLOR;

                                                                        var doc = rev.NextComponentInBook;

                                                                        var docFile =OpenFile(doc.Name);

                                                                        doc = app.ActiveDoc;

                                                                        doc.SimpleImportFormats(FactsFile, FormatFlags);

                                                                    }

                                                                        rev = rev.NextBookComponentInDFSOrder;

                                                    }

                                 }

         

                                    if(book.ObjectValid())

                                           {

                                               app.ActiveBook = book;

                                               var comp = book.FirstComponentInBook;

                                               while(comp.ObjectValid())

                                                     {

                                                            if(comp.ComponentType == Constants.FV_BK_FILE)

                                                                   {

                                                                        var doc=OpenFile(comp.Name);

                                                                         if(doc.ObjectValid())

                                                                               {

                                                                                    var name, saveParams, i;

                                                                                    doc = app.ActiveDoc;

                                                                                    name = doc.Name;

                                                                                    saveParams = GetSaveDefaultParams();

                                                                                    returnParams = new PropVals();

                                                                                    i = GetPropIndex(saveParams, Constants.FS_FileType);

                                                                                    saveParams.propVal.ival =Constants.FV_SaveFmtBinary90;

                                                                                    doc.Save(name, saveParams, returnParams);

                                                                               }

                                                            

                                                                   }

                                                            comp = comp.NextBookComponentInDFSOrder;

                                                     }

                                               }

                                     else

                                     alert("No book is open or active. Cannot run the script.");

                                     var props = GetUpdateBookDefaultParams();

                                     var index = GetPropIndex(props, Constants.FS_ShowBookErrorLog);

                                     props[index].propVal.ival = true;

                                     var index = GetPropIndex(props,Constants.FS_UpdateBookTextReferences);

                                     props[index].propVal.ival = true;

                                     var index = GetPropIndex(props,Constants.FS_UpdateBookXRefs);

                                     props[index].propVal.ival = true;

                                     var returnp = new PropVals();

                                     book.UpdateBook(props, returnp);

                                     saveAsPdf (book);

                                            

                                                    

                                    function saveAsPdf(book)

                                          {

                                  //              alert("Preparing to save the book as pdf!");

                                                var saveParams, name, i, baseName, status, fullName;

                                                name = book.Name;

                                                baseName = name.substr(0,name.lastIndexOf("."));

                                                fullName = baseName + ".pdf";

                                                saveParams = GetSaveDefaultParams();

                                                returnParams = new PropVals(); 

                                                i = GetPropIndex(saveParams, Constants.FS_FileType);

                                                saveParams.propVal.ival =Constants.FV_SaveFmtPdf;

                                                book.Save(fullName, saveParams, returnParams);

                                                i = GetPropIndex(returnParams, Constants.FS_SaveNativeError);

                                                status = returnParams.propVal.ival;

                                                if (status === Constants.FE_Success)

                                                     {

                                                            return (true);

                                                     }

                                               else

                                                     {

                                                            return (false);

                                                     }

                                          }

                                            

                                       

                                   

                                 /*   comp = book.FirstComponentInBook;

                                    while(comp.ObjectValid())

                                        {

                                              if(comp.ComponentType == Constants.FV_BK_FILE)

                                                     {

                                                               var doc=OpenFile(comp.Name);

                                                                  if(doc.ObjectValid())

                                                                         {

                                                                                   var name = doc.Name;

                                                                                  doc.Close(Constants.FF_CLOSE_MODIFIED);

                                                                             

                                                                          }

                                                     }

                                              comp = comp.NextBookComponentInDFSOrder;

                                        }*/

                                    function OpenFile(path)

                                        {

                                              var index;

                                              props = GetOpenDefaultParams();

                                    index = GetPropIndex(props, Constants.FS_AlertUserAboutFailure);

                                    if(index > -1)

                                        props[index].propVal.ival = false;

                                    index = GetPropIndex(props, Constants.FS_FileIsInUse);

                                    if(index > -1)

                                        props[index].propVal.ival = Constants.FV_ResetLockAndContinue;

                                    index = GetPropIndex(props, Constants.FS_BookIsInUse);

                                    if(index > -1)

                                        props[index].propVal.ival = Constants.FV_ResetLockAndContinue;

                                    index = GetPropIndex(props, Constants.FS_LockCantBeReset);

                                    if(index > -1)

                                        props[index].propVal.ival = Constants.FV_DoOK;

                                        index = GetPropIndex(props, Constants.FS_DisallowMIF);

                                    if(index > -1)

                                        props[index].propVal.ival = false;

                                       

                                    index = GetPropIndex(props, Constants.FS_DisallowXml);

                                    if(index > -1)

                                        props[index].propVal.ival = false;

                                         index = GetPropIndex(props, Constants.FS_FileIsOldVersion);

                                    if(index > -1)

                                        props[index].propVal.ival = Constants.FV_DoOK;

                                          index = GetPropIndex(props, Constants.FS_FontChangedMetric);

                                    if(index > -1)

                                        props[index].propVal.ival = Constants.FV_DoOK;

                                        index = GetPropIndex(props, Constants.FS_FontNotFoundInCatalog);

                                    if(index > -1)

                                        props[index].propVal.ival = Constants.FV_DoOK;

                                           index = GetPropIndex(props, Constants.FS_FontNotFoundInDoc);

                                    if(index > -1)

                                        props[index].propVal.ival = Constants.FV_DoOK;

                                     index = GetPropIndex(props, Constants.FS_LanguageNotAvailable);

                                    if(index > -1)

                                        props[index].propVal.ival = Constants.FV_DoOK;

                                          index = GetPropIndex(props, Constants.FS_OpenAsType);

                                    if(index > -1)

                                        props[index].propVal.ival = Constants.FV_AUTORECOGNIZE;

                                        index = GetPropIndex(props, Constants.FS_UpdateTextReferences);

                                    if(index > -1)

                                        props[index].propVal.ival = Constants.FV_DoNo;

                                        

                                    index = GetPropIndex(props, Constants.FS_UpdateXRefs);

                                    if(index > -1)

                                        props[index].propVal.ival = Constants.FV_DoNo;

                                        index = GetPropIndex(props, Constants.FS_UseRecoverFile);

                                    if(index > -1)

                                        props[index].propVal.ival = Constants.FV_DoNo;

                                       

                                    index = GetPropIndex(props, Constants.FS_UseAutoSaveFile);

                                    if(index > -1)

                                        props[index].propVal.ival = Constants.FV_DoNo;

                                        index = GetPropIndex(props, Constants.FS_OpenFileNotWritable);

                                    if(index > -1)

                                        props[index].propVal.ival = Constants.FV_DoOK;

                                    index = GetPropIndex(props, Constants.FS_RefFileNotFound);

                                    if(index > -1)

                                        props[index].propVal.ival = Constants.FV_DoOK;

                                    returnp = new PropVals();

                                    var file = Open(path, props, returnp);

                                    return file;

                                }   

This topic has been closed for replies.

1 reply

Jeff_Coatsworth
Community Expert
Community Expert
August 7, 2019

Never, ever heard of scripting to create a PDF out of FM, but I've moved you over to Scripting forum for better responses.

Legend
August 7, 2019

It's actually quite common to automate PDF generation. Having said that, I've never done it   And the script is too long for me to analyze and test myself. But allow me to ask some basic questions that may be a starting point... I assume that the PDF is OK when you generate it through the UI? Do you have all chapter files open when the PDF is generated? Are all cross-references properly resolved initially?

Like I said, I did not analyze the script thoroughly, but it does look to me like you might be generating the PDF with the files closed. If so, I would try it with all files open. FM seems smarter about xref resolution when all linked files are open, especially when they are in different folders.

Russ

manishk88663184
Participating Frequently
August 8, 2019

I assume that the PDF is OK when you generate it through the UI? Yes Do you have all chapter files open when the PDF is generated? Yes Are all cross-references properly resolved initially? Yes only for those chapters which are in the same folder. And what we want to achieve is to automatically build a cross-reference for those chapters which are not in the same folder thorough script. Right now we are manually creating the cross-reference for it. I am new to extend script so I am not sure whether this can be taken care from script or not