Batch export .ai files to pdf

Every time I send a copy of the current version of a manuscript to contributors, I have to export .ai files to .pdf (they can’t read .ai) for all of the figures that have changed and that takes a very annoying 5 minutes every day since I have a bunch of plots. And so as a true scientist, I spent several hours trying to find a solution.

There’s a .jsx hidden far inside the Illustrator example scripts folder that does the job; it’s called Save to PDFs, and it simply saves all files in a given folder matching a mask to pdfs. I tweaked it a little so it would ask less questions and just export all the ai files in a folder to (aifilename)_export.pdf, with the settings set up so the pdfs read correctly in the Preview Mac application (and other non-Adobe pdf readers). Here is the script:

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

ADOBE SYSTEMS INCORPORATED 
Copyright 2005-2006 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. 

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

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

Export to PDFs.jsx

DESCRIPTION

This sample gets files specified by the user from the 
selected folder and batch processes them and saves them 
as PDFs.

Edits by Patrick Mineault:
 - only .ai files processed
 - files saved in same folder as the input files
 - export files have name (oldname)_export.pdf
 - PDF settings: non-editable / acrobatLayers=false 
      for maximum compatibility with Preview

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

// 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 .ai files you want to convert to PDF');

// If a valid folder is selected
if ( sourceFolder != null )
{
    files = new Array();
    fileType = "*.ai"; //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.', '~' );
        destFolder = sourceFolder;
        for ( i = 0; i < files.length; i++ )
        {
            sourceDoc = app.open(files[i]); // 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 = '_export.pdf'; // new extension for pdf file
    newName = "";

    for ( var i = 0 ; docName[i] != "." ; i++ )
    {
        newName += docName[i];
    }
    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 = false;
    pdfSaveOpts.colorBars = false;
    pdfSaveOpts.colorCompression = CompressionQuality.AUTOMATICJPEGHIGH;
    pdfSaveOpts.compressArt = true; //default
    pdfSaveOpts.embedICCProfile = true;
    pdfSaveOpts.enablePlainText = true;
    pdfSaveOpts.generateThumbnails = true; // default
    pdfSaveOpts.optimization = true;
    pdfSaveOpts.pageInformation = false;
    pdfSaveOpts.preserveEditability = false;

    return pdfSaveOpts;
}
Save this as Export to PDFs.jsx and inside Illustrator run it by selecting File > Scripts > Other Scripts (or press Ctrl+F12 if the keyboard shortcuts are set to default). The rest should be pretty obvious.

26 thoughts on “Batch export .ai files to pdf

  1. hello, almost what i was looking for…
    but it keep the bleeding on the file. any option we could add to remove this ?
    thanks in advance…

  2. Useless in Illustrator CC.
    It modifies the text paragraph attributes during conversion and gives different results.

  3. Experts,
    I am looking for the target file name should be same as source file. Is that possible?.
    Currently, it puts filename as “untitled”.
    Thank You

  4. Hi there, i just added my pdf opts to the “sub-folder routine” that Thomas modified and this is working right now with cs5 and ML 10.8.3. I have 3 subfolders with old files which i don´t want to convert, but it easier to erase the pdfs from this folders than saving each individual file to pdf. Thanks Thomas and Patrick for this scripts!

  5. Hi there!, i was trying this useful script on snow leopard (10.6.8) and cs5 and then i moved to mountain lion 10.8.3 (terrible mistake) and cs5 and voila! the script no longer works. when i run the script in the extended toolkit it shows me runtime errors beginning at (sourceDoc = app.open…) so far i have not found a link between jsx not working in 10.8.3 and this script. Console on the toolkit shows me nothing apart from highlight the runtime errors. Do you have some guidance on what could be tha problem?? thanks for your time and cheers for the script.

    1. Hi Nikola,

      I’ve got a working version now. I haven’t tested it thoroughly and I haven’t built in any error-handling, but I did test it with 3 levels of subfolders and it seemed to be working.

      All PDF-files are saved in the same folder, where the AI-files were found.

      http://pastebin.com/PeRBP6TW

      Let me know if you have questions, issues or anything like that :)

    1. Hey Nikola. I did a short script (or rather, I stole it from somewhere :P) the other day, that travels through all subfolders and returns the appropriate files.

      Drop a comment here, so I’ll get a notification and I’ll make sure to post it tomorrow along with a few comments :)

    2. Hi Nikola,

      I’ve posted it here.

      http://pastebin.com/CNETChXm

      I’m not gonna take credit for the script itself, but the more we get it out there, the better.

      find_files returns an array of files, that you can manipulate how you see fit. If you need to find other extensions than .ai (.pdf, .indd etc). remember to change it on line 17. You also need to change the “-3” in name.slice() to the correct number (-4 for pdf or -5 for indd) :)

      Drop a comment here, if you need further help

      1. Hi Thomas,

        Thanks for the script, I really appreciate it.

        I tryed to implement it, but no luck :) I see in Console that it runs trough all sub folders, but finds no files to execute, but it still works on a single folder…

        I’m not experienced programmer, but tryed to understand how it works. :)

        I know I miss something and was trying all morning with different variations, this is what I have so far.

        http://pastebin.com/NWMpRdbj

      2. I’ll have a look at it and see what I can conjure up. Not gonna have too much time until tomorrow afternoon though.

      3. Hi Kai,

        Change this line:
        var pdfSaveOpts = new PDFSaveOptions();

        to:
        var pdfSaveOpts = app.pdfExportPresets.item(“[High Quality Print]”);

        And change the name inside the brackets to whichever PDF preset you prefer.

  6. You’ve just made me the most popular man in the company. Thanks for this. This will save my colleagues countless hours!

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s