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:
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;"
020
021 set ProfileCheck to do script RunPreflight_JS
022 end timeout
023 end tell
024 end tell
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.
Enjoy!
P.S. I know that the tab are not needed in the above script, I just like putting them in for readability.