102

I'm looking for a way in Ubuntu to reduce the size of a pdf (by reducing the quality of the images).

I know that this can be done in Ghostscript by typing the following command in terminal:

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

The problem is that I can't specify the quality with any accuracy. The parameter -dPDFSETTINGS=/screen is the one that decides the quality; but the alternatives are quite rigid (for example it is possible to do -dPDFSETTINGS=/ebook for slightly better quality).

I'm looking for a way to reduce the size of a pdf in a way that allows me to specify the desired quality numerically.

MERose
  • 437
  • 11
  • 24
Nicole
  • 1,121

11 Answers11

106

I was able to make a slight variation on your command successful using the -r300 option from @drN The -r option allows you to set the output resolution in the pdf as well as png.

gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/default \
    -dNOPAUSE -dQUIET -dBATCH -dDetectDuplicateImages \
    -dCompressFonts=true -r150 -sOutputFile=output.pdf input.pdf

Edit: Newer versions now prevalaent of gs have a shorter syntax:

gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/default \
    -dQUIET -dDetectDuplicateImages \
    -dCompressFonts=true -r150 -o output.pdf input.pdf

Thanks @pierre-adrien

For high resolution scans downsampling the output can be valuable to use /printer instead of /default for the -dPDFSETTINGS value.

More documentation can be found here: https://web.mit.edu/ghostscript/www/Ps2pdf.htm#Options

Tully
  • 1,931
  • 1
  • 15
  • 11
47

The simplest way I found is to open the source PDF file with LibreOffice Draw and then export to PDF with the expected DPI.

The export dialog window of Draw allows you to specify the DPI and other options for the exported PDF.

Serrega
  • 601
14

These two posts that I had posted on Stackoverflow should help you. I was trying to reduce the size of pdfs whilst ensuring that they met a certain dpi or ppi for my thesis.

Reducing size of pdf with ghostscript

Changing pdf image dpi using gs

Have you tried playing around with convert in Linux?

Edit:

gs \
  -o out300.png \
  -sDEVICE=pngalpha \
  -r300 \
   input.pdf

If I remember correctly, r300 is the output dpi but you might want to check. This converts a pdf to a png, though.

OR

convert -units PixelsPerInch myPic.pdf -density 300 fileout.pdf

Let us know how it goes! This is for a pdf or any other format to any other format. I just used an input file of myPic.pdf and an output file of fileout.pdf

dearN
  • 2,209
12

If reducing the size of the file is your primary objective (and not only reducing size of the figures and also not to specify the quality numerically), how about ps2pdf:

ps2pdf input.pdf output.pdf

Depending on the characteristics of the PDF, this can reduce the file size by an order of magnitude.

dopexxx
  • 233
4
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dQUIET -dBATCH -sOutputFile=myNewFile.pdf myOldFile.pdf

This is the easiest way I think, can reduce the size to 50+/- kb. On your terminal, go to the directory of the file: example

cd ~/document/files

Then type the above code where:

myNewFile.pdf

is the file's new name and

myOldFile.pdf

is the name of the file.

connelblaze
  • 2,139
3

You can use the gs command as suggested by others, but make sure you change dPDFSETTINGS to either screen, for low quality images (75 dpi), or ebook, for 150 dpi.

Here's a full list of the dPDFSETTINGS to choose from (based on this link):

  • -dPDFSETTINGS=/screen — Low quality and small size at 72dpi.

  • -dPDFSETTINGS=/ebook — Slightly better quality but also a larger file size at 150dpi.

  • -dPDFSETTINGS=/prepress — High quality and large size at 300 dpi.

_ -dPDFSETTINGS=/default — System chooses the best output, which can create larger PDF files.

You use a command like the following:

gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook \                                                                                                                                     
    -dNOPAUSE -dQUIET -dBATCH -dDetectDuplicateImages \
    -dCompressFonts=true -sOutputFile=output.pdf input.pdf 
2

From my answer to a similar Unix & Linux Stack Exchange question:

If the PDF file consists entirely of image data;

pdftk inputFile.pdf burst
mogrify -density 300 -format jpg -quality 20 pg_*.pdf
convert *.jpg -auto-orient outputFile.pdf

The density value can match the density of the source image (eg 300 dpi), but the jpeg quality should be lower than the source image (e.g. 20).

0

You can use ocrmypdf --oversample <DPI> flag to reduce the size of the pdf.

For example:

ocrmypdf --skip-text --oversample 150 input.pdf output.pdf

I usually like to do this:

ocrmypdf --skip-text --oversample 150 input.pdf input.pdf

to save time for renaming the pdf file. I chose dpi to be 150 as reference to ghostscript flag -dPDFSETTINGS=/ebook.

0
pdf2ps large.pdf very_large.ps
ps2pdf very_large.ps small.pdf
jasmines
  • 11,311
0

You can also open in gimp as layers, export to pdf again as layers – this turned a 1MB pdf into 275kb for me.

unhammer
  • 2,321
0

I found this script which showed the following gs command:

gs                  \
      -q -dNOPAUSE -dBATCH -dSAFER      \
      -sDEVICE=pdfwrite         \
      -dCompatibilityLevel=1.3      \
      -dPDFSETTINGS=/screen         \
      -dEmbedAllFonts=true          \
      -dSubsetFonts=true            \
      -dAutoRotatePages=/None       \
      -dColorImageDownsampleType=/Bicubic   \
      -dColorImageResolution=$3     \
      -dGrayImageDownsampleType=/Bicubic    \
      -dGrayImageResolution=$3      \
      -dMonoImageDownsampleType=/Subsample  \
      -dMonoImageResolution=$3      \
      -sOutputFile="$2"         \
      "$1"

Replace the $3 in -dColorImageResolution=$3, -dGrayImageResolution=$3 and -dMonoImageResolution=$3 with whatever resolution you want, if you're trying to reduce more than the default minimum (my case) the resolution should be less than 72, you can put different numbers.

Replace the $2 on -sOutputFile="$2" with the output file you want foo.pdf.

Replace the $1 at the end "$1" with the input file you want bar.pdf.

In my case my input file probably had way too many colors, so to propperly compress I needed to reduce the -dColorImageResolution more than the others, so I did -dColorImageResolution=52 and the 2 others I set as 72. Without reducing this variable I couldn't reduce the file value.

I hope this helps anyone in the future :)