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

Use a free third-party Java tool to truly compress PDFs Apps
All methods I have tried so far to compress PDFs have never really lived up to what I needed. Unless you are ready to pay for a commercial product, you are basically left to deal with the ColorSync options, which are extremely damaging to a PDF created by the Mac OS, or to the other extreme, simply do not compress and actually make the resulting file bigger. You also can't remove the fonts from a PDF with ColorSync.

So I put together a few tidbits of advice I found on the net, and came up with a robust solution that's both free and works from the Print dialog box. Here the details.

First of you need to get Multivalent, which is a free product (at least for our purposes, it is free). As of this writing, however, the latest version is missing the Tools component (it's undergoing a heavy revision), and that would break our setup. So you need to get this older version: Multivalent20060102.jar. You can find it at any of these sites:

site 1 | site 2 | site 3
(links courtesy of math_crank).

Once you have this downloaded, rename it to Multivalent.jar for simplicity, and place it in the following location:
/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home/lib/ext
[robg adds: Read my footnote at the end for a method of using this tool without installing it in the /System folder.]

If you have an older version of java, you will have to adjust the above address; mine is 1.6.0. Now open Automator and create a workflow. Drag the Run Shell Script action to the work area. In that action, change the pop-up for the code to as arguments, and paste the following:
cat "$1" > /Users/your_name/Desktop/PDF/temp$$.pdf && java tool.pdf.Compress -compatible -noembed -noalt -jpeg /Users/your_name/Desktop/PDF/temp$$.pdf && rm /Users/your_name/Desktop/PDF/temp$$.pdf
Change your_name to whatever your home directory is called, and if that name has spaces in it, you'll need to either escape them with a backslash, or enclose the entire directory name in quotes.

Save your workflow as Truly Compress PDF.workflow in the PDF Services folder in either in your home Library, or in the top-level Library.

On the Desktop, create a folder (named PDF in the sample workflow) that will be receiving your compressed files. You will have to change the name of the file to your desired name, as the above workflow creates a generic tempXXX-o.pdf name. For anything you need to print as a PDF, go to Print » PDF » Truly Compress PDF (or whatever you named it).

This works well under Snow Leopard, but I'd guess with small changes to the save location for Multivalent, it would function on previous OS versions.

You can add or remove arguments in the script line according to what you desire. Read about the options on the PDF Compress on the Mutivalent site. But I have used a nice once, and as long as you don't need fancy fonts, it will squeeze your files almost without any quality losses.

I don't have more time to spend on this, but it would be great if someone could expand the script and/or make changes to the Automator workflow, so when we select that workflow we get a "Save As" window and than name the file at once the proper way. If that's done, the need for a PDF folder will vanish, and the temp file can be created and deleted in the temp directory and not on the Desktop. Any help in this respect will be gladly appreciated.

[robg adds: You can also find the older version of Multivalent at SourceForge. I wanted to test this hint, but didn't want to install anything in my /System folder, and my Mac Pro still runs 10.5, so here's how I modified it. First I put Multivalent in its own folder named pdftools in my user's bin folder (which I've previously created).

In Terminal, I then ran these commands to expand and then remove the jar file:
$ cd ~/bin
$ jar xfv Multivalent20060102.jar 
$ rm Multivalent20060102.jar
Once expanded like this, you can directly access the various tools by running them from that directory. So I modified the workflow to read as follows:
cat "$1" > /Users/your_name/Desktop/PDF/temp$$.pdf && java /Users/your_name/bin/pdftools/tool.pdf.Compress -compatible -noembed -noalt -jpeg /Users/your_name/Desktop/PDF/temp$$.pdf && rm /Users/your_name/Desktop/PDF/temp$$.pdf
As above, replaced your_name with your user's name, and modify the path to the expanded Multivalent bundle as appropriate. Used in this manner, it worked perfectly (in 10.5, and I assume, in 10.6) without modifying the /System folder.]
    •    
  • Currently 2.79 / 5
  You rated: 2 / 5 (24 votes cast)
 
[16,225 views]  

Use a free third-party Java tool to truly compress PDFs | 12 comments | Create New Account
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: wrk3 on Jan 15, '10 08:38:27AM

This hint didn't really mention what was to be compressed (text data or graphic file data). I've been saving paper receipts to PDFs using only Image Capture, Preview and ColorSync, getting both high compression and good quality in the resulting PDF files. The key was to create a new filter in ColorSync.

Open the ColorSync Utility, click the Filters menu button and then the plus symbol at the bottom to create a new filter. Name the filter, then click the reveal triangle on the right side to bring up a menu. Choose "Add Image Effects Component," then "Image Compression." This will add a new drop-down titled "Mode:" set to Automatic. Change "Automatic" to "JPEG" and set the quality to whatever you want. I have it set to minimum. Although you might think it would devastate text images, I find the images perfectly readable at 100% magnification (and I'm talking about poor-quality text; a la grocery store checkout receipts).

Once done, close ColorSync and import a text image into Preview, then choose File -> Save As. In the Save As dialog, change the Format drop-down to PDF and change the Quartz Filter drop-down to whatever you named your new filter. The saved file will be reduced by whatever you set the JPEG compression to, with very good image quality. And no need to install anything.



[ Reply to This | # ]
Use a free third-party Java tool to truly compress PDFs
Authored by: chuang on Jan 21, '10 06:58:33AM

Tried your suggestion. JPG was 522 Kb and reduced to 36Kb and the quality looks as good if not better. Well impressed and thank you for a great tip!

Michael



[ Reply to This | # ]
Use a free third-party Java tool to truly compress PDFs
Authored by: sabi on Jan 15, '10 09:07:56AM

Putting things in the systemwide Java extension directory as this hint recommends is really not a good idea. First, there is a user-specific alternative on Mac OS X: ~/Library/Java/Extensions. Second, all you need is to put the jar file on the classpath, like this:

java -cp Multivalent20060102.jar tool.pdf.Compress ...



[ Reply to This | # ]
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}
	
	-- INITIAL PATHS:
	set homePosix to POSIX path of (path to home folder) as string
	set desktopPath to (path to desktop) as string
	
	-- PREFERENCES:
	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
		try
			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!
---
http://www.danshockley.com


[ 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 Preview.app. Interestingly enough, Apple made the usual command-click-on-window-title-to-see-file-path trick NOT work when you do this in Preview.app. 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 Preview.app, 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.

---
http://www.danshockley.com



[ Reply to This | # ]
Use a free third-party Java tool to truly compress PDFs
Authored by: Chealion on Jan 16, '10 11:41:34AM

Another alternative is to use Quartz Filters. There are some custom Quartz Filters created that work substantially better than the standard "Reduce File Size" Quartz Filter that tends to give images all kinds of JPG artifacts by either reducing the DPI or changing the compression used.

See http://discussions.apple.com/thread.jspa?threadID=1292868&tstart=30 for the filters.

---
Chealion - The one and only! =)



[ Reply to This | # ]
Use a free third-party Java tool to truly compress PDFs
Authored by: phil4u2 on Jan 16, '10 05:31:55PM

I tried "Rob's version" with a ~/bin/pdftools/ folder, but it doesn't work for me.

I use Mac OSX 10.6.2

When using Multivalent directly in Terminal I get the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: /Users/xxx/bin/pdfcompress/tool/pdf/compress
Caused by: java.lang.ClassNotFoundException: .Users.philippe.bin.pdfcompress.tool.pdf.compress
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:315)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:330)
at java.lang.ClassLoader.loadClass(ClassLoader.java:250)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:398)



[ Reply to This | # ]
Use a free third-party Java tool to truly compress PDFs
Authored by: phil4u2 on Jan 16, '10 05:40:31PM

Later on , I tried the modified hint from Sveto, that is, without decompressing (or expanding) the file Multivalent.jar, but instead only placing it in ~/bin/pdftools/, and it is now working.



[ Reply to This | # ]
Use a free third-party Java tool to truly compress PDFs
Authored by: RedFred on Jan 18, '10 01:01:21PM
don't put the jar in /System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home/lib/ext
From Apple's documentation.
In Mac OS X, put your extensions in /Library/Java/Extensions/. Java automatically looks in this directory as it is starting up the Java Virtual Machine.

Putting extensions in /Library/Java/Extensions/ loads those extensions for every user on that particular computer. It is preferable to limit which users can use certain extensions by putting them in the ~/Library/Java/Extensions/ directory inside the appropriate users’ home directories. By default, that folder does not exist, so you may need to make it.


[ Reply to This | # ]
Use a free third-party Java tool to truly compress PDFs
Authored by: llee on Oct 16, '10 06:48:11PM

I'm grateful for the suggestion, because this was the only method for employing Java of all mentioned in this post that actually resulted in processing of the temp file in ~/Desktop/PDF with tool.pdf.Compress. However, upon completion the fonts in the output documents all turned to jibberish. Maybe I'll go back and read the Colorsync method again.



[ Reply to This | # ]