Compress bitmap images within PDF files

Updated . Posted . Visible to the public.

Embedding bitmap images within PDF sometimes results in large files because the bitmaps are not compressed. If you don't need high quality images within the resulting PDF file, you can use ghostscript to compress embedded images:

ghostscript -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -sOutputFile=new-smaller-file.pdf large-original-file.pdf

Note that your PDF printer (or similiar generation tools) also often come with a compression setting for embedded raster images.

You can put this snippet into your ~/.bash_aliases:

alias pdf_compress='_compress(){ ghostscript -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -sOutputFile="$1".c.pdf "$1";}; _compress'

Reload aliases (source ~/bash_aliases) and you can simply use pdf_compress example.pdf on your commandline.

Thomas Eisenbarth
Last edit
Thomas Eisenbarth
License
Source code in this card is licensed under the MIT License.
Posted by Thomas Eisenbarth to makandra dev (2012-05-28 13:57)