Posted by: reformedmusings | February 12, 2011

Reducing PDF file sizes in Linux

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:


FILENAME=`basename $1`
DIRNAME=`dirname $1`

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.



  1. 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

    • 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?

  2. Hello! i’m a mac user! does post processing work on mac?

    • Hi Aaron! I don’t know, but it seems reasonable that it might be platform-independent. The only way to know is to try.

  3. […] te sam se dao u potragu za načinom da ga smanjim.  Na netu sam našao blogpost koji je naveo skriptu koja mi je u potpunosti odgovarala. Malo sam ju modifikovao, tako da sam kao konačnu […]

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s


%d bloggers like this: