52

I have a pdf file that contains images and I want to reduce its size in order to upload it to a site with a size limit.

So, how can I reduce the size of a pdf file from the command-line?

terdon
  • 234,489
  • 66
  • 447
  • 667
Pandya
  • 23,898
  • 29
  • 92
  • 144
  • by "images" probably you mean "raster images" but PDF files can also contain vector images, which [can be really large](https://community.adobe.com/t5/acrobat-discussions/pdf-size-is-content-stream-the-problem/m-p/2286382), and which are harder to compress, see also [Replacing vector images in a PDF with raster images](https://tex.stackexchange.com/questions/47076/replacing-vector-images-in-a-pdf-with-raster-images) – milahu Aug 21 '23 at 17:39

4 Answers4

63

You can use gs - GhostScript (PostScript and PDF language interpreter and previewer) as follows:

  • Set pdfwrite as output device by -sDEVICE=pdfwrite
  • Use the appropriate -dPDFSETTINGS.

From Documentation:

-dPDFSETTINGS=configuration
Presets the "distiller parameters" to one of four predefined settings:

  • /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.

Example:

$ du -h file.pdf 
27M file.pdf
$ gs -sDEVICE=pdfwrite -dPDFSETTINGS=/ebook -q -o output.pdf file.pdf
$ du -h output.pdf 
900K    output.pdf

Here -q suppress normal startup messages, and also do the equivalent of -dQUIET which suppresses routine information comments

Pandya
  • 23,898
  • 29
  • 92
  • 144
12
ps2pdf input.pdf output.pdf

I got the answer from ask ubuntu and it worked for me. It actually reduced 18.1Mb to 1.0Mb

Freeman
  • 221
  • 2
  • 4
  • 1
    Doesn't work for me. A searchable PDF output by `tesseract` remains completely unchanged when attempting to shrink it with this command. – Gabriel Staples Dec 30 '19 at 17:46
  • This is awesomely simple if you don't care to choose all the options that GhostScript needs. The man pages say that the script accepts the same parameters as `gs`, it gives this example: `ps2pdf -dPDFSETTINGS=/prepress figure.ps proof.pdf` – Cris Luengo Apr 28 '20 at 23:11
2

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).

user2585501
  • 121
  • 2
1

You can try this :

$ time pdftk myFile.pdf output myFile__SMALLER.pdf compress
GC Warning: Repeated allocation of very large block (appr. size 16764928):
    May lead to memory leak and poor performance.
GC Warning: Repeated allocation of very large block (appr. size 8384512):
    May lead to memory leak and poor performance.
GC Warning: Repeated allocation of very large block (appr. size 11837440):
    May lead to memory leak and poor performance.
GC Warning: Repeated allocation of very large block (appr. size 8384512):
    May lead to memory leak and poor performance.
GC Warning: Repeated allocation of very large block (appr. size 33525760):
    May lead to memory leak and poor performance.
GC Warning: Repeated allocation of very large block (appr. size 7254016):
    May lead to memory leak and poor performance.
GC Warning: Repeated allocation of very large block (appr. size 34041856):
    May lead to memory leak and poor performance.
GC Warning: Repeated allocation of very large block (appr. size 33525760):
    May lead to memory leak and poor performance.

real    0m23.677s
user    0m23.142s
sys     0m0.540s
$ du myFile*.pdf
108M    myFile.pdf
74M     myFile__SMALLER.pdf

It is faster than gs but compresses upto 30% in this case for a 107.5MiB input file.

SebMa
  • 1,941
  • 4
  • 22
  • 37
  • 3
    Doesn't work for me. A searchable PDF output by `tesseract` remains completely unchanged when attempting to shrink it with this command. – Gabriel Staples Dec 30 '19 at 17:47
  • This only restores the regular PDF page stream compression if the file happens to be in uncompressed (textual) format. Won't do much usually. – Michel de Ruiter Jan 23 '22 at 19:27