Skip to main content
Participant
July 19, 2007
Question

Tweaking the SaveDocsAsPDF script

  • July 19, 2007
  • 35 replies
  • 14666 views
Hello. Absolute scripting newbie here; will someone take pity...?

I am looking at the SaveDocsAsPDF script. It works for our purposes except I would like it to save PDFs using a Preset we created called "SGS PDFs. (We have trouble with people in our office forgetting to choose this setting, so I thought a script might be the answer.)

I found a posting on this forum where someone wanted to save files as small as possible, and got some clues from that. So I left everything as is in the original script, except I added this first line in the function:
{var NamePreset = '[SGS PDFs]';

Then after the line:
var options = new PDFSaveOptions();

I added:
options.PDFPreset=NamePreset;

I thought this would result in a script that does the same thing I now do "manually". However, if I save my document "manually" using File/Save As/ Adobe PDF, and choosing the SGS PDFs preset (and changing nothing else), I get a 716 KB file. But if I use my script, I get a 4.9 MB file. So it appears that what I added is NOT making the script use my preset settings.

So my question is: Is there something REALLY basic missing from my script, or... In order to get a script that accomplishes everything already determined in the Preset, do I manually have to list all those settings as options?

I think I can do the latter if I need to if I just study some of the other scripts... unless there's a shortcut!
This topic has been closed for replies.

35 replies

Participating Frequently
November 21, 2010

Is there a way to let the script read all the files within the subfolders as well at the following part of the script? :

    files = sourceFolder.getFiles( fileType );
Participating Frequently
November 20, 2010

This script is exactly what I was searching for, thanks for posting it!

Only one thing: Is there any way to discard or auto-confirm all the dialog boxes asking about Legacy Artwork, Crop Areas, Color Settings, etc.?

Cheers

Participating Frequently
November 20, 2010

Ooops, sorry. Never mind, I found the switch to turn off user interaction within the script itself... :/

Participant
November 3, 2010

Hello everyone, :">

could someone please explain how to tweak the script so it batch-converts ".eps" to .ai?

I tried to simply change every "PDF" to "AI" within the Script, but when I run the Script in Illustrator, I get an error message concerning the wrong "pdfSaveOpts" at the end of the script.

Thanks in advance,

Mike

Larry G. Schneider
Community Expert
Community Expert
November 4, 2010

Here's one to save as AICS4

/**********************************************************

ADOBE SYSTEMS INCORPORATED
Copyright 2005 Adobe Systems Incorporated
All Rights Reserved

NOTICE:  Adobe permits you to use, modify, and
distribute this file in accordance with the terms
of the Adobe license agreement accompanying it. 
If you have received this file from a source
other than Adobe, then your use, modification,
or distribution of it requires the prior
written permission of Adobe.

*********************************************************/

/**********************************************************

SaveAsAI14.jsx

DESCRIPTION

This sample gets files specified by the user from the
selected folder and batch processes them and saves them
as PDFs in the user desired destination with the same
file name.

**********************************************************/

// Main Code [Execution of script begins here]

// uncomment to suppress Illustrator warning dialogs
// app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;

var destFolder, sourceFolder, files, fileType, sourceDoc, targetFile, illustratorSaveOpts;

// Select the source folder.
sourceFolder = Folder.selectDialog( 'Select the folder with Illustrator files you want to convert to AI', '~' );

// If a valid folder is selected
if ( sourceFolder != null )
{
    files = new Array();
    fileType = prompt( 'Select type of Illustrator files to you want to process. Eg: *.ai', ' ' );
   
    // Get all files matching the pattern
    files = sourceFolder.getFiles( fileType );
   
    if ( files.length > 0 )
    {
        // Get the destination to save the files
        destFolder = Folder.selectDialog( 'Select the folder where you want to save the converted AI files.', '~' );
        for ( i = 0; i < files.length; i++ )
        {
            var optRef = new OpenOptions();
            optRef.updateLegacyText = true;


            sourceDoc = app.open(files, DocumentColorSpace.CMYK, optRef); // returns the document object
                                   
            // Call function getNewName to get the name and file to save the ai
            targetFile = getNewName();
           
            // Call function getIllustratorOptions get the illustratorSaveOptions for the files
            illustratorSaveOpts = getIllustratorOptions( );
           
            // Save as AI
            sourceDoc.saveAs( targetFile, illustratorSaveOpts );
           
            sourceDoc.close();
        }
        alert( 'Files are saved as AI in ' + destFolder );
    }
    else
    {
        alert( 'No matching files found' );
    }
}


/*********************************************************

getNewName: Function to get the new file name. The primary
name is the same as the source file.

**********************************************************/

function getNewName()
{
    var ext, docName, newName, saveInFile, docName;
    docName = sourceDoc.name;
    ext = '.ai'; // new extension for AI file
    newName = "";
       
    for ( var i = 0 ; docName != "." ; i++ )
    {
        newName += docName;
    }
    newName += ext; // full AI name of the file
   
    // Create a file object to save the ai file
    saveInFile = new File( destFolder + '/' + newName );

    return saveInFile;
}


/*********************************************************

getIllustratorOptions: Function to set the AI saving options of the
files using the illustratorSaveOptions object.

**********************************************************/

function getIllustratorOptions()
{
   
    // Create the PDFSaveOptions object to set the AI options
    var illustratorSaveOpts = new IllustratorSaveOptions();
   
    // Setting IllustratorSaveOptions properties.
    illustratorSaveOpts.embedLinkedFiles = true;
    illustratorSaveOpts.fontSubsetThreshold = 0.0
    illustratorSaveOpts.pdfCompatible = true
    illustratorSaveOpts.comatibility = Compatibility.ILLUSTRATOR14

    return illustratorSaveOpts;
}

Participating Frequently
June 2, 2010

Oh, that's good news if it works! :)

I will give it a try sometime soon... thanks for the effort in trying!

Participating Frequently
May 26, 2010

Oh, thanks a lot! If we can combine that with a "Save to Postscript" script, it would be the best of both worlds!

artchrome
Inspiring
June 2, 2010

Hi Joudenaarde,

Sorry for this late reply, so..

I passed some times to try save as ps but unfortunately, i don't have enough knowledge about print() and his many options (i've writed another post for this case)

i tried another way with EPS, it seems EPS is understood by distiller , both are postscript so whynot ?

But now i need reply, to complet my script.

Usually, you have maked a preset for distiller, so it seems now we don't need a "preset selector" in script..

Could try this script and tell me if you are some changes between "by the hand" process distiller / previous PDF script and / this script.

#target illustrator

if (app.documents.length>0){
var doc=app.activeDocument;     
var FolderRef = new Folder();
var folderResult=false;

try {
     FolderRef = FolderRef.selectDlg("Where is *.ps Save Folder ?");
if (FolderRef!=null){folderResult=true;}
} catch (e) {
     alert("! Error on selecting folder:\n"+e+" !");
     folderResult=false;
}
var saveOpts = new EPSSaveOptions();
saveOpts.cmykPostScript = true;
saveOpts.embedAllFonts = true;
saveOpts.embedLinkedFiles = true;
var theOutput = new File (FolderRef+"/"+doc.name);
alert (theOutput.fsName);
          app.documents[0].saveAs(theOutput,saveOpts);
f=File (FolderRef+'/'+doc.name);
if(f.open('e')){
     if(f.rename(doc.name.substr(0,doc.name.length-4)+".ps")){
f.close()     } else {alert ("error on renamed file")}
}
//     }
alert ("finished...");
}

this script take count only active document.

See you, art.chrome

Participating Frequently
May 26, 2010

That's what I thought as well :-)

I wanted to speed up making PDF's because I'm in the packaging industry, so I sometimes have to make 30 PDF's of pretty heavy Illustrator files. That's my main reason for wanting to automate the PDF-process a bit... I've looked into making an "Action", but that doesn't work as well as the Photoshop-version unfortunately.

Too bad the SaveDocsasPDF's doesn't compress it as well though. It would've saved me tons of time if it was able to create similar small-sized files. Thanks for the replies though! If anyone else has a trick or two, feel free to let us know ;-)

artchrome
Inspiring
May 26, 2010

Hi,

I haved a little script, i don't think that this is can help you directly, but it use your predefined settings (entered and save in PDF settings).

This could help you to see more clairly where is problem.

But, in fact i'm convinced that you need a script which is save your document as .ps in your distiller.

It's not a big deal, this script below should be help other peoples in all case (i hope ).

#target illustrator

alert ("This script save all open document(s) as PDF by Preset");


if (app.documents.length>0){
     
var FolderRef = new Folder();
var folderResult=false;

try {
     FolderRef = FolderRef.selectDlg("Folder to save your PDF");
if (FolderRef!=null){folderResult=true;}
} catch (e) {
     alert("! Error on selecting folder:\n"+e+" !");
     folderResult=false;
}
var list=app.PDFPresetsList;
var Plist='';
for (var i in list){Plist+="\n"+i+" : "+list}
var pIndex=prompt ('Choice your PDF preset by number'+Plist,0);

for (i=app.documents.length;i>0;i--){
     var PDF = new PDFSaveOptions();
     PDF.pDFPreset =list[pIndex];
     var saveName = new File (FolderRef+"/"+documents[0].name);
     documents[0].saveAs(saveName,PDF);
     documents[0].close();
     }
alert ("finished...");
}

So.. now i'm looking for Save as PS file for Distiller..

Regards, art.chrome

stephen_pfny
Participant
June 23, 2016

Any way to add something before this to let is select files from a folder and then execute the same script? This script is great, but adding the functionality of letting it do its magic without having files open would be tremendous. Thx.artchrome

Participating Frequently
May 26, 2010

Hello art.chrome,

Thanks for the reply! I have been playing with those settings you mentioned, and ended up having them on AUTOMATICJPEGLOW which makes them a little bit smaller. But I think there are some other settings that I'm misunderstanding which should/could save the amount that the Distiller is saving me:

These are the settings I have at the moment:

{ var NamePreset = '[Smallest File Size]';


// Create the PDFSaveOptions object to set the PDF options

var pdfSaveOpts = new PDFSaveOptions();


// Setting PDFSaveOptions properties. Please see the JavaScript Reference

// for a description of these properties.

// Add more properties here if you like

pdfSaveOpts.acrobatLayers = false;

pdfSaveOpts.colorBars = false;

pdfSaveOpts.colorCompression = CompressionQuality.AUTOMATICJPEGMEDIUM;

pdfSaveOpts.compressArt = true; //default

pdfSaveOpts.embedICCProfile = false;

pdfSaveOpts.enablePlainText = true;

pdfSaveOpts.generateThumbnails = true; // default

pdfSaveOpts.optimization = true;

pdfSaveOpts.pageInformation = true;

pdfSaveOpts.pDFPreset = NamePreset;

But I'm not quite sure which of these are the magic space-savers to be honest... or if there are any other settings I can implement to help size things down a bit. I had a test-file that became a ±346KB PDF via Distiller, and ±812KB via the above script. It became 760KB if I change the Compression quality to "LOW" though, so it did help a little bit :-)

artchrome
Inspiring
May 26, 2010

Hi,

May be Distiller make better jobs than saveAsPDF option..

PDF settings are complex (for me) there are full of settings, some one more obscurs than others

You can have an idea if open with a text editor the setting file at:

Windows XP
Documents and Settings\[username]\Application Data\Adobe\Adobe PDF\Settings\
Windows Vista and  Windows 7
Users\[username]\AppData\Roaming\Adobe\Adobe PDF\Settings\
Mac OS
User/[username]/Library/Application   Support/Adobe/Adobe PDF/Settings/

and compare it with your actual setting script, to find what is the secret/magic setting ?

A better way (may be), is to save by script your document as .ps file directly in your distiller folder to make an optimized PDF. So you need search a script able to make this..

Regards, art.chrome



Participating Frequently
May 26, 2010

After Googling for tweaking the SaveDocsasPDF for a while I ran into this thread, thank heavens ;-) I hope it's ok to ask an additional question regarding the provided script in message nr.9, so here goes.

I copied and pasted that script in the Extendscript Toolkit and saved it to be able to make good and small PDF's via Illustrator CS4. It absolutely works like a charm, but there's only one change I'm hoping to find some help with. When printing to Postscript and using Distiller to make a "Smallest File Size" PDF, the PDF's generally are about half the size compared to PDF's made with the script in Illustrator... even though I use the "Smallest File Size"-setting in both cases.

I'm unforunately one of those people who are biological incapability of scripting, but would anyone be able to help me with adjusting the script to get the filesize down a bit more? It would be very much appreciated! :-)

Sincerely,

- J

artchrome
Inspiring
May 26, 2010

Hi,

joudenaarde wrote:

I'm unforunately one of those people who are biological incapability of scripting, but would anyone be able to help me with adjusting the script to get the filesize down a bit more? It would be very much appreciated! :-)

i'm sure that you able to change some lines... try it and  see if there are some changes

I think that we reduce PDF size with  change some options..

for me the script beggining  with : line 1:

var destFolder, sourceFolder, files, fileType, sourceDoc, targetFile, pdfSaveOpts;

i've remove some comments...

but the main change could be below: line 87

pdfSaveOpts.colorCompression = CompressionQuality.AUTOMATICJPEGHIGH;

there many possibilities for CompressionQuality:



AUTOMATICJPEG2000HIGH
AUTOMATICJPEG2000LOSSLESS
AUTOMATICJPEG2000LOW
AUTOMATICJPEG2000MAXIMUM
AUTOMATICJPEG2000MEDIUM
AUTOMATICJPEG2000MINIMUM
AUTOMATICJPEGHIGH
AUTOMATICJPEGLOW
AUTOMATICJPEGMAXIMUM
AUTOMATICJPEGMEDIUM
AUTOMATICJPEGMINIMUM
JPEG2000HIGH
JPEG2000LOSSLESS
JPEG2000LOW
JPEG2000MAXIMUM
JPEG2000MEDIUM
JPEG2000MINIMUM
JPEGHIGH
JPEGLOW
JPEGMAXIMUM
JPEGMEDIUM
JPEGMINIMUM
ZIP4BIT
ZIP8BIT
None

i suggest to use AUTOMATICJPEGMEDIUM like as below:

pdfSaveOpts.colorCompression = CompressionQuality.AUTOMATICJPEGMEDIUM;

Let's me know, if this can help you.. or not

art.chrome

Participant
May 5, 2010

I would love to have this exact same script, but to convert to PNGs, can anyone help me with this?

Larry G. Schneider
Community Expert
Community Expert
May 5, 2010

Try this. Be advised that there is no changing the ppi of a scripted exported PNG.

/**********************************************************

ADOBE SYSTEMS INCORPORATED
Copyright 2005 Adobe Systems Incorporated
All Rights Reserved

NOTICE:  Adobe permits you to use, modify, and
distribute this file in accordance with the terms
of the Adobe license agreement accompanying it. 
If you have received this file from a source
other than Adobe, then your use, modification,
or distribution of it requires the prior
written permission of Adobe.

*********************************************************/

/**********************************************************

Save as PDFs.js

DESCRIPTION

This sample gets files specified by the user from the
selected folder and batch processes them and saves them
as PDFs in the user desired destination with the same
file name.

**********************************************************/

// Main Code [Execution of script begins here]

// uncomment to suppress Illustrator warning dialogs
// app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;

var destFolder, sourceFolder, files, fileType, sourceDoc, targetFile, pngExportOpts;

// Select the source folder.
sourceFolder = Folder.selectDialog( 'Select the folder with Illustrator files you want to convert to PNG', '~' );

// If a valid folder is selected
if ( sourceFolder != null )
{
    files = new Array();
    fileType = prompt( 'Select type of Illustrator files to you want to process. Eg: *.ai', ' ' );
   
    // Get all files matching the pattern
    files = sourceFolder.getFiles( fileType );
   
    if ( files.length > 0 )
    {
        // Get the destination to save the files
        destFolder = Folder.selectDialog( 'Select the folder where you want to save the converted PNG files.', '~' );
        for ( i = 0; i < files.length; i++ )
        {
            sourceDoc = app.open(files); // returns the document object
                                   
            // Call function getNewName to get the name and file to save the pdf
            targetFile = getNewName();
           
            // Call function getPNGOptions get the PNGExportOptions for the files
            pngExportOpts = getPNGOptions();
           
            // Export as PNG
            sourceDoc.exportFile( targetFile, ExportType.PNG24, pngExportOpts );
           
            sourceDoc.close(SaveOptions.DONOTSAVECHANGES);
        }
        alert( 'Files are saved as PNG in ' + destFolder );
    }
    else
    {
        alert( 'No matching files found' );
    }
}


/*********************************************************

getNewName: Function to get the new file name. The primary
name is the same as the source file.

**********************************************************/

function getNewName()
{
    var ext, docName, newName, saveInFile, docName;
    docName = sourceDoc.name;
    ext = '.png'; // new extension for png file
    newName = "";
       
    for ( var i = 0 ; docName != "." ; i++ )
    {
        newName += docName;
    }
    newName += ext; // full png name of the file
   
    // Create a file object to save the png
    saveInFile = new File( destFolder + '/' + newName );

    return saveInFile;
}


/*********************************************************

getPNGOptions: Function to set the PNG saving options of the
files using the PDFSaveOptions object.

**********************************************************/

function getPNGOptions()
{
   
    // Create the PDFSaveOptions object to set the PDF options
    var pngExportOpts = new ExportOptionsPNG24();
   
   
   
    // Setting PNGExportOptions properties. Please see the JavaScript Reference
    // for a description of these properties.
    // Add more properties here if you like
    pngExportOpts.antiAliasing = true;
    pngExportOpts.artBoardClipping = true;
    //pngExportOpts.horizontalScale = 100.0;
    //pngExportOpts.matte = true;
    //pngExportOpts.matteColor = 0, 0, 0;
    pngExportOpts.saveAsHTML = false;
    pngExportOpts.transparency = false;
    //pngExportOpts.verticalScale = 100.0;

    return pngExportOpts;
}

Participant
May 6, 2010

Thank a lot! Does this mean that it defaults to 72ppi? Is there any way to change that ppi at all?

Participant
June 19, 2008
It was possible to resolve the Save error (it had to do the AIprefs file). Earlier, when I booted from the clean (windows-installation-wise) drive it was still picking up the old AIprefs; since the clean drive has a backup of my data.
Participant
June 23, 2009

Hi Guyz,

I am totally noob to scripting. I used this batch conversion java script (Save as PDFs) for .eps to pdf conversion in Ilustrator and tweaked for my use.

Here goes my problem- I need to convert thousands of eps, dwg and dxf files to pdf in different page orientation, vectors fit to page with 10 mm left on all sides of artboard and center the vectors in artboard.

What I understand that page size can not be changed so I need to determine

-size of vector

-open new artboard with that size

-center vector and save

Kindly help me. Here's the script

/**********************************************************

ADOBE SYSTEMS INCORPORATED

Copyright 2005-2008 Adobe Systems Incorporated

All Rights Reserved

NOTICE:  Adobe permits you to use, modify, and

distribute this file in accordance with the terms

of the Adobe license agreement accompanying it. 

If you have received this file from a source

other than Adobe, then your use, modification,

or distribution of it requires the prior

written permission of Adobe.

*********************************************************/

/**********************************************************

Save as PDFs.jsx

DESCRIPTION

This sample gets files specified by the user from the

selected folder and batch processes them and saves them

as PDFs in the user desired destination with the same

file name.

**********************************************************/

// Main Code [Execution of script begins here]

// uncomment to suppress Illustrator warning dialogs

// app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;

var destFolder, sourceFolder, files, fileType, sourceDoc, targetFile, pdfSaveOpts;

// Select the source folder.

sourceFolder = Folder.selectDialog( 'Select the folder with Illustrator files you want to convert to PDF', '~' );

// If a valid folder is selected

if ( sourceFolder != null )

{

files = new Array();

fileType = prompt( 'Select type of Illustrator files to you want to process. Eg: *.ai', ' ' );

// Get all files matching the pattern

files = sourceFolder.getFiles( fileType );

if ( files.length > 0 )

{

// Get the destination to save the files

destFolder = Folder.selectDialog( 'Select the folder where you want to save the converted PDF files.', '~' );

for ( i = 0; i < files.length; i++ )

{

sourceDoc = app.open(files); // returns the document object

// Call function getNewName to get the name and file to save the pdf

targetFile = getNewName();

// Call function getPDFOptions get the PDFSaveOptions for the files

pdfSaveOpts = getPDFOptions( );

// Save as pdf

sourceDoc.saveAs( targetFile, pdfSaveOpts );

sourceDoc.close();

}

alert( 'Files are saved as PDF in ' + destFolder );

}

else

{

alert( 'No matching files found' );

}

}

/*********************************************************

getNewName: Function to get the new file name. The primary

name is the same as the source file.

**********************************************************/

function getNewName()

{

var ext, docName, newName, saveInFile, docName;

docName = sourceDoc.name;

ext = '.pdf'; // new extension for pdf file

newName = "";

for ( var i = 0 ; docName != "." ; i++ )

{

newName += docName;

}

newName += ext; // full pdf name of the file

// Create a file object to save the pdf

saveInFile = new File( destFolder + '/' + newName );

return saveInFile;

}

/*********************************************************

getPDFOptions: Function to set the PDF saving options of the

files using the PDFSaveOptions object.

**********************************************************/

function getPDFOptions()

{

// Create the PDFSaveOptions object to set the PDF options

var pdfSaveOpts = new PDFSaveOptions();

// Setting PDFSaveOptions properties. Please see the JavaScript Reference

// for a description of these properties.

// Add more properties here if you like

pdfSaveOpts.acrobatLayers = true;

//pdfSaveOpts.colorBars = true;

pdfSaveOpts.colorCompression = CompressionQuality.AUTOMATICJPEGHIGH;

//pdfSaveOpts.compressArt = true; //default

//pdfSaveOpts.embedICCProfile = true;

//pdfSaveOpts.enablePlainText = true;

pdfSaveOpts.generateThumbnails = true; // default

pdfSaveOpts.optimization = true;

//pdfSaveOpts.pageInformation = true;

// uncomment to view the pdfs after conversion.

// pdfSaveOpts.viewAfterSaving = true;

return pdfSaveOpts;

}