Skip to content

Instantly share code, notes, and snippets.

@guifromrio
Created August 30, 2013 14:39
Show Gist options
  • Save guifromrio/6390547 to your computer and use it in GitHub Desktop.
Save guifromrio/6390547 to your computer and use it in GitHub Desktop.
Compress PDF files with ghostscript

This can reduce files to ~15% of their size (2.3M to 345K, in one case) with no obvious degradation of quality.

ghostscript -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/printer -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf

Other options for PDFSETTINGS:

  • /screen selects low-resolution output similar to the Acrobat Distiller "Screen Optimized" setting.
  • /ebook selects medium-resolution output similar to the Acrobat Distiller "eBook" setting.
  • /printer selects output similar to the Acrobat Distiller "Print Optimized" setting.
  • /prepress selects output similar to Acrobat Distiller "Prepress Optimized" setting.
  • /default selects output intended to be useful across a wide variety of uses, possibly at the expense of a larger output file.

Source: http://ghostscript.com/doc/current/Ps2pdf.htm

@ifthakharriyad
Copy link

ifthakharriyad commented Jun 20, 2021

@firstdoit How can I compress a pdf in a different directory using this command? /books/book.pdf instead of input.pdf Doesn't compress the book.pdf.
I am using ghostscript4js in my node app to compress pdf using the commands but the pdf needs to be at the same directory as the server.js file to be able to compress it. Since I want to keep my pdfs in a separate directory (i.e. /books) I wonder how can I do that?
And also can I output into a different directory?

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