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

@alexynior28
Copy link

/ebook is my favorite. 71 MB to 4.9 MB!

@rahimnagori
Copy link

I'm using GS to compress PDF files. Sometimes it works smoothly but sometimes it behaves strangely. I don't know why. Can somebody help me with this?

I'm using the following command to convert a large pdf :

gs \ -q -dNOPAUSE -dBATCH -dSAFER \ -sDEVICE=pdfwrite \ -dCompatibilityLevel=1.3 \ -dPDFSETTINGS=/ebook \ -dEmbedAllFonts=true \ -dSubsetFonts=true \ -dAutoRotatePages=/None \ -dColorImageDownsampleType=/Bicubic \ -dColorImageResolution=72 \ -dGrayImageDownsampleType=/Bicubic \ -dGrayImageResolution=72 \ -dMonoImageDownsampleType=/Subsample \ -dMonoImageResolution=72 \ -sOutputFile=output.pdf \ input.pdf

My input file size is around 12 MB and before completion of the script, I gets a message of server timeout.

  • Does gs not work for large files?
  • Also sometimes output is larger than the input, how can I handle this problem?

@mixmixmix
Copy link

@rahimnagori I have a 17MB file and the command you produced takes 30 sec on a 5year old machine. The problem is that it compresses some images too much, and size drops only to 11MB. The other commands suggested here with ghostscript doesn't change the size at all :/

@ElectricRCAircraftGuy
Copy link

ElectricRCAircraftGuy commented Dec 27, 2020

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

Dead link. :(

Update: try this: https://www.ghostscript.com/doc/current/VectorDevices.htm#PSPDF_IN:

Controls and features specific to PostScript and PDF input

-dPDFSETTINGS=configuration

Presets the "distiller parameters" to one of four predefined settings:

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

The exact settings for each of these are shown in the dozens of options in this table: https://www.ghostscript.com/doc/current/VectorDevices.htm#distillerparams.

@palashkulsh
Copy link

palashkulsh commented Feb 25, 2021

I refer this thread quite frequently and sometimes when ps cann't do the satisfactory job then imagemagick helps

convert -density 200x200 -quality 60 -compress jpeg input.pdf output.pdf

@pateltejasmca
Copy link

My digital signature removed after executing this command. I need that signature in my pdf. What should i do ?
gswin64c.exe -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile="new.pdf" "old.pdf"

@palashkulsh
Copy link

from what i know you'll have to resign new file as file is changed in after running the command.

@Kurt-Mueller
Copy link

Works as expected on a Mac/mojave, gs 9.53.3_1 with pdfs with OCR.
View with preview it is ok, but when printed on an Epson WF-C5790 text ist white on a black background.
Is there an explanation for that and a solution?

@Kurt-Mueller
Copy link

Works as expected on a Mac/mojave, gs 9.53.3_1 with pdfs with OCR.
View with preview it is ok, but when printed on an Epson WF-C5790 text ist white on a black background.
Is there an explanation for that and a solution?

The problem only occurs if printed via Airprint. pdfwrite modifies the pdf in a way, the Airprint driver on the EPSON WF-5790 produces a black background. Printed via the standard macos driver the print is OK.

@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