I routinely print and scan stuff to PDF format. Both in Windows and Linux this produces pretty large files, usually in the 400+K range. In Windows, you can spend $199 plus shipping or tax for Adobe Acrobat Pro, which will both allow you to print to a PDF and then modify the output. In Ubuntu, I use the free cups-pdf package to print web pages and the like to PDF format. It relies on ghostscript (also free) which is usually installed by default but not always. To install both from the terminal:
sudo aptitude install cups-pdf ghostscript
This will add a printer to your system simply called PDF that will put its output files in a new PDF directory in your home folder. You can change that, of course.
In order to shrink the output file, I wrote a little bash script based on the help provided on this page. You can simple open GEdit, Kate, or another text editor, then copy and paste the following into the new file:
#!/bin/bash
FILENAME=`basename $1`
DIRNAME=`dirname $1`
OUTNAME=opt_$FILENAME
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dQUIET -dBATCH -sOutputFile=”$OUTNAME” $1
rm ~/PDF/$1
mv ~/PDF/$OUTNAME ~/PDF/$1
The first block parses the file name passed to the script. The ‘gs’ command tells ghostscript how to reduce the file size, putting the result in a new file. The last block simply deletes the larger file and then renames the new, smaller PDF file to the original name. That’s all there is to it.
How well does it work? Well, I was able to reduce a 2.2 MB file printed from a web page down to 370K. Pretty effective.
As a side note, I tried to implement this script as a postprocessing step in the cups-pdf’s configuration file (/etc/cups/cups-pdf.conf), but no go after an entire afternoon of effort. Apparently Apparmor locks down CUPS and particularly cups-pdf pretty tightly, so they won’t communicate with an external script anymore without modifying the security settings. Pretty frustrating. If anybody has a fix that won’t overly compromise security (like turning off apparmor), please comment on this post.
In the meantime, I have a quick way to reduce PDFs down to manageable sizes in Ubuntu. Very cool. And free.
Subscribe to Reformed Musings
I have found using the convert tool from ImageMagick has the same affect. Scanned PDFs are reduced by ~90%
convert in.pdf out.pdf && rm in.pdf
By: psalmodyguy6o8 on February 16, 2011
at 3:08 pm
Thanks for stopping in and offering an alternative! I tried convert as suggested, but it had the opposite effect. I use this site as a pdf print sample because it makes a 2.3 MB file. The Ghostscript script reduces the output to 390K, but ImageMagick increased the file size to about 8.4 MB. I tried another page that pdfed at 428K and convert’s output it at 1.1 MB. There must be some ImageMagick setting that I’m missing. Any ideas?
By: reformedmusings on February 16, 2011
at 7:08 pm
Hello! i’m a mac user! does post processing work on mac?
By: Aaron Lee on November 12, 2011
at 7:56 am
Hi Aaron! I don’t know, but it seems reasonable that it might be platform-independent. The only way to know is to try.
By: reformedmusings on November 12, 2011
at 8:13 am