How to reduce heavy PDFs

Submitted by Nicola Rainiero on 2012-08-13

Sometime people shares PDF documents too big in size, so they are hard to keep and to display, specially in old PCs and eBook readers. Generally these PDFs are formed by images with high resolutions that need reductions. It follows my solution to cut down their size using ImageMagick in two steps.

In Free software to edit PDF article I spoke about the PDF optimization with Ghostscript (a command-line collection of free programs), here it is my favourite command:

gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf

 Where the “-dPDFSETTINGS=” option can assume the following values:
/screen transform input.pdf file in a low resolution file called output.pdf;
/ebook the same but in medium resolution;
/printer for printing output;
/prepress for pre-press output;
/default an output intended to be useful across a wide variety of uses, possibly at the expense of a larger output file.

Generally it almost always works, reducing in size and with a better displaying. Rarely does nothing, specially in PDFs formed by only images and without text (PDFs realized by scanner). In this case I use the ImageMagick suite.


Like mentioned by Wikipedia, ImageMagick is an open source software suite for displaying, converting, and editing raster image files. It can read and write over 100 image file formats. ImageMagick is licensed under the Apache 2.0 license.

I won't enter in details but I am going to explain you only shell commands that I use to reduce PDFs using a little example.


I don't find any PDF useful to do this (I have converted them and then I remove the original from my computer). So I have created one, assuming the worst case:
3 tiff pages (no compression) colored at 600 dpi, in short images of 130 MB each!

I have joined them with:

convert *.tif -density 600 -set units PixelsPerInch PDF_high.pdf

Giving a 490 MB monster PDF file! This PDF is slow in loading and hard to archive (maybe in a 1GB USB pen). I can use Ghostscript (with ebook option) to optimize it, but I obtain a PDF with blank pages and of 375 kB.

Using ImageMagick

I can convert it in jpeg format (but you can use PNG if you want better quality). To do this and to preserve the 600 dpi density:

convert -density 600 -set units PixelsPerInch PDF_high.pdf pag_%02d.jpg

It produces 3 JPG files (1 MB each): pag_00.jpg, pag_01.jpg and pag_02.jpg

To join all files:

convert *.jpg -quality 75 -set units PixelsPerInch PDF_jpg.pdf

Choosing PNG format, you have other options to control quality and "-alpha off" to close down the alpha channel. The final PDF is 1,1 MB in size and is yet slow in loading. You can now use Ghostscript, obtaining a new PDF more fast and in 265 kB.

Mission completed! I can erase the jpegs with this command:

rm *.jpg

This time I can't upload the example files, because I haven't enough free space in my domain to do this!





Related Content:

Nicola Rainiero

A civil geotechnical engineer with the ambition to facilitate own work with free software for a knowledge and collective sharing. Also, I deal with green energy and in particular shallow geothermal energy. I have always been involved in web design and 3D modelling.