Skip to main content

PDF Compression with Ghostscript

PDF do preserve the original information especially image when exported. The feature makes it very attractive for archive but it might be too large to spread.

We can use ghostscript command to compress (actually rewrite) a PDF file with a lot of images embedded.

Step 1: Generate PostScript file corresponding to the PDF file

pdf2ps -dLanguageLevel=3 input.pdf output.ps

Step 2: Re-write the PDF file with images resized to a specific DPI

Note: PDFSETTINGS specific parameters is shown in the documentation

https://ghostscript.com/doc/9.26/VectorDevices.htm#distillerparams

We may just specify the PDFSETTINS to the following for a quick test:

  • /default
  • /prepress: 300dpi
  • /printer: 300dpi
  • /ebook: 150dpi
  • /screen: 72dpi
ps2pdf -dPDFSETTINGS=/printer -sOutputFile=output.pdf output.ps

Step 3: Tweaking the advanced parameters

Sometimes we need to tweak and override the default settings of a specific pdf setting, we can just override it with command-line parameters:

export DPI=500
ps2pdf                                \
  -dPDFSETTINGS=/printer              \
  -dCompatibilityLevel=1.5            \
  -dAutoRotatePages=/None             \
  -dDownsampleColorImages=true        \
  -dColorImageResolution=${DPI}       \
  -dColorImageDownsampleType=/Bicubic \
  -dDownsampleGrayImages=true         \
  -dGrayImageResolution=${DPI}        \
  -dGrayImageDownsampleType=/Bicubic  \
  -dDownsampleMonoImages=true         \
  -dMonoImageResolution=${DPI}        \
  -dMonoImageDownsampleType=/Bicubic  \
  -dNOPAUSE -dQUIET -dBATCH           \
  -sOutputFile=output.pdf output.ps

 

We may also use a combined command for convenience without generating the PostScript file:

ghostscript -sDEVICE=pdfwrite -dPDFSETTINGS=/printer -sOutputFile=output.pdf input.pdf

 

Footnotes:

The 9.26 ghostscript pdf settings table:

 

Comments

Comments powered by Disqus