Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

@abdulbadii
Copy link

How to uncompress a PDF file using Ghostscript ?

@mideoye
Copy link

mideoye commented May 23, 2020

Hi Ahmed, thanks for posting this - was really helpful :)

I just wanted to notify you about a little error in the description of the input and output files:

-sOutputFile=out.pdf \ #Output file
file.pdf #Input file

@ahmed-musallam
Copy link
Author

@mideoeye, fixed it! Thank you!

@oliverlambson
Copy link

oliverlambson commented Jan 4, 2021

I removed the original file extension so instead of file.pdf.compressed.pdf you get file.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; 
}

Thanks for the script! Adding it to my .zshrc is going to save loads of wasted googling time every time I need to compress a pdf!

@dheadshot
Copy link

I found I got some corrupted pages unless I used -dCompatibilityLevel=1.4 instead of -dCompatibilityLevel=1.3 and 144 Resolution seemed to scale it up, so I changed that to 64 and it worked perfectly. Settings are probably specific to my scenario though, so YMMV; just be aware these might need adjusting.

Thanks for this!

@rajan-31
Copy link

I am getting error on Windows 10, Powershell 5

gswin64c.exe `
-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="output.pdf" "input.pdf" `
**** Error: stream operator isn't terminated by valid EOL.
               Output may be incorrect.
**** Error: stream operator isn't terminated by valid EOL.
               Output may be incorrect.

but, I can open compressed pdf. Nothing seems wrong or corrupted.

@rootwork
Copy link

Thanks for this! Three notes:

  • The very first line of your script, I think you need a \ after gs.
  • While I didn't get corrupted pages like @dheadshot, I got dramatically smaller file sizes using -dCompatibilityLevel=1.4 -- like, 90% smaller files. PDF 1.4 was released in 2001, so I can't imagine there are that many systems or software that can't deal with it.
  • In situations where the PDF was destined to be printed or I otherwise knew it wouldn't matter, adding -dNOTRANSPARENCY saved a little bit extra as well.

@NightSpirit2099
Copy link

I found I got some corrupted pages unless I used -dCompatibilityLevel=1.4 instead of -dCompatibilityLevel=1.3 and 144 Resolution seemed to scale it up, so I changed that to 64 and it worked perfectly. Settings are probably specific to my scenario though, so YMMV; just be aware these might need adjusting.

Thanks for this!

I just added an argument to the function, so you can define de dpi when you call it

@muzimuzhi
Copy link

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

Link is broken. Try this one instead: https://ghostscript.readthedocs.io/en/latest/Use.html#command-line-options.

@andrewschaeffer
Copy link

Worked like a charm! thanks!

@Herz3h
Copy link

Herz3h commented May 3, 2023

Adding this link here as it is interesting: https://www.ghostscript.com/blog/optimizing-pdfs.html

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