Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Click here to return to the 'Use a free third-party Java tool to truly compress PDFs' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Use a free third-party Java tool to truly compress PDFs
Authored by: Sveto on Jan 15, '10 10:14:13AM

Depends on the needs. As I stated the compression in this case is adjusted for general purposes. That means it removes all embedded fonts, image previews and compresses the images in JPEG. So it removes all the "junk" included by MacOS when a PDF is created from the Print menu. The resulting file now is lightweight yet with sufficient quality to be printed in general. If you have used specific fonts in a fancy presentation, than the computer you are opening the file on might not have them and will replace with others that might change the experience overall. In the case of ColoSync compression filter you might get confused as to the grade of JPEG deterioration, thinking it looks ok, yet if you try to print it will look horrible.

My proposal works really great for:
*/ printing Fedex labels out of the web browser
*/ most of the internet articles, or others that include both text and images (try the normal PDF creation and you get 10 times bigger files, try your ColorSync filter and you get even bigger file, or really bad quality, you will notice right away)
*/ most word, excel, text documents

Now for the paranoid once. I was thinking of giving an alternative to placing files in the system, but opted not to because placed Multivalent that way makes it available and recognized by java so you can use it with short references for many other things like extracting text from PDF and tones of others, just read from the Multivalent page all it offers. It is a lot!

If you want to keep Multivalent on your desktop replace the shell script text like this:

cat "$1" > /Users/your_name/Desktop/PDF/temp$$.pdf && java -classpath /Users/your_name/Desktop/PDF/Multivalent.jar tool.pdf.Compress -compatible -noembed -noalt -jpeg /Users/your_name/Desktop/PDF/temp$$.pdf && rm /Users/your_name/Desktop/PDF/temp$$.pdf

This assumes you placed the Multivalent.jar directly in your Desktop/PDF/ folder, where the compressed files are generated. You can change it to any other place by adjusting the location after the classpath

Finally I hope someone would suggest optimization to the Automator workflow so a "Save As" window can pop-up when the workflow is selected.

There is no need to resent my hint. Test it, read what it can do, if you don't like it, just dump the Multivalent and the workflow and your system will be like before.

[ Reply to This | # ]
Ability to choose output file name and more
Authored by: Krioni on Jan 17, '10 10:12:55PM
OK. So, Sveto pointed out that it would be nice to be able to choose an output folder. So, I rewrote the code for this hint using AppleScript. When creating your workflow, instead of using the "Run Shell Script" command, use the "Run AppleScript" command instead. In the box for the AppleScript code, paste the following:

on run {input, parameters}
	set homePosix to POSIX path of (path to home folder) as string
	set desktopPath to (path to desktop) as string
	set multivalentPosix to homePosix & "bin/pdftools/Multivalent.jar"
	set useOutputFolder to true -- use the named folder as the default save location
	set outputFolderPath to desktopPath & "PDF" & ":" -- a Mac path, not Posix
	-- can ignore outputFolderPath if you set useOutputFolder to false
	set askFileName to true -- ASK for an output file name (and folder). 
	-- If false, uses the default document name generated by Mac OS X
	-- in the default folder (specified above or Desktop)
	if useOutputFolder then
			alias outputFolderPath
		on error -- does not yet exist, so create folder
			set outputFolderName to getNameFromMacPath(outputFolderPath)
			tell application "Finder" to make new folder at pathAlias 
				with properties {name:outputFolderName}
		end try
	else -- default is just desktop
		set outputFolderPath to desktopPath
	end if
	repeat with oneItem in input
		set itemPath to (oneItem as string)
		set itemName to getNameFromMacPath(itemPath)
		set outputPath to outputFolderPath & itemName
		if askFileName then
			tell me to activate
			set outputPath to (choose file name with prompt 
				"Choose a place to save your compressed PDF:" default name itemName 
				default location (alias outputFolderPath)) as string
		end if
		set outPosix to POSIX path of outputPath
		-- tool.pdf.Compress writes the file to the same path as the input file, but with "-o.pdf":
		set compressedPath to text 1 thru -5 of itemPath & "-o.pdf"
		-- get all but ".pdf" to find temp output file name/path
		set compressCommand to "java -classpath " & quoted form of multivalentPosix & 
			" tool.pdf.Compress -compatible -noembed -noalt -jpeg " & 
			quoted form of POSIX path of itemPath
		-- compress the PDF created by Mac OS X:	
		do shell script compressCommand
		-- now, move the compressed PDF to the desired location:
		do shell script "mv " & quoted form of POSIX path of compressedPath 
			& " " & quoted form of outPosix
	end repeat
	return input
end run

on getNameFromMacPath(macPath)
	set {oldDelims, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
	set nameFromPath to last text item of macPath
	set AppleScript's text item delimiters to oldDelims
	return nameFromPath
end getNameFromMacPath
NOTE: There are PREFERENCES included in the code above. They allow you to specify the location of your Multivalent JAR file, assuming you want it in a sub-folder of your user folder. If that is wrong, you can remove the 'homePosix &' part and just specify the whole Posix path to the JAR file. You can also choose a default outputFolderPath for the compressed PDF - if you don't it defaults to using the Desktop. Also, the code above will ask you for a file name, you can set askFileName to false to just use the name Mac OS X comes up with. I hope this is useful. I look forward to using it, since it seems to save quite a bit of space in PDFs. Thank you to Sveto for getting us the info about Multivalent!

[ Reply to This | # ]
Ability to choose output file name and more
Authored by: Sveto on Jan 19, '10 08:17:45AM

Thank you very much Krioni. That is exactly what I was requesting. I have only 1 question. I was unable to understand where the temporary uncompressed file was located and if it was deleted after the compression. Did I miss something?

[ Reply to This | # ]
Ability to choose output file name and more
Authored by: Krioni on Jan 19, '10 10:26:39AM

Mac OS X creates the temp file itself in a temp directory that gets cleaned out automatically after a while. When you choose "Preview" from the print dialog, it's the temp file that is opened by Interestingly enough, Apple made the usual command-click-on-window-title-to-see-file-path trick NOT work when you do this in However, if you toss an alias to Skim (or some other PDF reader) in your PDF Services folder, and send the PDF there instead of, you'll be able to command-click the window titlebar to see where it creates the temp file.

That meant I didn't have to clean up the temp file, since it is handled in the Mac OS X approved manner. In your original hint, you may have been able to do the same thing. I think $1 is the path to the temp file, so perhaps you could have just operated on it instead of using cat and a redirect to send it to your temp location. Of course, you'd then need to "mv" the output file (and perhaps remove the "-o" that gets tacked into the file name) to your desired destination.


[ Reply to This | # ]