Acrobat: Reduce File Size of PDF to below 4mb

Hi all,

I’m working on automating the routine of reducing the filesize of a PDF document. I need the document to be below 4mb. The original PDFs can be anywhere between 40mb and 120mb. The best workflow thus far starting with 1) InDesign is Export as PDF, 2) open pdf and “Reduce File Size…” overwriting existing file, 3) “save as…” individual Tiff files in new “Temp” folder with resolution at 200dpi, 4) combine all Tiff files into one doc and “save as…” originalName&“_sm”.extension, 5) “Reduce File Size…” overwriting existing file. 6) If file is not smaller than 4mb, repeat process from 1-5. Ugh!!! That takes SO MUCH TIME and my constant attention.

I’ve roughed out the basic framework for a script that will tackle this, but can’t get the save funtion (or anything afterwards to work).

Your help would be greatly appreciated!

Thanks!!!
-Evan

set sourceFile1 to choose file with prompt "Select first file to convert to small PDF."
set sourceFiles to {sourceFile1}

set fileCount to count of items in sourceFiles

set myFileSize to size of (info for sourceFile1)
if myFileSize is greater than 4000000 then
	set sourceFile1 to my reduceFileSize(sourceFile1)
end if

on reduceFileSize(sourceFile1)
	repeat with fileName in sourceFile1
		set {name:fileName} to info for sourceFile1
		if fileName contains "." then
			set tid to AppleScript's text item delimiters
			set AppleScript's text item delimiters to "."
			set fileName to item 1 of fileName
			set AppleScript's text item delimiters to tid
		end if
		set OutFile to fileName & "_proof"
	end repeat
	
	tell application "Adobe Acrobat 7.0 Professional"
		activate
		open sourceFile1
		--save to sourceFile1
		--reduce file size overwriting existing file
		--try
		--save file as Tiffs in folder myTempTiffs
		--on error
		--create folder myTempTiffs
		--save file as Tiffs in folder myTempTiffs
		--end try
		--make pdf from multiple files
		--select all files of myTempTiffs
		--save as filename & "_sm.pdf"
		--reduce file size overwriting existing file
		close sourceFile1
	end tell
end reduceFileSize

my method this

  1. print to postscript file
  2. convert PS filet to PDF using apple’s PSTOPDF
  3. use PDFTK compress function to reduce the file size

this works extremely well for me , you may want to try it by hand first and see if you ge the results that you are looking for.

MM

Hi Evan,
Another option for you to try, Export from Indesign as a pdf then run PDF Shrink http://www.apagoinc.com/prod_home.php?prod_id=30 on the exported pdf and save to a new folder. You can download a demo version of PDF Shrink and try one of your exported files on it using this code:

set thefile to choose file
set save_path to (path to desktop as string) & "converted files" as alias

tell application "PDF Shrink"
	process thefile using "Web" destination save_path
end tell

The great thing about PDF shrink is that you can specify the settings for the images and other settings. You can always incorporate the export as pdf from Indesign as part of the script and have the whole procedure done in one. I don’t think that the Reduce File Size option in Acrobat is a scriptable function, at least not without using GUI scripting.
Nik

MM and Nik,

Thanks for your advice.

MM, I couldn’t find the PStoPDF application or Automator task. If you were suggesting to use Distiller, I tried that too. Again, it didn’t compress the file enough.

Nik, that compression method only reduced the filesize from 38mb to 32mb. I’m pretty sure I worked the settings even smaller than the “email” presets. Any further suggestions?

Anyone have any suggestions on the scripting?

Hi Evan,
How much smaller is your 38mb pdf if you use Acrobats Reduce File Size? Is your pdf a single or multiple page pdf? Is it not possible to export as a pdf from Indesign using [smallest file size] preset? If you need to create a Hi Res and Low Res pdf then we could always export from Indesign twice using two different presets!
Thanks,
Nik

P.S. If there’s no other option this worked for me in Acrobat 7

	tell application "Finder"
		set pdfItemList to every file of released_ad_basket whose name ends with ".pdf"
		repeat with i from 1 to number of items of pdfItemList
			set this_item to item i of pdfItemList
			set this_file_name to name of this_item
			tell application "Adobe Acrobat 7.0 Professional" to activate
			tell application "Adobe Acrobat 7.0 Professional" to open (this_item as string)
			tell application "System Events"
				tell process "Acrobat"
					delay 1
					click menu item "Reduce File Size..." of menu 1 of menu bar item "File" of menu bar 1
					delay 1
					click button "OK" of group 1 of window "Reduce File Size"
					delay 2
					click button "Save" of window "Save As"
					delay 1
					click button "Replace" of window 1
					delay 7
				end tell
			end tell
			tell application "Adobe Acrobat 7.0 Professional" to close all docs saving "No"
		end repeat
	end tell

Evan,

PSTOPDF is a shell command that takes input file and converts to PDF file
PDFTK can be found here http://www.accesspdf.com/pdftk/
here is an example


set dt to path to desktop as Unicode text
set infile to dt & "test.ps"
set infile2 to quoted form of POSIX path of (dt & "test.pdf")
set outfile to quoted form of POSIX path of (dt & "test_compressed.pdf")
do shell script ("pstopdf " & infile)

do shell script ("/usr/local/bin/pdftk " & infile2 & " output " & outfile & " compress")

mm

Nik,

Well, I tried your code (plus a line to specify the desired folder), but got an error: “System events got an error: NSReceiverEvaluationScriptError: 4” I had this error before when trying to use menu items with applescript. After a little research I found an article that quickly dissipated the issue.

http://frater8.blogspot.com/2007/05/system-events-got-error.html

In summary: “GUI Scripting needs to be enabled. this can be enabled in either the Universal Access preference pane (i didn’t find it there myself) or the Applescript Utility in the Utilities directory. it may be the Enable access for assistive devices checkbox but if it is then it’s not well marked as to what it does. what this does, in my limited understanding, is enables the script to use System Events to control the user interface such as buttons and menus.”

With this taken care of, I’ll reintroduce my initial loop function and try to integrate the “save as tiff” option. Then recombine the files together. If anyone’s interested I can post updates on the scripting.

Thanks again!!!

(mm- Thanks for your suggestions, too. I just am not as familiar with some of the functions you employ and will try that if my method doesn’t pan out. Thanks though!)

Ok, so I’m getting really close. But, how in the world do you tell System Events to select an item from a dropdown menu?? The code that is commented out doesn’t yet work, but maybe it would if I could select “tiff” from that dialog dropdown menu.

Thanks!

tell application "Finder"
	
	set sourceFolder to choose folder with prompt "Select folder of PDFs to convert to small PDFs."
	
	set pdfItemList to every file of sourceFolder whose name ends with ".pdf"
	
	repeat with i from 1 to number of items of pdfItemList
		set myFile to item i of pdfItemList
		set myFileSize to size of myFile
		if myFileSize is greater than 4000000 then
			set myFile to my reduceFileSize(myFile)
		end if
	end repeat
	
end tell

on reduceFileSize(myFile)
	tell application "Finder"
		set this_file_name to name of myFile
		tell application "Adobe Acrobat 7.0 Professional" to activate
		tell application "Adobe Acrobat 7.0 Professional" to open (myFile as string)
		tell application "System Events"
			tell process "Acrobat"
				delay 1
				click menu item "Reduce File Size..." of menu 1 of menu bar item "File" of menu bar 1
				delay 1
				click button "OK" of group 1 of window "Reduce File Size"
				delay 1
				click button "Replace" of window 1
				delay 25
				
				click menu item "Save As..." of menu 1 of menu bar item "File" of menu bar 1
				delay 1
				--CHANGE FORMAT TO "TIFF"
				--click button "settings..."
				--delay 3
				--set (text field 1 of window "Save As TIFF Settings") to "200"
				--delay 2
				--click button "OK" of window "Save As TIFF Settings"
				--delay 1
				--click button "OK" of window "Save As"
				--delay 3
			end tell
		end tell
		tell application "Adobe Acrobat 7.0 Professional" to close all docs saving "No"
	end tell
end reduceFileSize

Hi Evan,
Can you tell me why you want to save the pdf that has had it’s file size reduced to a tiff? Can you give this a try just so that we can make a comparison of file sizes? Just open one of your documents in Indesign and run this script:

tell application "Adobe InDesign CS2"
	set the_container to (file path of document 1 as string)
	export document 1 format PDF type to (the_container & "Test") using PDF export preset "[Smallest File Size]" without showing options
end tell

It seems to me that you’re going to a lot of effort to try and create a small pdf when you maybe able to do this directly from Indesgin, appologies if I’m missing the point!
Nik

Nik,

Perhaps it’s the nature of the files I’m dealing with, but they just don’t get small enough with the typical methods. Every other page contains at least 30 (detailed) placed eps files. My first move is always to export from InDesign as “Smallest File Size”. Even so, the files are about 38mb. I don’t think even my super-fast computer could handle rendering a high-quality PDF.

Does that make more sense now? I can try it when I get back to work in the morning, but it seems like your script will just export the file from InDesign using the “Smallest File Size” option.

Are there any resources you know of that might explain how to select an option from a dropdown menu in a dialog box?

I ran some analysis on that dialog box, and it returned that there are about 14 UI elements. (I asked it to get all UI elements in that window.) I then gave it a repeat function to click on each of them in turn. Etc… No dice on that. Plus, only about 6 were named. Others were “missing name”, or something to that effect.

-Evan

Hi Evan,
I think I understand now!! Acrobat seems to be a little funny with regard to what you’re allowd to script, after giving this some thought maybe you could use Image Events to convert the pdf to a tif? I tested the script below on a single page pdf and it creates a 150ppi tif image. Give this a try

set thepath to path to desktop as string
set thefile to choose file
tell application "Finder" to set filename to (characters 1 thru -5 of (the name of thefile as string)) as string
set savepath to (path to desktop as string) & filename & ".tif"
tell application "Image Events"
	launch
	set openfile to open thefile
	save openfile as TIFF in file savepath with icon
end tell

Hi Evan,
OK, idea Number 2, do you have to save them as tif files? how about you export the pages from Indesign as jpgs and then merge them back as a pdf? Here’s the code for Indesign.

tell application "Adobe InDesign CS2"
	set the_container to (file path of document 1 as string)
	export document 1 format JPG to (the_container & "Test.jpg") without showing options
end tell

If this option works for you then we can look at combining the jpegs back as a pdf!
Nik

Nik,

Now that is sweet satisfaction. I ran that script, then combined the files to make a PDF and it was quite teeny.

Just a couple mods now:
make the jpgs 200dpi
combine the jpgs together into a PDF
delete the jpgs

I can then throw on some code that will repeat this for ever file in a folder.

Thanks so much, fantastic!

-E

Also, where did you learn Applescript and what resources would you recommend?

I tried telling it to combine all files of the_container whose name ends with “.jpg” but it couldn’t get files of the_container. Similarly, couldn’t delete those files because it couldn’t “get” them.

Hi Evan,
I’m pleased we’re getting somewhere, This next script will turn the jpg files into single pdfs then I’ve used JoinPDF 2.3.1 available here: http://www.iis.ee.ic.ac.uk/~g.briscoe/joinPDF/ to combine the pdf back into a combined pdf. Just make sure that this has been installed before you run the script otherwise it’ll error. I’ll look at the jpg settings while you’re doing this!
Nik

set source_folder to (path to the desktop as string) & "jpeg files" as alias

tell application "Finder" to set item_list to every item of source_folder whose name ends with ".jpg"

repeat with this_jpg in item_list
	set this_jpg to this_jpg as alias
	tell application "Adobe Acrobat 7.0 Professional"
		open this_jpg
		set pdf_doc_name to the name of document 1
		save document 1 to file ((source_folder as string) & pdf_doc_name)
		close document 1
	end tell
	--delete this_jpg
end repeat

tell application "Finder" to set pdf_list to name of every item of source_folder whose name ends with ".pdf"

--> This next part of the script requires JoinPDF2.3.1 to be installed

set output_path to POSIX path of source_folder

set destfile to POSIX path of source_folder & "combined.pdf"
set RawScript to "joinPDF " & "'" & destfile & "'" & space
repeat with this_pdf in pdf_list
	set fullpath to "'" & output_path & this_pdf & "'" & space
	set RawScript to RawScript & "'" & output_path & this_pdf & "'" & space
end repeat

do shell script RawScript

repeat with this_item in pdf_list
	set this_pdf_alias to (source_folder as string) & this_item as alias
	tell application "Finder" to delete this_pdf_alias
end repeat

once step closer…

tell application "Adobe InDesign CS3"
	set the_container to (file path of document 1 as string)
	--can be low/medium/high/maximum
	set JPEG Quality of JPEG export preferences to maximum
	set resolution of JPEG export preferences to 200
	export document 1 format JPG to (the_container & "Test.jpg")
end tell

Excellent, I don’t think you could specify the resolution in CS2 so that’s a bonus. Let me know how you get on with the other bits!
Nik

Really close now!

I made a couple edits to allow it to work for multiple files. However there’s one or two bugs involved with this.

set source_folder to (choose folder with prompt "Select folder of InDesign files to convert to PDFs.")

tell application "Finder" to set indd_item_list to (every item of source_folder whose name ends with ".indd")

repeat with i from 1 to number of items of indd_item_list
	set this_item to item i of indd_item_list
	set this_file_name to name of this_item
	
	tell application "Adobe InDesign CS3"
		activate
		open this_item
		--can be low/medium/high/maximum
		set JPEG Quality of JPEG export preferences to maximum
		set resolution of JPEG export preferences to 200
		set fileStringPath to source_folder & this_file_name & ".jpg" as string
		export document 1 format JPG to fileStringPath
	end tell
	
	--renames the files for easier batch pdf processing
	--the following script lines don't work because it can't "get the files", any suggestions??
	set jpgsToRename to (every item of source_folder as string whose name ends with ".jpg")
	repeat with p from 1 to count of jpgsToRename
		tell application "Finder"
			set name of item i of jpgsToRename to (this_file_name & "_" & i & ".jpg")
		end tell
	end repeat
	
	--
	--Part 2
	--
	
	--picks up only the current jpg files
	set item_list to (every item of source_folder whose name ends with "_" & i & ".jpg")
	
	repeat with this_jpg in item_list
		set this_jpg to this_jpg as alias
		tell application "Adobe Acrobat 7.0 Professional"
			close documents
			open this_jpg
			set pdf_doc_name to the name of document 1
			save document 1 to file ((source_folder as string) & pdf_doc_name)
			close document 1
		end tell
		delete this_jpg
	end repeat
	
	tell application "Finder" to set pdf_list to (name of every item of source_folder whose name ends with "_" & i & ".pdf")
	
	--> This next part of the script requires JoinPDF2.3.1 to be installed
	
	set output_path to POSIX path of source_folder
	
	set destfile to POSIX path of source_folder & this_file_name & "_" & i & ".pdf"
	set RawScript to "joinPDF " & "'" & destfile & "'" & space
	repeat with this_pdf in pdf_list
		set fullpath to "'" & output_path & this_pdf & "'" & space
		set RawScript to RawScript & "'" & output_path & this_pdf & "'" & space
	end repeat
	
	do shell script RawScript
	
	repeat with this_item in pdf_list
		set this_pdf_alias to (source_folder as string) & this_item as alias
		tell application "Finder" to delete this_pdf_alias
	end repeat
	
end repeat

Hi Evan,
try this code for the renaming of the jpgs:

tell application "Finder" to set jpgsToRename to every item of source_folder whose name ends with ".jpg"

set file_count to 1
repeat with this_jpg in jpgsToRename
	tell application "Finder" to set name of this_jpg to this_file_name & "_" & file_count & ".jpg" as string
	set file_count to file_count + 1
end repeat

Nik

Hi Evan,
I found a few other erros with your code so give this complete code a try:

set source_folder to (choose folder with prompt "Select folder of InDesign files to convert to PDFs.")

tell application "Finder" to set indd_item_list to (every item of source_folder whose name ends with ".indd")

repeat with this_indd in indd_item_list
	set indd_filename to the name of this_indd
	set this_file_name to characters 1 thru -6 of indd_filename as string
	
	tell application "Adobe InDesign CS3"
		activate
		open this_indd
		--can be low/medium/high/maximum
		set JPEG Quality of JPEG export preferences to maximum
		set resolution of JPEG export preferences to 200
		set fileStringPath to source_folder & this_file_name & ".jpg" as string
		export document 1 format JPG to fileStringPath
		close document 1 saving no
	end tell
end repeat

tell application "Finder" to set jpgsToRename to every item of source_folder whose name ends with ".jpg"

set file_count to 1
repeat with this_jpg in jpgsToRename
	tell application "Finder" to set name of this_jpg to this_file_name & "_" & file_count & ".jpg" as string
	set file_count to file_count + 1
end repeat

tell application "Finder" to set item_list to (every item of source_folder whose name ends with ".jpg")

repeat with this_jpg in item_list
	set this_jpg to this_jpg as alias
	tell application "Adobe Acrobat 7.0 Professional"
		open this_jpg
		set pdf_doc_name to the name of document 1
		save document 1 to file ((source_folder as string) & pdf_doc_name)
		close document 1
	end tell
	tell application "Finder" to delete this_jpg
end repeat

tell application "Finder" to set pdf_list to name of every item of source_folder whose name ends with ".pdf"

--> This next part of the script requires JoinPDF2.3.1 to be installed

set output_path to POSIX path of source_folder

set destfile to POSIX path of source_folder & "combined.pdf"
set RawScript to "joinPDF " & "'" & destfile & "'" & space
repeat with this_pdf in pdf_list
	set fullpath to "'" & output_path & this_pdf & "'" & space
	set RawScript to RawScript & "'" & output_path & this_pdf & "'" & space
end repeat

do shell script RawScript

repeat with this_item in pdf_list
	set this_pdf_alias to (source_folder as string) & this_item as alias
	tell application "Finder" to delete this_pdf_alias
end repeat

Nik