Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

need help --script to create a PDF with security features

Explorer ,
Jan 20, 2020 Jan 20, 2020

I already have a script that i give to my fellow designers that in part creates both a high res PDF and a low res PDF. both open a dialog where the design can spec how they prefer the PDF to be made. At the last meeting, the "powers that be" decided to make all the low resolution PDFs be uniform AND include security features to lock down the low res PDF from alteration. Security features cannot be saved in a preset so i cannot go down the route of creating a preset and having the script assign that preset to the PDF. I found a list of PDFExportPreference but am unsure how to use this information.

 

scott

 

 

  

TOPICS
How to , Import and export , Scripting
2.3K
Translate
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 2 Correct answers

Community Expert , Jan 20, 2020 Jan 20, 2020

Here's a sample code using useSecurity and changeSecurityPassword. You also need to set the restrictions for printing, copying and changing. Be sure to change any preferences back at the end. 

 

var currPrefs = app.pdfExportPreferences;
with (currPrefs) {
    useSecurity = true;
    changeSecurityPassword = "myPassword";
    acrobatCompatibility = AcrobatCompatibility.ACROBAT_7;
    viewPDF = true;
    pageRange = PageRange.ALL_PAGES;
    disallowPrinting = false;
    disallowCopying = true;
    d
...
Translate
Community Expert , Jan 21, 2020 Jan 21, 2020

Hi,

just two small changes to make that work:

 

1. Use the preset itself that is stored in variable pdfExportPreset.

And not its name that is stored in variable pdfExportPresetName.

2. Use the preset as the fourth argument in method exportFile(). Currently its the fifth argument.

 

DOM documentation to your rescue:

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Document.html#d1e49241__d1e53606

 

Regards,
Uwe Laubender

( ACP )

Translate
Community Expert ,
Jan 20, 2020 Jan 20, 2020
AFAIK, you can not save security settings as part of a preset. You'll have to add that at export (or do it in Acrobat) but honestly, it's a false sense of security. Be aware that some third party apps won't honor it.
Translate
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
Explorer ,
Jan 20, 2020 Jan 20, 2020

you are correct, it is not possible to have a PRESET include security.


so i am looking for a script that will export a "generic" low rsolution pdf which will include security.

I see that the option to changeSecurityPassword and useSecurity are both available in PDFExportPreferences but i do not know enough about scripting to use this information. I am good enough to alter an existing script but not sure how to get started because i cannot find any scripts that are using PDFExportPreferences to start from



These low res PDFs are place into our online catalog of available printed pieces.
the problem is idiots were taking these low res pdfs and altering them.
I know it is not great security but at least it makes it more difficult.

Translate
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
Advisor ,
Jan 20, 2020 Jan 20, 2020

Scott,

you can add the PDFExportPreferences useSecurity property to your script like this...

app.pdfExportPreferences.useSecurity = true;

 

I added it to one of my existing scripts to see what it would do on export, see below on what the the Document Properties show without  and with useSecurity.

 

without:

Screen Shot 2020-01-20 at 12.40.49 PM.pngexpand image

with:

Screen Shot 2020-01-20 at 12.45.36 PM.pngexpand image

Either way it appears that you'll have to go into the Document Properties of the exported pdf and manaully set\change the Security settings to your preferences.

 

Regards,

Mike

 

Translate
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
Advisor ,
Jan 20, 2020 Jan 20, 2020

Scott,

 

Disregard......

"Either way it appears that you'll have to go into the Document Properties of the exported pdf and manaully set\change the Security settings to your preferences."

 

you can apply each of the Security settings like Brian is showing below.....

 

Regards

Mike

Translate
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 Expert ,
Jan 20, 2020 Jan 20, 2020

Here's a sample code using useSecurity and changeSecurityPassword. You also need to set the restrictions for printing, copying and changing. Be sure to change any preferences back at the end. 

 

var currPrefs = app.pdfExportPreferences;
with (currPrefs) {
    useSecurity = true;
    changeSecurityPassword = "myPassword";
    acrobatCompatibility = AcrobatCompatibility.ACROBAT_7;
    viewPDF = true;
    pageRange = PageRange.ALL_PAGES;
    disallowPrinting = false;
    disallowCopying = true;
    disallowChanging = true;
}


var doc = app.activeDocument;
doc.exportFile(ExportFormat.PDF_TYPE, File("~/Desktop/testsec1.pdf"), false);

currPrefs.useSecurity = false;
Translate
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
Explorer ,
Jan 20, 2020 Jan 20, 2020

thank you that was exactly the kickstart i needed....

 

 

YOU ARE THE BEST!!!!

 

 

scott

Translate
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 Expert ,
Jan 20, 2020 Jan 20, 2020

Hi Brian,

I would reset all other values as well after the export is done.

Note: There is a properties property of app.pdfExportPreferences you could use.

 

Also missing:

 

1. A check if the pdf file exists before trying to export.

 

2. A try/catch around exportFile() to get a known exception if the PDF file cannot be written.

Main causes:

PDF file with the same name is already open.

Folder to save the file has not the rights to write a file to.

File name is too long to write a file to the folder.

( There may be others as well. )

 

3. A defined PDF Preset as fourth argument in exportFile().

 

Regards,
Uwe Laubender

( ACP )

 

 

 

Translate
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
Explorer ,
Jan 20, 2020 Jan 20, 2020

I already am aware and will take care of #1 and #2 but could you explain #3 a bit more?

 

scott

Translate
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 Expert ,
Jan 20, 2020 Jan 20, 2020

You can set a preset for the export as the fourth argument in exportFile: 

 

var preset = app.pdfExportPresets.itemByName("mypreset");

doc.exportFile(ExportFormat.PDF_TYPE, File("~/Desktop/testsec1.pdf"), false, preset);

Translate
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 Expert ,
Jan 20, 2020 Jan 20, 2020

Hi Scott,

exportFile() takes a lot of arguments.

Details here:

http://jongware.mit.edu/idcs6js/pc_Document.html#exportFile

 

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Document.html#d1e49521__d1e53922

 

The fourth argument is using : PDFExportPreset

 

So I think you could use a pre-defined PDFExportPreset for your lowres PDFs.

No need to define all necessary property/value pairs of your PDF Export with app.pdfExportPreferences.

Just do the security settings there. All other should come from a saved PDFExportPreset , a installed *.joboptions file.

 

Some code to illustrate this:

 

 

( function()
{

	// Here goes the password:
	var passWord = "myPassword";
	// Here one have to set the PDF Export Preset name:
	var pdfExportPresetName = "yourPDF-Preset-Name";
	
	
	// No document open? Do nothing:
	if( app.documents.length == 0 ){ return };
	
	var doc = app.activeDocument;
	
	// If the active document was never saved, do nothing:
	if( !doc.saved )
	{ alert( "ERROR: Save your INDD-Document before running this script! | SCRIPT "); return };
	
	
	// Check, if the preset is available:
	var pdfExportPreset = app.pdfExportPresets.itemByName( pdfExportPresetName );
	// Preset is not available, do nothing:
	if( !pdfExportPreset.isValid ){ alert( "ERROR: No pdfExportPreset found. | SCRIPT"); return };

/*
AND MORE CODE TO EXPORT TO PDF
*/


}() )

 

 

Regards,
Uwe Laubender
( Adobe Community Expert )

 

EDITED LINK

Translate
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
Explorer ,
Jan 20, 2020 Jan 20, 2020

will the security still be applied even though in the end you are now using a preset? I assumed that the preset would undo the security requests

Translate
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 Expert ,
Jan 20, 2020 Jan 20, 2020

Hi Scott,

that depends on what property/value pairs a PDF Preset can define.

 

Look into DOM documentation again, this time in the most current one perhaps:

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#PDFExportPreset.html

 

Can you see property useSecurity for object PDFExportPreset? I do not.

So, I think a PDFExportPreset can go along very well with changed app.pdfExportPreferences.

 

Just do a test, come back and let us know how it went 🙂

 

Regards,
Uwe Laubender

( ACP )

Translate
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
Explorer ,
Jan 21, 2020 Jan 21, 2020

Unfortunately i am not getting it to work....

 

i have some of the error checking you mentioned earlier in the script and am attaching just the relevant section...

 

 

this is what i tried, a combination of the two answers so far and it is working...

except I cannot control the preset that is used.

It always uses the most recent PDF preset on export even though i am asking it to use "smallest size_spreads"

 

var currPrefs = app.pdfExportPreferences;
with (currPrefs) {
    useSecurity = true;
    changeSecurityPassword = "Password";
    acrobatCompatibility = AcrobatCompatibility.ACROBAT_7;
    viewPDF = true;
    pageRange = PageRange.ALL_PAGES;
    disallowPrinting = false;
    disallowCopying = true;
    disallowChanging = true;
}
 
 
// Here one have to set the PDF Export Preset name:
var pdfExportPresetName = "smallest size_spreads";
// Check, if the preset is available:
var pdfExportPreset = app.pdfExportPresets.itemByName( pdfExportPresetName );
// Preset is not available, do nothing:
if( !pdfExportPreset.isValid ){ alert( "ERROR: No pdfExportPreset found. | SCRIPT")};
 
 
var pdfPath = Folder.selectDialog("Folder to save the LOW res pdf:");  
var pdfName = d.name.substring(0, d.name.indexOf("."))+"_lr.pdf";
 
  
var userDefFileName = prompt("File name:",pdfName,undefined);  
  
if(userDefFileName == null){  
    exit();  
    };  
  
var pdfFullName = pdfPath+"/"+userDefFileName;  
  
if(File(pdfFullName).exists){  
    c=confirm("The PDF-file \""+userDefFileName+"\" is already existing. Do you want to overwrite it?",true,undefined);  
    if (c==0){exit()};    
    };  
 
var doc = app.activeDocument;
doc.exportFile(ExportFormat.PDF_TYPE,File(pdfFullName),false,undefined,pdfExportPresetName);  
 
currPrefs.useSecurity = false;
Translate
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 Expert ,
Jan 21, 2020 Jan 21, 2020

Hi,

just two small changes to make that work:

 

1. Use the preset itself that is stored in variable pdfExportPreset.

And not its name that is stored in variable pdfExportPresetName.

2. Use the preset as the fourth argument in method exportFile(). Currently its the fifth argument.

 

DOM documentation to your rescue:

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Document.html#d1e49241__d1e53606

 

Regards,
Uwe Laubender

( ACP )

Translate
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
Explorer ,
Jan 21, 2020 Jan 21, 2020

thank you for helping me out with this project....you make me look like a rockstar!!!!!

 

scott

Translate
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
Explorer ,
Jan 21, 2020 Jan 21, 2020

for the preset "Smallest file size_spreads"  the only change to the adobe supplied [smallest File Size] preset i made is to create the PDF as spreads

 

because "exportReaderSpreads"is a property for object PDFExportPreset i cannot add exportReaderSpreads=true; 

up in my list of preferences because it will be overridden by the info in [Smallest File Size]

 

is this correct?

 

i would like to write this using the factory supplied [Smallest File Size] if possible to avoid having all my designers install or create a PDF preset in addition to the script

 

scott

Translate
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 Expert ,
Jan 22, 2020 Jan 22, 2020

Hi Scott,

you could add a throw-away pdfExportPreset, apply a true to property exportReaderSpreads and after the export is done remove it. Look into the method duplicate() for pdfExportPreset for that.

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#PDFExportPreset.html#d1e325649__d1e326872

 

Regards,
Uwe Laubender

( ACP )

Translate
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 Expert ,
Jan 22, 2020 Jan 22, 2020

Hi Scott,

if you are testing my suggestion with the throw-away duplicate of a pdfExportPreset:

 

WARNING

Do not test the script code with the ESTK ( ExtendScript Toolkit ).

The ESTK will throw an error:

With my German version: Die angeforderte Aktion konnte nicht ausgeführt werden, da das Objekt nicht mehr existiert.

( $ID/ObjectDeletedError )

 

Where?

 

var dupPreset = pdfExportPreset.duplicate();

// THAT THROWS ERROR:
dupPreset.exportReaderSpreads = true ;

 

It is as if the duplicate of the preset does not exist anymore.

 

WORKAROUND

Run the same script code from InDesign's Scripts Panel and no error will be thrown.

The PDF can be exported using the changed duplicate. In the above case as exported spreads.

Just tested this with InDesign 2020 on Windows 10.

Also tested it a year ago with InDesign CC-2019.

 

This is a longstanding bug!

The following thread in the OLD InDesign Scripting Forum gave me the hint:

 

Duplicate pdfExportPreset
enbanat May 27, 2013 6:24 PM

NEW LINK:

https://community.adobe.com/t5/indesign/duplicate-pdfexportpreset/td-p/5155495

OLD LINK:
https://forums.adobe.com/message/5355641#5355641

 

Sorry! Unfortunately this thread was not moved to the current InDesign Forum last year.

What a shame!

 

EDITED: January 22, 2020

 

Regards,
Uwe Laubender

( ACP )

Translate
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
Explorer ,
Jan 22, 2020 Jan 22, 2020

i'm afraid that you are now over my head. this is what i tried and it does not create spreads using the [Smallest FileSize] adobe supplied preset. I am sure i just bungled everything because i do not understand

 

if(app.documents.length>0){  
    var d=app.activeDocument;  
    };  
else{  
    alert("Please open a document to execute export to pdf. Script will be aborted.");  
    exit();  
    }  
if(d.saved == false){  
    alert("Save your document first before executing export to pdf. Script will be aborted.");  
    exit();  
    };  
   
 
 
//----------------------------------------------
var dupPreset = app.pdfExportPresets.item('[Smallest File Size]').properties;
dupPreset.exportReaderSpreads = true;
app.pdfExportPreferences.properties = dupPreset;
 
 
 
var currPrefs = app.pdfExportPreferences;
with (currPrefs) {
    useSecurity = true;
    changeSecurityPassword = "Password";
    acrobatCompatibility = AcrobatCompatibility.ACROBAT_7;
    viewPDF = true;
    pageRange = PageRange.ALL_PAGES;
    disallowPrinting = false;
    disallowCopying = true;
    disallowChanging = true;
}
 
 
// Here one have to set the PDF Export Preset name:
var pdfExportPresetName = '[Smallest File Size]';
// Check, if the preset is available:
var pdfExportPreset = app.pdfExportPresets.itemByName('[Smallest File Size]');
// Preset is not available, do nothing:
if( !pdfExportPreset.isValid ){ alert( "ERROR: No pdfExportPreset found. | SCRIPT")};
 
 
var pdfPath = Folder.selectDialog("Folder to save the LOW res pdf:");  
var pdfName = d.name.substring(0, d.name.indexOf("."))+"_lr.pdf";
 
  
var userDefFileName = prompt("File name:",pdfName,undefined);  
  
if(userDefFileName == null){  
    exit();  
    };  
  
var pdfFullName = pdfPath+"/"+userDefFileName;  
  
if(File(pdfFullName).exists){  
    c=confirm("The PDF-file \""+userDefFileName+"\" is already existing. Do you want to overwrite it?",true,undefined);  
    if (c==0){exit()};    
    };  
 
var doc = app.activeDocument;
doc.exportFile(ExportFormat.PDF_TYPE,File(pdfFullName),false,pdfExportPresetName);  
 
currPrefs.useSecurity = false;
Translate
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 Expert ,
Jan 22, 2020 Jan 22, 2020

Hi Scott,

I suggested to duplicate a pdfExportPreset.

I did not suggest to work with a presets properties property.

 

Do not do this:

 

var dupPreset = app.pdfExportPresets.item('[Smallest File Size]').properties;

 

 

But this:

 

var dupPreset = app.pdfExportPresets.itemByName('[Smallest File Size]').duplicate();

 

Only now the preset is duplicated.

 

Then change the duplicated preset's value for property exportReaderSpreads to true ( false is default ) :

 

dupPreset.exportReaderSpreads = true ;

 

 

And use the duplicated and changed preset to export the document to PDF.

Do not use a name! A preset is required as 4th argument:

 

var doc = app.activeDocument;
doc.exportFile
(
ExportFormat.PDF_TYPE,
File(pdfFullName),
false,
dupPreset
);

 

 

Finally throw the duplicated preset away:

 

dupPreset.remove();

 

 

Regards,
Uwe Laubender

( ACP )

Translate
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
Explorer ,
Jan 22, 2020 Jan 22, 2020
LATEST

I think that worked!!!!!

 

here is what i ended up with:

 

//----------------------------------______LOW____--------------------------------------

 

 

//--------------------------pdf security export---------------------------------------------

if(app.documents.length>0){  

    var d=app.activeDocument;  

    };  

else{  

    alert(“Please open a document to execute export to pdf. Script will be aborted.”);  

    exit();  

    }  

if(d.saved == false){  

    alert(“Save your document first before executing export to pdf. Script will be aborted.”);  

    exit();  

    };  

   

 

 

//----------------------------------------------

var dupPreset = app.pdfExportPresets.itemByName(‘[Smallest File Size]’).duplicate();

dupPreset.exportReaderSpreads = true;

 

var currPrefs = app.pdfExportPreferences;

with (currPrefs) {

    useSecurity = true;

    changeSecurityPassword = “Password”;

    acrobatCompatibility = AcrobatCompatibility.ACROBAT_7;

    viewPDF = true;

    pageRange = PageRange.ALL_PAGES;

    disallowPrinting = false;

    disallowCopying = true;

    disallowChanging = true;

}

 

 

 

 

var pdfPath = Folder.selectDialog(“Folder to save the LOW res pdf:”);  

var pdfName = d.name.substring(0, d.name.indexOf(“.”))+”_lr.pdf”;

 

  

var userDefFileName = prompt(“File name:”,pdfName,undefined);  

  

if(userDefFileName == null){  

    exit();  

    };  

  

var pdfFullName = pdfPath+”/”+userDefFileName;  

  

if(File(pdfFullName).exists){  

    c=confirm(“The PDF-file \””+userDefFileName+”\” is already existing. Do you want to overwrite it?”,true,undefined);  

    if (c==0){exit()};    

    };  

 

var doc = app.activeDocument;

doc.exportFile

(

ExportFormat.PDF_TYPE,

File(pdfFullName),

false,

dupPreset

);

 

currPrefs.useSecurity = false;

dupPreset.remove();

 

Translate
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