Skip to content

Instantly share code, notes, and snippets.

@arnaudbos
Forked from ahmed-musallam/compress_pdf.md
Created February 11, 2022 10:05
Show Gist options
  • Save arnaudbos/3adb50aef9c64bd916a3c84e81a2eef4 to your computer and use it in GitHub Desktop.
Save arnaudbos/3adb50aef9c64bd916a3c84e81a2eef4 to your computer and use it in GitHub Desktop.
How to compress PDF with ghostscript

How to compress PDF using ghostscript

As a developer, it bothers me when someone sends me a large pdf file compared to the number of pages. Recently, I recieved a 12MB scanned document for just one letter-sized page... so I got to googlin, like I usually do, and found ghostscript!

to learn more abot ghostscript (gs): https://www.ghostscript.com/

What we are interested in, is the gs command line tool, which provides many options for manipulating PDF, but we are interested in compressign those large PDF's into small yet legible documents.

credit goes to this answer on askubuntu forum: https://askubuntu.com/questions/3382/reduce-filesize-of-a-scanned-pdf/3387#3387?newreg=bceddef8bc334e5b88bbfd17a6e7c4f9

Steps below were only tried on macOs sierra

you can install gs via the official site or via homebrew

brew install ghostscript

now to compress a pdf:

gs 
 -q -dNOPAUSE -dBATCH -dSAFER \
 -sDEVICE=pdfwrite \
 -dCompatibilityLevel=1.3 \
 -dPDFSETTINGS=/screen \
 -dEmbedAllFonts=true -dSubsetFonts=true \
 -dColorImageDownsampleType=/Bicubic \
 -dColorImageResolution=144 \                `#PDF downsample color image resolution`
 -dGrayImageDownsampleType=/Bicubic \
 -dGrayImageResolution=144 \                 `#PDF downsample gray image resolution`
 -dMonoImageDownsampleType=/Bicubic \
 -dMonoImageResolution=144 \                 `#PDF downsample mono image resolution`
 -sOutputFile=out.pdf \                      `#Output file`
 file.pdf                                    `#Input file`

you can find documentation on ghostcript commands here: https://www.ghostscript.com/doc/current/Use.htm#Options

you'll notice that I set all the ImageResolution options to 144, I found that this value gives the best results for legible text scans, you can change that to whatever you like

I also added a function to my .bash_profile to make a shorthand that will compress and rename file.pdf to file.pdf.compressed.pdf:

pdfcompress ()
{
   gs -q -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pdfwrite -dCompatibilityLevel=1.3 -dPDFSETTINGS=/screen -dEmbedAllFonts=true -dSubsetFonts=true -dColorImageDownsampleType=/Bicubic -dColorImageResolution=144 -dGrayImageDownsampleType=/Bicubic -dGrayImageResolution=144 -dMonoImageDownsampleType=/Bicubic -dMonoImageResolution=144 -sOutputFile=$1.compressed.pdf $1; 
}

use it: pdfcompress somefile.pdf

@arnaudbos
Copy link
Author

Command to merge pdf files used recently, keeping for future reference maybe?

gs \
-q -dNOPAUSE -dBATCH -dSAFER \
-sDEVICE=pdfwrite \
-dCompatibilityLevel=1.3 \
-dPDFSETTINGS=/screen \
-dEmbedAllFonts=true -dSubsetFonts=true \
-dColorImageDownsampleType=/Bicubic \
-dColorImageResolution=144 \
-dGrayImageDownsampleType=/Bicubic \
-dGrayImageResolution=144 \
-dMonoImageDownsampleType=/Bicubic \
-dMonoImageResolution=144 \
-sOutputFile="path/to/output.pdf" "some file as header.pdf" "other files/*" anotherone.pdf ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment