118

I use pdftk to compress a pdf using the following command line

pdftk file1.pdf output file2.pdf compress

It works as the weight of my file decreased.

Are there [options] to change the compression???

Or maybe other solutions to compress my file? It is heavy because some graphics have a lot of points. Is there a way to convert these graphs to jpg for instance and adapt the compression?

RockScience
  • 17,932
  • 26
  • 89
  • 125
  • 2
    From my experience, it depends what is inside your pdf. If it is a graph with many dots for instance, the best solution is to convert the graph to png and include this png into the pdf. – RockScience Jan 25 '12 at 04:44

13 Answers13

142

I had the same problem and found two different solutions (see this thread for more details). Both reduced the size of my uncompressed PDF dramatically.

  • Pixelated (lossy):

    convert input.pdf -compress Zip output.pdf
    
  • Unpixelated (lossless, but may display slightly differently):

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

Edit: I just discovered another option (for lossless compression), which avoids the nasty gs command. qpdf is a neat tool that converts PDFs (compression/decompression, encryption/decryption), and is much faster than the gs command:

qpdf --linearize input.pdf output.pdf
Kristian
  • 1,099
  • 1
  • 11
  • 24
nullglob
  • 6,903
  • 1
  • 29
  • 31
  • 3
    Awesome. gs worked for me, converting a 4MB file to 339K. There was a loss of quality, but it served my purpose sufficiently. – Sridhar Sarnobat Oct 17 '12 at 19:10
  • 37
    You can use "printer" PDF setting for a better quality: `gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.5 -dPDFSETTINGS=/printer -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf` – imriss Jun 25 '13 at 18:52
  • 11
    To adjust quality (and therefore size), vary PDFSETTINGS value. See http://ghostscript.com/doc/current/Ps2pdf.htm#Options – 1in9ui5t Oct 03 '13 at 01:32
  • 2
    qpdf didn't change anything for me, the zip compress looks horrible, but the gs with printer setting works well – Sebastian Blask Jul 09 '14 at 21:15
  • 7
    Note that the `gs` command in the answer is not exactly lossless, since it lowers the resolution and quality of embedded JPGs. But it is lossless re. text, keeping it as text, while the `convert` command converts it to raster graphics. – tanius May 09 '15 at 17:10
  • With graphicsmagick I had to `gm convert`, not just `convert`. – CMCDragonkai Nov 15 '15 at 15:18
  • 19
    Setting option `-dPDFSETTINGS=` to `/ebook` gives a very nice output for me: sure, it's compressed and some jpg artifacts are visible, but it's totally readable for a reasonable size. Thanks! – Joël Dec 22 '15 at 03:43
  • `gs` wirked marvelously well, however the image magick `convert in.pdf -compress zip out.pdf` increased the size of my file quite dramatically. – asoundmove Feb 06 '17 at 00:06
  • 1
    the link to the thread seems to be dead, but it's available in the [wayback machine](https://web.archive.org/web/20150510223810/https://ubuntuforums.org/archive/index.php/t-1133357.html). – myrdd Nov 17 '18 at 22:50
  • 1
    `convert` will turn your txt pdf into a bunch of crappy images. this solution is waaaay destructive :-) – CeDeROM Apr 29 '21 at 21:42
  • The gs command does not transform the PDF Ojects but only the pictures. I explain me. In my PDF created with PDKTK I have some fields. So I use the flatten option (in PDFTK) to convert this fieds into a simple text. So that it can no longer be edited. But this simples texts seems not to be transformed with gs command. And so the compression of the file is not complete. And so the solution would be to convert PDF to a format that could be processed completely by the gs command But what format...? – Juan Jul 22 '22 at 10:59
  • 1
    The link posted by @1in9ui5t to the list of PDFSETTINGS options seems to be dead. This one is working now: https://ghostscript.readthedocs.io/en/latest/VectorDevices.html#controls-and-features-specific-to-postscript-and-pdf-input – Pablo Arnalte-Mur Feb 02 '23 at 17:13
45

Trying to compress a PDF I made with 400ppi tiffs, mostly 8-bit, a few 24-bit, with PackBits compression, using tiff2pdf compressed with Zip/Deflate. One problem I had with every one of these methods: none of the above methods preserved the bookmarks TOC that I painstakingly manually created in Acrobat Pro X. Not even the recommended ebook setting for gs. Sure, I could just open a copy of the original with the TOC intact and do a Replace pages but unfortunately, none of these methods did a satisfactory job to begin with. Either they reduced the size so much that the quality was unacceptably pixellated, or they didn't reduce the size at all and in one case actually increased it despite quality loss.

pdftk compress:

no change in size
bookmarks TOC are gone

gs screen:

takes a ridiculously long time and 100% CPU
errors:
    sfopen: gs_parse_file_name failed.                                 ? 
    | ./base/gsicc_manage.c:1651: gsicc_set_device_profile(): cannot find device profile
74.8MB-->10.2MB hideously pixellated
bookmarks TOC are gone

gs printer:

takes a ridiculously long time and 100% CPU
no errors
74.8MB-->66.1MB
light blue background on pages 1-4
bookmarks TOC are gone

gs ebook:

errors:
    sfopen: gs_parse_file_name failed.
      ./base/gsicc_manage.c:1050: gsicc_open_search(): Could not find default_rgb.ic 
    | ./base/gsicc_manage.c:1651: gsicc_set_device_profile(): cannot find device profile
74.8MB-->32.2MB
badly pixellated
bookmarks TOC are gone

qpdf --linearize:

very fast, a few seconds
no size change
bookmarks TOC are gone

pdf2ps:

took very long time
output_pdf2ps.ps 74.8MB-->331.6MB

ps2pdf:

pretty fast
74.8MB-->79MB
very slightly degraded with sl. bluish background
bookmarks TOC are gone
hmj6jmh
  • 630
  • 5
  • 13
39

this procedure works pretty well

pdf2ps large.pdf very_large.ps

ps2pdf very_large.ps small.pdf

give it a try.

jortizromo
  • 806
  • 9
  • 20
  • 11
    This is not a general solution. In many cases, the resulting pdf is larger. – rotskoff Dec 16 '12 at 04:00
  • 5
    This worked the best out of all mentioned solutions for me. A few large images went down from 23MB to 1.4MB with by far the least quality loss. – AerandiR Feb 26 '13 at 05:19
  • 1
    @rotskoff There probably is no general solution because there are different types of documents. However I see your point. It would be nice to have software figuring what works best for us. – tiktak Dec 18 '13 at 15:23
  • Thanks, this worked for me, while qpdf and gs did not reduce the size of the output file. – sebastian Jun 05 '14 at 07:33
  • 1
    As mentioned [here](http://askubuntu.com/a/243753) another drawback to this method is that it will break URL links inside the document. – ptomato Sep 29 '14 at 19:15
  • 1
    I tried several solutions mentioned on this page and this one worked the best with the best quality. Went from 22.1MB PDF to 220.2MB PS and in the end got a 3.8MB PDF with little perceived quality loss. – Brandon May 10 '16 at 14:58
  • 1
    31,6 MB vs. 8,8 MB. this has won for me. – desperateCoder Sep 20 '16 at 20:33
  • I suspect that using just `ps2pdf large.pdf small.pdf` has a chance to give the same result. (This are not misprints: `ps2pdf` accepts `pdf` input as well.) – Ilya Zakharevich Apr 06 '21 at 03:01
  • did return exactly the same file size :-( – CeDeROM Apr 29 '21 at 21:46
33

If file size is still too large it could help using ps2pdf to downscale the resolution of the produced pdf file:

pdf2ps input.pdf tmp.ps
ps2pdf -dPDFSETTINGS=/screen -dDownsampleColorImages=true -dColorImageResolution=200 -dColorImageDownsampleType=/Bicubic tmp.ps output.pdf

Adjust the value of the -dColorImageResolution option to achieve a result that fits your needs (the value describes the image resolution in DPIs). If your input file is in grayscale, replacing Color through Gray or using both options in the above command could also help. Further fine-tuning is possible by changing the -dPDFSETTINGS option to /default or /printer. For explanations of the all possible options consult the ps2pdf manual.

Dominik
  • 331
  • 3
  • 2
  • 1
    Thanks for tip. With -dPDFSETTINGS I could reduce the size of my scanned PDF – eshwar Apr 03 '20 at 15:04
  • 2
    THANK YOU. I don't think there is a general solution for everyone's use case - but I tried almost every solution on this thread and this is the only one that worked for me!!! Being able to "tune" the dColorImageResolution parameter was key - had to get the doc size small enough for this government site to accept it but big enough to be legible. Thanks, uncle Sam, for yet another painful hoop to jump through :) – alliedtoasters Apr 07 '20 at 01:10
  • TANK U! This reduced 10x size with no visible image loss :-) ` gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dBATCH -dQUIET -dDownsampleColorImages=true -dColorImageResolution=200 -dColorImageDownsampleType=/Bicubic -sOutputFile=output.pdf input.pdf` – CeDeROM Apr 29 '21 at 21:52
7

The one-line pdf2ps option (by Lee) actually increased the pdf size. However, the two steps one did better. And it can be combined in a single one using redirection from & to standard input/output and pipes:

pdf2ps large.pdf - | ps2pdf - small.pdf

did reduce a PDF generated by xsane from 18 Mo to 630 ko!

Links are lost, but for the present example, it's not a concern... and was the easiest way to achieve the desired result.

E. Curis
  • 71
  • 1
  • 3
  • 2
    You could try `ps2pdf` instead, see [my comment](https://stackoverflow.com/questions/5296667/pdftk-compression-option/50237222?noredirect=1#comment93590084_50237222) to @Lee's answer. – myrdd Nov 18 '18 at 10:46
5

pdf2ps large.pdf small.pdf is enough, instead of two steps

pdf2ps large.pdf very_large.ps 
ps2pdf very_large.ps small.pdf

However, ps2pdf large.pdf small.pdf is a better choice.

  • ps2pdf is much faster
  • without additional parameters specified, pdf2ps sometimes produces larger file.
wsdzbm
  • 3,096
  • 3
  • 25
  • 28
  • Where did you find this option? Is it a feature in some recent version? It did not work for me. Even though I named the output file `out.pdf`, it became a PS file (`mimetype out.pdf` says `out.pdf: application/postscript`). – myrdd Nov 14 '18 at 09:21
  • mine is the most recent version 9.xx. not sure your. – wsdzbm Nov 15 '18 at 14:31
  • I'm using the debian stable ("stretch") packaged version, which is 9.25. Could you check if you indeed have a pdf file by typing `mimetype small.pdf`? – myrdd Nov 15 '18 at 22:15
  • the output of `mimetype small.pdf` is `small.pdf: application/pdf`. I think the program can determine the filetype automatically according to the suffix. – wsdzbm Nov 16 '18 at 01:49
  • my `pdf2ps` is like part of ghostscript 9.25 – wsdzbm Nov 16 '18 at 01:50
  • That's strange. Does your `pdf2ps` look different than [this one](http://git.ghostscript.com/?p=ghostpdl.git;a=blob;f=lib/pdf2ps;h=40b98723161a7a3a7d642ed24567981c5b24c655;hb=HEAD)? – myrdd Nov 17 '18 at 21:56
  • Oh, `ps2pdf` works for me to convert pdf-to-pdf. Did you maybe confuse `ps2pdf` and `pdf2ps`? – myrdd Nov 17 '18 at 22:45
  • I used `pdf2ps`. It's the same as in the link. `ps2pdf` may also work. I didn't try. – wsdzbm Nov 18 '18 at 00:22
  • Would be great if you could try if `ps2pdf` makes any difference to you (maybe by comparing hashes, or by using ”diffpdf“). In my understanding, `ps2pdf` should always work, since it uses the `pdfwrite` driver, just like the highest-voted `gs` answer. — I suggest you to state in your answer that `ps2pdf` might work as well (or even that it might work in more cases). (If you'll do so, you'll get +1 from me ;).) – myrdd Nov 18 '18 at 10:56
  • 1
    @myrdd yeah, I made tests. `ps2pdf` is better. – wsdzbm Nov 19 '18 at 12:54
  • thanks! `ps2pdf` did reduce file size 2x. this is worse than `gs` that did 10x compression with no image loss, but it is easier to remember `ps2pdf` :-) :-) :-) – CeDeROM Apr 29 '21 at 22:00
3

After trying gpdf as nullglob suggested, I found that I got the same compression results (a ~900mb file down to ~30mb) by just using the cups-pdf printer. This might be easier/preferred if you are already viewing a document and only need to compress one or two documents.

In Ubuntu 12.04, you can install this by

sudo apt-get install cups-pdf

After installation, be sure to check in System Tools > Administration > Printing > right-click 'PDF' and set it to 'enable'

By default, the output is saved into a folder named PDF in your home directory.

Community
  • 1
  • 1
ryanjdillon
  • 17,658
  • 9
  • 85
  • 110
2

I know there are already many replies to this post but I had the same problem with a PDF created with PDKTK which I wanted to reduce in size.

And as I had said in the comments, the gs command was not suitable for my case.

And as it had already been said in the comments the result of the convert command was too degraded for some people.

But in reality no. The "convert" command can give a correct PDF with a fairly small size.

With this command, the visual is correct with a compression ratio of 74% on my PDF :

convert -density 125 original_file.pdf -quality 100 -compress Zip compress_file.pdf

With this command, the visual is a little less correct but with a compression ratio of 81% on my PDF :

convert -density 100 original_file.pdf -quality 100 -compress Zip compress_file.pdf

Juan
  • 690
  • 4
  • 15
  • This is a really poor option because you remove all the vector graphics, not just the embedded graphics. You essentially convert the pages into compressed bitmaps. That means the crisp text is gone and you could just as well have been collecting jpegs. – oligofren Dec 27 '22 at 07:14
  • Maybe it's a poor option in your case but it worked for the problem I had. So of course I'm posting it here so I can help. And it's not like pasting jpegs. – Juan Jan 03 '23 at 09:06
1

After trying all the answers listed here, the best results I have obtained for a pdf with lots of graphics is

pdftocairo input.pdf output.pdf -pdf

I discovered this by opening a pdf with Evince in Gnome and then printing to file. This resulted in better file compression and better file quality compared to all the other answers for my pdf file. It seems cairo graphics is used in the background when printing to a file this way: running pdfinfo on the resulting file reveals

Producer: cairo 1.16.0 (https://cairographics.org)

Paul Bryan
  • 121
  • 2
0

Okular's Print to PDF

I just turned a 140MB PDF produced with Keynote into 2.8Mb using Okular's Print to PDF. Text was converted to raster and zooming-in too much cleary shows pixels, but images were kept pretty sharp and its useable for messaging apps.

Ariel M.
  • 896
  • 8
  • 24
-1

I didn't see a lot of reduction in file size using qpdf. The best way I found is after pdftk is done use ghostscript to convert pdf to postscript then back to pdf. In PHP you would use exec:

$ps = $save_path.'/psfile.ps';
exec('ps2ps2 ' . $pdf . ' ' . $ps);
unlink($pdf);
exec('ps2pdf ' .$ps . ' ' . $pdf);
unlink($ps);

I used this a few minutes ago to take pdftk output from 490k to 71k.

Kjuly
  • 34,476
  • 22
  • 104
  • 118
Tom
  • 15
  • 1
  • 4
    PHP adds a completely unnecessary complexity and narrows the applicability of this answer – ndemou Nov 08 '17 at 16:34
-3

I had the same issue and I used this function to compress individual pages which results in the file size being compressed by upto 1/3 of the original size.

for (int i = 1; i <= theDoc.PageCount; i++)
{
       theDoc.PageNumber = i;
       theDoc.Flatten();
}
Gabbar
  • 4,006
  • 7
  • 41
  • 78
-5

In case you want to compress a PDF which contains a lot of selectable text, on Windows you can use NicePDF Compressor - choose "Flate" option. After trying everything (cpdf, pdftk, gs) it finally helped me to compress my 1360 pages PDF from 500 MB down to 10 MB.

solf
  • 1