Skip to main content
Inspiring
November 21, 2022
Question

Automating Export to pdf

  • November 21, 2022
  • 2 replies
  • 735 views

Hello,

 

We have documents with a lot of conditional text and variables, that allows exporting lots of versions of the document. We use structured documents.

 

Before exporting the document we usually do the following for the whole book:

- Set show/hide consitional text

- Import variables from an external .fm file (File / Import / Formats...)

- Import template (File / Import / Formats...)

- Update book for numbering etc.

- Import EDD from external .fm file to make everything re-align

 

This is working well but it is also quite long when one need to do this for several versions of the output.

 

What would be the best way to aytomatize the whole process ?

 

I wanted to look at the scripts but when I do File / Script / New Script... nothin happens. Should I install some additional software apart from Framemaker to use scripts ?

 

Thanks in advance.

 

Best regards

 

Eric

 
 
 
 
This topic has been closed for replies.

2 replies

Eric5F88Author
Inspiring
November 23, 2022

Hello,

Thanks to your support I am very close to have my script working !

The only things I didn't manage to do are:

  • Handle recovery/locks related messages when opening some files
  • Most importantly: when I run the script for the first time, it saves the book as a pdf just fine, but whrn I try to run it the second time I have a message stating "no files selected in the book" that pops up and the pdf is not saved. To save I use the following method (thanks again to FrameExpert :)) https://community.adobe.com/t5/framemaker-discussions/how-to-save-as-pdf-from-a-script/m-p/13106930#M75798 . Any idea about how to solve that ? It is an issue as it also happens when I try to iterate over all my conditional tags, trying to automatically save each version to a separate pdf file, which is my goal. If I close every file and reopen them, it works fine again.

Thanks in advance !

Best regards

Eric

 
 
frameexpert
Community Expert
Community Expert
November 23, 2022

Here is a general-purpose function for opening a document or book. The first parameter is the absolute path to the document, the second is true or false, which determines if you want the document or book to open visibly on the screen. The last two parameters are optional: the third is for specifying a structured application and the fourth is true if you want a new untitled document based on the file you specify. If you want to use the fourth parameter, but not the third, you have to specify "undefined" (without the quotes) for the third.

 

I would have to see your code to understand what is happening with your second bullet point.

 

function openDocOrBook (filename, visible, structApp, newDoc) {
    
    var i, openProps, retParm, docOrBook;

    // Get default property list for opening documents.
    openProps = GetOpenDefaultParams ();
    // Get a property list to return any error messages.
    retParm = new PropVals ();

    // Set specific open property values to open the document.
    i = GetPropIndex (openProps, Constants.FS_AlertUserAboutFailure);
    openProps[i].propVal.ival = false;
    i = GetPropIndex (openProps, Constants.FS_MakeVisible);
    openProps[i].propVal.ival = visible;
    i = GetPropIndex (openProps, Constants.FS_FileIsOldVersion);
    openProps[i].propVal.ival = Constants.FV_DoOK;
    i = GetPropIndex (openProps, Constants.FS_FileIsInUse);
    openProps[i].propVal.ival = Constants.FV_ResetLockAndContinue;
    i = GetPropIndex (openProps, Constants.FS_OpenFileNotWritable);
    openProps[i].propVal.ival = Constants.FV_DoOK;
    i = GetPropIndex (openProps, Constants.FS_FontChangedMetric);
    openProps[i].propVal.ival = Constants.FV_DoOK;
    i = GetPropIndex (openProps, Constants.FS_FontNotFoundInCatalog);
    openProps[i].propVal.ival = Constants.FV_DoOK;
    i = GetPropIndex (openProps, Constants.FS_FontNotFoundInDoc);
    openProps[i].propVal.ival = Constants.FV_DoOK;
    i = GetPropIndex (openProps, Constants.FS_RefFileNotFound);
    openProps[i].propVal.ival = Constants.FV_AllowAllRefFilesUnFindable;
    if (structApp) {
        i = GetPropIndex (openProps, Constants.FS_StructuredOpenApplication);
        openProps[i].propVal.sval = structApp;
    }
	if (newDoc) {
		i = GetPropIndex (openProps, Constants.FS_NewDoc);
		openProps[i].propVal.ival = newDoc;
	}

    // Attempt to open the document.
    docOrBook = Open (filename, openProps, retParm);

    if (docOrBook.ObjectValid () === 1) {
        // Add a property to the document or book, indicating that the script opened it.
        docOrBook.openedByScript = true;
    }
    else {
        // If the document can't be open, print the errors to the Console.
        PrintOpenStatus (retParm);
    }

    return docOrBook; // Return the document  or book object.
}

 

 

Eric5F88Author
Inspiring
November 23, 2022

Hello frameexpert,

Thanks again for answering my question ! The function you provided works great indeed !

Here is the full script:

 

 

 

var projectBasePath = "C:\\mydocs\\docs\\SVN\\project_folder\\";
var commonBasePath = "C:\\mydocs\\docs\\SVN\\framemaker_common\\"
var pdfNamePrefix = "UG_";
var pdfNameSuffix = "_test.pdf";

var conditionalTags = ["tag1", "tag2"];

var book = app.FirstOpenBook;

for (var i = 0; i < conditionalTags.length; i++) {
    conditionalText(conditionalTags[i]);
    importVariables(conditionalTags[i]);
    updateTemplate();
    updateBook();
    updateEDD();
    savePdf(conditionalTags[i]);
}

alert("done !");

function updateTemplate()
{
    var templateName = commonBasePath + "template.fm";
    var templateDoc = openDocOrBook(templateName, true, true);
    var comp = book.FirstComponentInBook;
    while(comp.ObjectValid()) {
        var doc = openDocOrBook(comp.Name, true, true);
        // Filter by attributes settings flag ? Math definitions flag == equations settings ? object styles ?
        var res = doc.SimpleImportFormats(
            templateDoc, 
            Constants.FF_UFF_PGF |
            Constants.FF_UFF_FONT |
            Constants.FF_UFF_PAGE |
            Constants.FF_UFF_TABLE |
            Constants.FF_UFF_COLOR |
            Constants.FF_UFF_DOCUMENT_PROPS |
            Constants.FF_UFF_REFPAGE |
            Constants.FF_UFF_XREF |
            Constants.FF_UFF_MATH |
            Constants.FF_UFF_COMBINED_FONTS |
            Constants.FF_UFF_REMOVE_EXCEPTIONS |
            Constants.FF_UFF_REMOVE_PAGE_BREAKS
        );
        if (res != Constants.FE_Success) {
            alert("Template: failed with " + doc.Name);
        }
        comp = comp.NextBookComponentInDFSOrder;
    }
}

function updateEDD()
{
    var eddName = commonBasePath + "EDD.fm";
    var eddDoc = openDocOrBook(eddName, true, true);
    var comp = book.FirstComponentInBook;
    while(comp.ObjectValid())
    {
        var doc = openDocOrBook(comp.Name, true, true);
        // Filter by attributes settings flag ? Math definitions flag == equations settings ? object styles ?
        var res = doc.SimpleImportElementDefs(
            eddDoc, 
            Constants.FF_IED_REMOVE_OVERRIDES | 
            Constants.FF_IED_REMOVE_BOOK_INFO
        );
        if (res != Constants.FE_Success) {
            alert("EDD: failed with " + doc.Name);
        }
        comp = comp.NextBookComponentInDFSOrder;
    }
}

function conditionalText(tag)
{
    showState_PV = new PropVal();
    showState_PV.propIdent.num = Constants.FS_ShowState;
    showState_PV.propVal.valType = Constants.FT_Integer;
    showState_PV.propVal.ival = Constants.FV_ShowAsPerConditions;
    
    showConditions_PV = new PropVal();
    showConditions_PV.propIdent.num = Constants.FS_ShowConditions;
    showConditions_PV.propVal.valType = Constants.FT_Strings;
    var conditions = new Strings(tag);
    showConditions_PV.propVal.ssval = conditions;
    
    conditionalPropVals = new PropVals();
    conditionalPropVals.push(showConditions_PV);
    conditionalPropVals.push(showState_PV);
    
    book.ApplyConditionalSettings(conditionalPropVals);
}

function importVariables(tag)
{
    var fileName = projectBasePath + "Variable_" + tag + ".fm";
    var variableDoc = openDocOrBook(fileName, true, true);
    var comp = book.FirstComponentInBook;
    while(comp.ObjectValid())
    {
        var doc = openDocOrBook(comp.Name, true, true);
        // Filter by attributes settings flag ? Math definitions flag == equations settings ? object styles ?
        var res = doc.SimpleImportFormats(variableDoc, Constants.FF_UFF_VAR);
        if (res != Constants.FE_Success)
        {
            alert("Import variables: failed with " + doc.Name);
        }
        comp = comp.NextBookComponentInDFSOrder;
    }
}

function updateBook()
{
    var updateParams = GetUpdateBookDefaultParams();

    updateParams[GetPropIndex(updateParams, Constants.FS_AlertUserAboutFailure)].propVal.ival = false;
    updateParams[GetPropIndex(updateParams, Constants.FS_AllowInconsistentNumProps)].propVal.ival = Constants.FV_DoOK;
    updateParams[GetPropIndex(updateParams, Constants.FS_AllowNonFMFiles)].propVal.ival = Constants.FV_DoOK;
    updateParams[GetPropIndex(updateParams, Constants.FS_AllowViewOnlyFiles )].propVal.ival = Constants.FV_DoOK;
    updateParams[GetPropIndex(updateParams, Constants.FS_MakeVisible)].propVal.ival = true;
    updateParams[GetPropIndex(updateParams, Constants.FS_ShowBookErrorLog)].propVal.ival = false;
    updateParams[GetPropIndex(updateParams, Constants.FS_UpdateBookGeneratedFiles)].propVal.ival = true;
    updateParams[GetPropIndex(updateParams, Constants.FS_UpdateBookMasterPages)].propVal.ival = false;
    updateParams[GetPropIndex(updateParams, Constants.FS_UpdateBookNumbering)].propVal.ival = true;
    updateParams[GetPropIndex(updateParams, Constants.FS_UpdateBookOleLinks)].propVal.ival = true;
    updateParams[GetPropIndex(updateParams, Constants.FS_UpdateBookTextReferences)].propVal.ival = true;
    updateParams[GetPropIndex(updateParams, Constants.FS_UpdateBookXRefs )].propVal.ival = true;

    var updateReturnParams = new PropVals();
    book.UpdateBook(updateParams, updateReturnParams);
}

function savePdf(tag)
{
    var params, returnParams;
    
    params = GetSaveDefaultParams ();
    returnParams = new PropVals ();
    
    params[GetPropIndex (params, Constants.FS_FileType)].propVal.ival = Constants.FV_SaveFmtPdf;
    params[GetPropIndex (params, Constants.FS_PDFUseDistiller)].propVal.ival = 0;

    FA_errno = 0;
    book.BookIsSelected = 1; // THIS IS SOLVING THE "NO FILE SELECTED" ISSUE
    book.Save(projectBasePath + pdfNamePrefix + tag + pdfNameSuffix, params, returnParams);
	if (FA_errno !== 0) {
		PrintSaveStatus (returnParams);
	}
}

function openDocOrBook (filename, visible, structApp)
{    
    var openProps, retParm, docOrBook;

    // Get default property list for opening documents.
    openProps = GetOpenDefaultParams ();
    // Get a property list to return any error messages.
    retParm = new PropVals ();

    // Set specific open property values to open the document.
    openProps[GetPropIndex(openProps, Constants.FS_AlertUserAboutFailure)].propVal.ival = false;
    openProps[GetPropIndex(openProps, Constants.FS_MakeVisible)].propVal.ival = visible;
    openProps[GetPropIndex(openProps, Constants.FS_FileIsOldVersion)].propVal.ival = Constants.FV_DoOK;
    openProps[GetPropIndex(openProps, Constants.FS_FileIsInUse)].propVal.ival = Constants.FV_ResetLockAndContinue;
    openProps[GetPropIndex(openProps, Constants.FS_OpenFileNotWritable)].propVal.ival = Constants.FV_DoOK;
    openProps[GetPropIndex(openProps, Constants.FS_FontChangedMetric)].propVal.ival = Constants.FV_DoOK;
    openProps[GetPropIndex(openProps, Constants.FS_FontNotFoundInCatalog)].propVal.ival = Constants.FV_DoOK;
    openProps[GetPropIndex(openProps, Constants.FS_FontNotFoundInDoc)].propVal.ival = Constants.FV_DoOK;
    openProps[GetPropIndex(openProps, Constants.FS_RefFileNotFound)].propVal.ival = Constants.FV_AllowAllRefFilesUnFindable;
    if (structApp) {
        openProps[GetPropIndex(openProps, Constants.FS_StructuredOpenApplication)].propVal.sval = structApp;
    }

    // Attempt to open the document.
    docOrBook = Open (filename, openProps, retParm);

    if (docOrBook.ObjectValid () === 1) {
        // Add a property to the document or book, indicating that the script opened it.
        docOrBook.openedByScript = true;
    }
    else {
        // If the document can't be open, print the errors to the Console.
        PrintOpenStatus (retParm);
    }

    return docOrBook; // Return the document  or book object.
}

 

 

 

Thanks in advance !

Best regards

Eric

 
 
Community Expert
November 21, 2022

Hello Eric,

a script seems to be a good idea to automatize your workflow.

 

With "new script" the "Adobe ExtendScript Toolkit" should be started.

Maybe you have yet to install the toolkit.

 

Best regards

Stephan

Eric5F88Author
Inspiring
November 21, 2022

Hello Stephan,

Thanks for your answer !

I were not able to find where to download the ExtendedScript ToolKit installer, where can I find it ?

Thanks in advance.

Best regards

Eric

 
 
Community Expert
November 21, 2022

Hello Eric,

you can find the latest version here: CEP-Resources/ExtendScript-Toolkit at master · Adobe-CEP/CEP-Resources · GitHub .

But it is no longer developed (read this: How to install Extend Script Toolkit CC - Adobe Support Community - 11181537  ).

Best regards

Stephan