Quotidien Hebdomadaire Mensuel

Quotidien Shaarli

Tous les liens d'un jour sur une page.

June 1, 2023

How to Compress PDF files in Command Line using Ghostscript

Reduce PDF file size in Linux command line

You can use Ghostscript command line tool for compressing a PDF file. Most Linux distributions include the open source version of Ghostscript already. However, you can still try to install it just to make sure.

On Debian/Ubuntu based distributions, use the following command to install Ghostscript:

sudo apt install ghostscript

MacOS with Brew:

brew install ghostscript

Now that you have made sure that Ghostscript is installed, you can use the following command to reduce the size of your PDF file:

gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/prepress -dNOPAUSE -dQUIET -dBATCH -sOutputFile=compressed_PDF_file.pdf input_PDF_file.pdf

In the above command, you should add the correct path of the input and out PDF file.

The command looks scary and confusing. I advise copying and pasting most of it. What you need to know is the dPDFSETTINGS parameter. This is what determines the compression level and thus the quality of your compressed PDF file.

dPDFSETTINGS Description
/prepress (default) Higher quality output (300 dpi) but bigger size
/ebook Medium quality output (150 dpi) with moderate output file size
/screen Lower quality output (72 dpi) but smallest possible output file size