Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

How to compress a scanned PDF

📅 2014-Jun-12 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ compress, imagemagick, pdf ⬩ 📚 Archive

Scanned documents are typically stored in the PDF format. Depending on the scanning options used, these scanned documents can be large and you might need to shrink their size to fit certain size requirements.

Such compression can be achieved using ImageMagick tools by playing with the pixel density of the images, the image format used (JPEG) and the JPEG quality options. ImageMagick uses Ghostscript to actually do this conversion, invoking it with a long list of options to achieve what we requested.

$ sudo apt install ghostscript imagemagick
$ convert -density 200x200 -quality 80 -compress jpeg original.pdf out.pdf

I typically play with the -density values until the PDF fits the size I need. I only play with the -quality value if it really affects the scan quality, increasing it to 90 if necessary.

$ convert -density 200x200 -quality 80 -compress jpeg -verbose original.pdf out.pdf

Note: Another way to shrink PDF is to convert to PS (pdf2ps) and then back to PDF (ps2pdf). Note that this does double the work and creates a ginormous intermediate PS file. I would not recommend this since it takes longer time, creates huge intermediate files and you get no control over the DPI or compression ratio.

Tried with: Ubuntu 20.04

© 2022 Ashwin Nanjappa • All writing under CC BY-SA license • 🐘📧