Compressing PDFs in Bash

I have scripts for hplip to scan PDF’s to folders using the ADF. These PDFs are very large. To convert these to smaller file-sizes for emailing, I added the following functions to .bashrc.

mediumpdf() { read -p "Enter input PDF filename: " name \
&& gs -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 \
-dPDFSETTINGS=/printer -sOutputFile=medium-quality-$name $name; }
smallpdf() { read -p "Enter input PDF filename: " name \
&& gs -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 \
-dPDFSETTINGS=/ebook -sOutputFile=low-quality-$name $name; }
tinypdf() { read -p "Enter input PDF filename: " name \
&& gs -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 \
-dPDFSETTINGS=/screen -sOutputFile=lowest-quality-$name $name; }
bestpdf() { read -p "Enter input PDF filename: " name \
&& gs -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 \
-dPDFSETTINGS=/prepress -sOutputFile=best-quality-$name $name; }

Using a 32.9MB test PDF straight from HPLIP, bestpdf created a 10.5MB with very good quality that was almost indistinguishable from the original.  Mediumpdf created a 7.8MB that looked good enough for faxing with some color distortion.  Smallpdf created a 1.5MB that might be good enough for faxing.  Tinypdf created a 482kB with clear color distortions.

Leave a Reply