Skip to content

Instantly share code, notes, and snippets.

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

@Johnwulp

This comment has been minimized.

Copy link

@Johnwulp Johnwulp commented Aug 21, 2017

Not always working. See command below:

-q -dNOPAUSE -dBATCH -dSAFER -dPDFA=2 -dPDFACompatibilityPolicy=1 -dSimulateOverprint=true -sDEVICE=pdfwrite -dCompatibilityLevel=1.3 -dPDFSETTINGS=/screen -dEmbedAllFonts=true -dSubsetFonts=true -dAutoRotatePages=/None -dColorImageDownsampleType=/Bicubic -dColorImageResolution=150 -dGrayImageDownsampleType=/Bicubic -dGrayImageResolution=150 -dMonoImageDownsampleType=/Bicubic -dMonoImageResolution=150

@AneudyM

This comment has been minimized.

Copy link

@AneudyM AneudyM commented Dec 10, 2017

Also, remember that dCompatibilityLevel will be overridden by what you choose in dPDFSETTINGS (i.e.: Level 1.3 or 1.4). If you want to choose a specific level it must be done by declaring dCompatibilityLevel AFTER the declaration of dPDFSETTINGS, otherwise, it is unnecessary.

@michaeljohnston0

This comment has been minimized.

Copy link

@michaeljohnston0 michaeljohnston0 commented Dec 20, 2017

This is great. Any thoughts on compression affecting search functionality? After compressing everything looks great to the eye, but if I do Ctrl+F or similar search for text in Mac preview, the matches are very spotty. Other than that, worked like a charm.

@cschwaderer

This comment has been minimized.

Copy link

@cschwaderer cschwaderer commented Feb 26, 2018

In my test case, this command destroyed digital signatures in a PDF.

@dusek

This comment has been minimized.

Copy link

@dusek dusek commented Mar 18, 2018

In my test case, this command destroyed digital signatures in a PDF.

@cschwaderer Any processing (modification) will always invalidate digital signatures, that is a principle of digital signatures. I.e. even if they were left in the document (and not removed by ghostscript), the signatures would no longer match with the changed document. That is correct, since the signer did not sign the changed (processed) document, only the original one.

@readyready15728

This comment has been minimized.

Copy link

@readyready15728 readyready15728 commented Apr 18, 2018

I can confirm this was very useful for me. I also have a means of EPUB recompression detailed here:

https://github.com/readyready15728/misc/blob/master/epub-recompression.md

@krishjun

This comment has been minimized.

Copy link

@krishjun krishjun commented Jul 26, 2018

thanks '/ebook ' works perfect , big difference in size and marginal difference in quality.

@caminoezequiel

This comment has been minimized.

Copy link

@caminoezequiel caminoezequiel commented Aug 12, 2018

Thanks it worked very good for me. I am using to index files with ElasticSearch so I also added options to convert to grayscale with: -dColorConversionStrategy=/Gray -dProcessColorModel=/DeviceGray

The original file had 9 mb, next 4.3mb and finally with grayscale 3.0 mb

@csantosburguete

This comment has been minimized.

Copy link

@csantosburguete csantosburguete commented Oct 14, 2018

I used current ghostscript version 9.25 (2018-09-13) to reduce the size of a PDF that contains hyperlinks to several URLs, as well as hyperlinks to several points inside the PDF. Ghostscript reduces the file size substantially, BUT destroys all the hyperlinks. Any ideas, any hints, please?

@xMrWhite

This comment has been minimized.

Copy link

@xMrWhite xMrWhite commented Dec 6, 2018

@csantosburguete you could try -dPDFSETTINGS=/ebook to preserve links, but this has a lower quality for images and text, you might want to tune compression with settings after that option

@Skynet-T1000

This comment has been minimized.

Copy link

@Skynet-T1000 Skynet-T1000 commented Jan 2, 2019

For me some reason it didn't work . Simply Go to LibreOffice open the pdf file in Draw. select "Export to PDF" and reduce the DPI to 150 and quality to 50% or so you're done.
screen shot 2019-01-02 at 5 10 15 pm

@catleeball

This comment has been minimized.

Copy link

@catleeball catleeball commented Jul 8, 2019

Note that by default Ghostscript removes hyperlinks from PDFs. To preserve links, include the flag -dPrinted=false.

@filipezabala

This comment has been minimized.

Copy link

@filipezabala filipezabala commented Apr 10, 2020

find . -name '*.pdf' | while read pdf; do gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -sOutputFile="${pdf}_new.pdf" "$pdf"; done

@wwone

This comment has been minimized.

Copy link

@wwone wwone commented May 14, 2020

The use of LibreOffice had not occurred to me. MANY THANKS for pointing in that direction. I just reduced a 140MB monster to 6 MB. This was a newsletter with many illustrations. The final result is quite useable on the screen, and will be much easier for the online users to download. The original version (140MB), of course, was sent to the printer for the mailed newsletter.

Getting parameters correct for GhostScript is very frustrating. Glad to see that LibreOffice folks have connected with a good set of tools.

@alexynior28

This comment has been minimized.

Copy link

@alexynior28 alexynior28 commented May 24, 2020

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

@rahimnagori

This comment has been minimized.

Copy link

@rahimnagori rahimnagori commented Sep 25, 2020

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

This comment has been minimized.

Copy link

@mixmixmix mixmixmix commented Sep 29, 2020

@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

This comment has been minimized.

Copy link

@ElectricRCAircraftGuy 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

This comment has been minimized.

Copy link

@palashkulsh 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

This comment has been minimized.

Copy link

@pateltejasmca pateltejasmca commented Mar 25, 2021

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

This comment has been minimized.

Copy link

@palashkulsh palashkulsh commented Mar 25, 2021

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

@Kurt-Mueller

This comment has been minimized.

Copy link

@Kurt-Mueller Kurt-Mueller commented Apr 7, 2021

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

This comment has been minimized.

Copy link

@Kurt-Mueller Kurt-Mueller commented Apr 8, 2021

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.

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