Using Save() method to save as PDF

Community Beginner ,
Feb 05, 2018 Feb 05, 2018

Copy link to clipboard

Copied

Hello everyone,

I am trying to save a FM-Doc as PDF and using the Save() method.

The method does not work correctly. I am getting no saved PDF file, and while execution I get a InvalidObject.
Could someone tell me, what the problem could be?
Here is the code:

function saveDocAsPDF(doc){

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

name = doc.Name;

fullName = "TESTPDF.pdf";

saveParams = GetSaveDefaultParams();

returnParams = new PropVals(); 

var PDF = doc.Save(fullName, saveParams, returnParams);

$.writeln("SaveReturnParams / Errors " + PrintSaveStatus(returnParams));

i = GetPropIndex(returnParams, Constants.FS_SaveNativeError);   // CHECK STATUS

status = returnParams.propVal.ival;

if (status === Constants.FE_Success) {

  return (true);

} else {

  return (false);

}

};

And these are the properties set by me:

function getSaveDefaultParams(){

var params, i;

   

    params = GetSaveDefaultParams();

      

    i = GetPropIndex(params, Constants.FS_FileType);

    params.propVal.ival = Constants.FV_SaveFmtPdf;

    i = GetPropIndex(saveParams, Constants.FS_AlertUserAboutFailure);

    saveParams.propVal.ival = true;

    i = GetPropIndex(saveParams, Constants.FS_FileType);

    saveParams.propVal.ival = Constants.FV_SaveFmtPdf;

    i = GetPropIndex(saveParams, Constants.FS_FileIsInUse);

    saveParams.propVal.ival = Constants.FV_ResetLockAndContinue;  

    i = GetPropIndex(saveParams, Constants.FS_SaveFileNotWritable);

    saveParams.propVal.ival = Constants.FV_DoShowDialog;  

    return (params);

};

Thanks a lot!

TOPICS
Scripting

Views

643

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Enthusiast , Feb 05, 2018 Feb 05, 2018
Hello Selim?The problem is due to a few errors in your scripts.The fullName variable needs more definition. The Save() function needs a path too.I normally use the ExtendScript File() or Folder() functions to ensure hat the file name that I give to any FrameMaker function is valid. In this case we define the path using the helpful File.path property. I then concatenate that with the new file name. Then I change that to the path convention that FrameMaker prefers using the File.fsName property.Yo...

Likes

Translate

Translate
Enthusiast ,
Feb 05, 2018 Feb 05, 2018

Copy link to clipboard

Copied

Hello Selim?

The problem is due to a few errors in your scripts.

The fullName variable needs more definition. The Save() function needs a path too.

I normally use the ExtendScript File() or Folder() functions to ensure hat the file name that I give to any FrameMaker function is valid. In this case we define the path using the helpful File.path property. I then concatenate that with the new file name. Then I change that to the path convention that FrameMaker prefers using the File.fsName property.

Your getSaveDefaultParams() function is not called by the first function as you use your GetSaveDefaultParams(). Probably a good idea to name your functions differently to FrameMaker's API functions

function saveDocAsPDF(doc){ 

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

    name = doc.Name,

    fullName = File(File(doc.Name).path + "/TESTPDF.pdf").fsName;

   

    saveParams = getSaveDefaultParams(); 

    returnParams = new PropVals();   

    var PDF = doc.Save(fullName, saveParams, returnParams); 

    $.writeln("SaveReturnParams / Errors " + PrintSaveStatus(returnParams)); 

    i = GetPropIndex(returnParams, Constants.FS_SaveNativeError);   // CHECK STATUS 

    status = returnParams.propVal.ival; 

    if (status === Constants.FE_Success) { 

    return (true); 

    } else { 

    return (false); 

    } 

};

In your getSaveDefaultParams() function there are some naming errors.

function getSaveDefaultParams(){ 

var params, i; 

     

    params = GetSaveDefaultParams(); 

        

    i = GetPropIndex(params, Constants.FS_FileType); 

    params.propVal.ival = Constants.FV_SaveFmtPdf; 

    i = GetPropIndex(params, Constants.FS_AlertUserAboutFailure); 

    params.propVal.ival = true; 

    i = GetPropIndex(params, Constants.FS_FileType); 

    params.propVal.ival = Constants.FV_SaveFmtPdf; 

    i = GetPropIndex(params, Constants.FS_FileIsInUse); 

    params.propVal.ival = Constants.FV_ResetLockAndContinue;    

    i = GetPropIndex(params, Constants.FS_SaveFileNotWritable); 

    params.propVal.ival = Constants.FV_DoShowDialog;    

    return (params); 

};

In this case you were referring to saveParams in some places while having defined params as the variable that contains the Save Default Params.

With the code amended as shown I got the PDF to save in the same folder as the source document.

I hope that this works for you?

Ian

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 07, 2018 Feb 07, 2018

Copy link to clipboard

Copied

Hello Ian,

thanks a lot for your detailed answer!

I was able to fix my code and run it as i intended. Thanks !

I am actually new to FM and ExtendScript programming, so excuse my "bad" programming style.

Selim

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Feb 07, 2018 Feb 07, 2018

Copy link to clipboard

Copied

LATEST

No problem Selim. We are here to help when we can. You are on the right track, but I would just say that you need to be careful with the use of undeclared variables, which in JavaScript (ExtendScript) always become global variables. While that may seem to allow more flexibility it can also be the cause of difficult to locate bugs.

It can be very useful to step through your code one line at a time to see the progression of variable values in the Data Browser. If an expected variable isn't present you may have forgotten to declare it.

Another way to get an impartial review of your code is to make use of JSLint which will initially horrify you, but eventually really help. http://www.jslint.com/. Paste your ExtendScript in there and run a check.  The creator of JSLint also wrote the best JavaScript book. JavaScript: The Good Parts by Douglas Crockford It's short and to the point and will save you hours of frustration.

Good luck

Ian

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines