Applescript / Javascript > Adobe Acrobat scripting

PDF Optimization

(1/2) > >>

Recently, I got challenged to find a solution for batch processing PDF documents. In this process, each PDF document had to be cropped, optimize for the web and saved as individual pages.

To my biggest surprise, the "optimize for the web" part proved to be the most elusive. After searching the Net, the only thing I was able to find were third party solution costing upwards of $500.

But then, an Adobe employee told me that since version 9.1 you could could make javascript calls to trigger a specific Preflight Profile. Up until then, I thought those profile were only there to "check" things up in my PDF documents but after reading up on them and playing with them, I realize they can do much more than that. Acrobat ships with quite a few of those Profiles pre-installed, furthermore, we can all create custom profiles that better suit our needs for any given situation.

To make everything work, I had to make the proper Javascript call. For this I referred to the Javascript 9.1 SDK where I searched for the keyword "Preflight". While I think that those reference pages are generally light on description and working examples, I was able to achieve what I set out to do using the following construct:

--- Quote ---001     tell application "Adobe Acrobat Pro"
002          activate
003          tell document 1
004               -- Optimize for web
005               with timeout of (1 * hours) seconds
006                    set RunPreflight_JS to ""
007                    set RunPreflight_JS to RunPreflight_JS & "var oProfile = Preflight.getProfileByName(\"" & ProfileName & "\")" & return
008                    set RunPreflight_JS to RunPreflight_JS & "if( oProfile != undefined ) {" & return
009                    set RunPreflight_JS to RunPreflight_JS & tab & "var oThermometer = app.thermometer;" & return
010                    set RunPreflight_JS to RunPreflight_JS & tab & "var myPreflightResult = this.preflight( oProfile, false, oThermometer);" & return
011                    set RunPreflight_JS to RunPreflight_JS & tab & "if( myPreflightResult.numErrors > 0 ) {" & return
012                    set RunPreflight_JS to RunPreflight_JS & tab & tab & "var theResult = 'Error';" & return
013                    set RunPreflight_JS to RunPreflight_JS & tab & "} else {" & return
014                    set RunPreflight_JS to RunPreflight_JS & tab & tab & "var theResult =  'OK';" & return
015                    set RunPreflight_JS to RunPreflight_JS & tab & "}" & return
016                    set RunPreflight_JS to RunPreflight_JS & "} else {" & return
017                    set RunPreflight_JS to RunPreflight_JS & tab & tab & "var theResult =  'Profile Missing';" & return
018                    set RunPreflight_JS to RunPreflight_JS & "}" & return
019                    set RunPreflight_JS to RunPreflight_JS & "theResult;"
021                    set ProfileCheck to do script RunPreflight_JS
022               end timeout
023          end tell
024     end tell
--- End quote ---

Here, ProfileName refers to the Preflight Profile I wish to run on the current document.

This Javascript, or  ProfileCheck has 3 possible outcomes:

* Profile Missing: The Preflight Profile referenced in getProfileByName is not installed;
* Error: There were errors while running the specified Preflight Profile;
* OK: The Preflight Profile ran without errors.

P.S. I know that the tab are not needed in the above script, I just like putting them in for readability.

Preflight Profiles have been around since Acrobat InProduction (equivalent to Acrobat 4) I used this very crudely to fix troublesome PDFs that would'nt RIP on an old level2 imagesetter, this was before I'd used Pitstop. The problem is that the interface for these Profiles is scarily complicated and I don't find it at all intuitive. I'm always impressed by forum posters who suggest fixing things using these tools but even Pitstop's v8 disaster of a GUI is streets ahead. I'm under the impression that a lot of Acrobat Pro's Profiles and FixUps are written by Callas and that their PDFToolbox is a user friendly GUI into it.

Anyway I'm glad you found your solution.

By The way, what does optimize for the web do?

optimize for web makes a multi page PDF document only download data on a page by page request from a browser.


--- Quote from: Glenn on October 29, 2009, 10:34:05 AM ---By The way, what does optimize for the web do?

--- End quote ---

Well, there is two side to this question.

To make a PDF web friendly your first try would be to save your PDF without linearize (or is it WITH I can't remember). This reduce the file size somewhat while flattening form I believe and other nebulous stuff. This is akin to choosing the Optimize for Fast Web View option when optimizing a PDF. I believe this option allows you to streem the PDF to the end user (allowing the end user to view pages before the whole thing has finished downloading).

In my situation however I needed to compress images as well. Acrobat comes with the "Online Publishing (optimize for size)" Preflight Profile that does that pretty well but I needed to tweak it somewhat for my needs. So I created the necessary "fixups" and setup my own customized profile.


I do very little for web usage myself but lots of PDF repurposing though so this will become very handy indeed. (hence the karma)  ;)


[0] Message Index

[#] Next page

Go to full version