Copy link to clipboard
Copied
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;
}
Copy link to clipboard
Copied
Never, ever heard of scripting to create a PDF out of FM, but I've moved you over to Scripting forum for better responses.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Hi Manish,
With your latest comments, I am not sure exactly what the scenario is. Do you have a perfectly resolved book for which you get a good PDF through the UI, but a broken PDF through ExtendScript? Or are you adding xrefs before you create the PDF that become unresolved? I'm not clear if the problem is the PDF process or an inability to programmatically insert xrefs.
You certainly can create and resolve xrefs with a script. But I'm not sure if that is your issue. It usually does not require much code but it can be a little tricky.
Russ
Copy link to clipboard
Copied
Hi Russ, Yes, it's not breaking because of the script. And I want my script to create Xref during the generation of pdf. Can you guide me the script part to create Xref Thanks, Manish
Copy link to clipboard
Copied
Hi Manish,
It takes several steps to create and resolve an xref. I don't have time at the moment to create a sample, as I am unfortunately (for you) preparing for a few days of vacation. Is there anyone else watching that can take over, and of course take credit for the final answer? Manish, we would need to know whether these documents are structured or not.
Russ
Copy link to clipboard
Copied
Hi Russ, The document is not structured. Please let me know the steps to create an Xref once you come back from your vacation. Waiting for your reply. Thanks, Manish