34

Need a Java-based solution or, at the worst, command-line for Linux.

I tried to use Ghostscript:

gs -sDEVICE=pdfwrite -dPDFA -dBATCH -dNOPAUSE -dUseCIEColor \
   -sProcessColorModel=DeviceCMYK -sPDFACompatibilityPolicy=1 \
   -sOutputFile=downgraded.pdf leon_range_my12_w22_brochure.pdf

but I got a lot of errors...

Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
macromaniac
  • 407
  • 1
  • 4
  • 11
  • 3
    Those parameters won't downsample images anyway. What version of GS are you using, and have reported the errors as a bug ? The -dPDFA switch needs extra information to create a conforming PDF/A file which you haven't supplied. Possibly the 'errors' are actually warnings.... – KenS Feb 29 '12 at 13:57

5 Answers5

57

Here's an example of how you can downsample all (color, gray and mono) images to 72dpi with a Ghostscript commandline:

gs \
  -o downsampled.pdf \
  -sDEVICE=pdfwrite \
  -dDownsampleColorImages=true \
  -dDownsampleGrayImages=true \
  -dDownsampleMonoImages=true \
  -dColorImageResolution=72 \
  -dGrayImageResolution=72 \
  -dMonoImageResolution=72 \
  -dColorImageDownsampleThreshold=1.0 \
  -dGrayImageDownsampleThreshold=1.0 \
  -dMonoImageDownsampleThreshold=1.0 \
   input.pdf

Update:
The *ImageDownsampleThreshold=1.0 parameters enforce that all Images with a resolution higher than the target resolution of 72 dpi will be downsampled. If this parameter is not given (or set to a different value), the default values will be used: *ImageDownsampleThreshold=1.5. This default value will only downsample images with a value of 108 dpi (or higher) and leave the other ones untouched.

Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
  • @codin: Comments are not for discussing an entirely new topic. Please ask a new question, tag it as `[ghostscript]` + `[pdf]` and I'll try to answer it as best as I can. Please also state what's the purpose of your request. Saving on filesize? Remove info contained in the images? Or? – Kurt Pfeifle Dec 19 '13 at 12:23
  • Thank you Kurt, great answer! I have tried to reduce the image resolution to 36, but the size of the output file did not change, even though 70% of its size was given from an image in the first page. Any idea of why? – coccoinomane Oct 18 '16 at 14:33
  • 1
    @GuidoWalterPettinari: Without having access to the PDF itself I won't speculate. Maybe you can apply the following answer to your file in order to narrow down the elements which occupy the bulk of its bytes: http://stackoverflow.com/a/37858893/359307 – Kurt Pfeifle Oct 18 '16 at 14:43
  • Thanks Kurt, that's a very useful resource! I didn't know that Ghostscript had a simple command to strip images from PDF. I will look into that. – coccoinomane Oct 28 '16 at 16:09
  • 1
    @GuidoWalterPettinari: The official way to say *"Thanks"* on StackOverflow is to upvote useful answers :-) – Kurt Pfeifle Oct 28 '16 at 21:26
17

This is what I am using:

gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -sOutputFile=OUTPUT.pdf INPUT.pdf

For your reference:

-dPDFSETTINGS=/screen (screen-view-only quality, 72 dpi images)

-dPDFSETTINGS=/ebook (low quality, 150 dpi images)

-dPDFSETTINGS=/printer (high quality, 300 dpi images)

-dPDFSETTINGS=/prepress (high quality, color preserving, 300 dpi imgs)

-dPDFSETTINGS=/default (almost identical to /screen)

Roger
  • 8,286
  • 17
  • 59
  • 77
3

Try moonshiner, a GUI to ghostscript.

Mark Ursino
  • 31,209
  • 11
  • 51
  • 83
gvdmoort
  • 31
  • 1
  • while moonshiner indeed looked promising during its 5 months of development activity on Sourceforge in 2009, nowadays it looks like it's a dead project. No updates since May of 2009.... :-( – Kurt Pfeifle Mar 05 '12 at 18:08
  • 2
    @pipitas I haven't made any big changes to moonshiner mainly because as far as I'm concerned, it's pretty much done. I agree that the lack of activity makes it look dead, but I still use it extensively to this day. I just haven't had anything to add anymore :) – balpha Jun 03 '12 at 07:44
  • 1
    @balpha: have you looked into the extensive support for ICC profile embedding and OutputIntents handling Ghostscript has acquired in the meanwhile? – Kurt Pfeifle Jun 03 '12 at 09:52
  • @balpha: You could mention this somewhere. –  May 29 '13 at 23:09
2

For a scanned document in which each page is a full color image, I used a combination of the command line above and another that I found on a different site,

gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dDownsampleColorImages=true \
-dColorImageResolution=150 -dNOPAUSE  -dBATCH -sOutputFile=output.pdf input.pdf

Each of my pages was a color scan of a document. This command line reduced the resolution of the pages to 150dpi, cutting the file size in half without significant loss of resolution. It's still looks good and the text is comfortably readable on my Nexus 7.

mlitty
  • 21
  • 1
  • is this command you are using at all? please post entire command if you have merged other combinations posted above. I have issue with color images pdf which isn't reducing properly. Thanks – Aqib Mumtaz Aug 20 '15 at 08:46
0

I tested today, that for some reason Ghostscript (9.54.0) -dPDFSETTINGS=/screen will automatically increase the filesize, if the image resolution is set to 70 dpi or higher. If the dpi value is set just below 70, it will operate really well and create practically screen quality document in significantly smaller filesize, keeping all vector based fonts intact. Here is my command line that really squeezes the air out of the massive PDF:s.

gswin64.exe -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dBATCH -dQUIET -dDownsampleColorImages=true -dColorImageResolution=69 -dColorImageDownsampleType=/Bicubic -dAutoFilterColorImages=false -sOutputFile=YourOutputFile YourInputFile.pdf
Supernuija
  • 19
  • 2