Compress a PDF with pdftk

UPDATED (9/7/11): The package is not pdftk, but Ghostscript, and it’s easily installable through homebrew on a mac.

Ok, so I don’t know why this works, but it does and I haven’t seen any decrease in quality. I suspect converting from PDF to PS and back just optimizes the PDF.
Requirements

The Process

  1. Convert your PDF to PS (this creates a large file
  2. Convert the new PS back to a PDF


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

Results
large.pdf : 6.3MB
very_large.ps : 53.4MB
small.pdf : 2.4MB
Looks like pretty good compression to me.

If anyone knows why this works so well, please let me know.

Advertisements

56 responses to “Compress a PDF with pdftk

  1. I guess there is some quality loss during the process. Losing lines and curves just having bitmaps …

  2. pdftk is also able to uncompress and compress a pdf (see those options for the ‘pdftk’ binary) which should simply uncompress and recompress the data within the pdf to produce a smaller pdf — but not as small as the improvement you got.

  3. This method not always work, in my case the resulted PDF is bigger.

  4. Very good!
    thanks

  5. Pingback: Compressione di un pdf con minima perdita di qualitร  « Il blog di Andrea Lazzarotto

  6. I don’t know what happends during the process but it helps me a lot ๐Ÿ™‚ Great!
    Thanks

  7. it blanked the top edges of all pages…

  8. Thank you sooooo very very much for those instructions! I had this pdf file that was, believe it or not, 747 Mb in size. After using your instructions, I got myself a file that was ONLY 11 Mb in size without any loss in quality to the file! Thank you again!

  9. handy 6MB->227KB

  10. Its one and a half years since your post…but still helping people ๐Ÿ˜‰

    Thanks.

  11. 7.2 MB -> 360 MB -> 14.3 MB ๐Ÿ˜ฆ

    • I have the same problem.
      10.9 MB to 486 MB to 56 MB!
      I suggest you use this method:
      ps2pdf -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dQUIET -dBATCH -sOutputFile=out.pdf original.pdf

  12. Hmm…

    with this howto I got 4.9 MB from 1.4 MB in size! Something went wrong!

  13. Install ghostscript and:

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

    • LaPeGa: Thanks! Exactly what I was looking for!
      Your solution compress images greatly, reducing the PDF file size considerably. It’s a lossy compression (loose some information) in the images. The text is still selectable after compression. Exactly what someone need when trying to send a large PDF by email.

  14. Nice tip.
    One thing though, I don’t believe pdftk is required here. In fact, you are not even using it. The commands pdf2ps and ps2pdf are part of ghostscript.

    Too bad it didn’t help me, my 42 mb file went up to 7.9 gb and down to 1.4 gb ๐Ÿ˜ฆ

  15. It doesn’t work in my case, which generated a bigger file…

  16. that worked great! Best solution yet! Crushed my 27 mb down to 6.8mb

  17. Pingback: hx4

  18. Santiago Restrepo

    It’s just incredible. Has one PDF made of 800×600 photographs of a document = 75 Mb. After your treatment = 23.1 Mb.
    I thought if I looped the process it were going to be like 10 Mb. It was not. It was = 23.1 Mb. So, no fractal allowed!

    Thanks!

  19. Still a great tip! Thanks.
    (90MB -> 15MB)

  20. THANK YOU VERY MUCH!
    SOURCE was 645 MB ==> RESULT is 7,5 MB

  21. You can also use ImageMagick’s convert or mogrify tools to downsample a PDF. e.g; convert foo.pdf -colors 2 -density 150×150 bar.pdf might turn a 5.5 Mb, 300dpi grayscale PDF into a 457kb 150dpi B&W PDF. However you get more readable results at the expense of larger output (1.4Mb) if converting to B&W with ps2pdf foo.pdf | pdf2ps – bar.pdf and mogrifying the results.

  22. pdf2ps and ps2pdf are not a part of pdftk

  23. Interesting. It is working well.

    Thanks for this tip.

  24. From 220Mb pdf to 1,9 Gb ps to a 72Mb PDF!
    Thank you!

  25. This worked well for me here. I had a 3,3MB PDF that contained a few scanned pages beside text. This trick reduced the size to 1,4MB instead!

    Thanks for sharing, even if it does not work for every document, it is still worth a try!

  26. gs -dNOPAUSE -sDEVICE=pdfwrite -r150 -sOUTPUTFILE=merged.pdf -dBATCH *.pdf

    >>>( -r150 )<<< set output file to 150dpi
    -r100 set 100dpi etc etc…

    my 2 cents

  27. Thanks, one PDF went from 258MB to 8MB. In case you are using Python 2.7, here’s a little script that provides a very basic GUI. Creates two folders, one for the PS file, one for the compressed PDF. Hope the indentations display properly…

    +++++++++++++++++++++++++++++++++++++

    import os, subprocess, Tkinter, tkFileDialog, tkSimpleDialog

    from Tkinter import *

    root = Tk()
    root.title(“PDF Compressor”)

    def compressPdf():
    volumeDirectory = str(tkFileDialog.askdirectory(title=”Select Volume”))
    pathName, directoryName = os.path.split(volumeDirectory)
    directoryNameNew = directoryName + ‘_compressed’
    directoryPathNew = os.path.join(pathName, directoryNameNew)
    psDirectoryName = directoryName + ‘_ps’
    psDirectoryPath = os.path.join(pathName, psDirectoryName)
    if os.path.exists(directoryPathNew):
    pass
    else:
    os.makedirs(directoryPathNew)
    if os.path.exists(psDirectoryPath):
    pass
    else:
    os.makedirs(psDirectoryPath)
    pdfFiles = os.listdir(volumeDirectory)
    for pdfFile in pdfFiles:
    pdfFileName, extension = pdfFile.split(“.”)
    psFileName = pdfFileName + ‘.ps’
    pdfOldPath = os.path.join(pathName, directoryName, pdfFile)
    psPath = os.path.join(psDirectoryPath, psFileName)
    pdfNewPath = os.path.join(directoryPathNew, pdfFile)
    subprocess.call([“pdf2ps”, pdfOldPath, psPath])
    subprocess.call([“ps2pdf”, psPath, pdfNewPath])
    print pdfNewPath
    print ‘+++++++++++++++++++’
    print ‘Done!’

    def close():
    root.destroy()

    menu = Menu(root)
    root.config(menu=menu)
    ## File Menu
    filemenu = Menu(menu)
    menu.add_cascade(label=”File”, menu=filemenu)
    filemenu.add_command(label=”Exit”, command=close)
    ## Process Menu
    processmenu = Menu(menu)
    menu.add_cascade(label=”Process Data”, menu=processmenu)
    processmenu.add_command(label=”Compress PDFs”, command=compressPdf)

    mainloop()

  28. Cool trick.

    I just tried it with poor results – The output was 10x bigger than the input. But my ubuntu box has “pdf2ps” & “pdftops”. Using the latter instead of the former gave good results.

    I have no idea why this works, but it’s a cool trick. Thanks!

    Here’s another thing that shrinks a PDF where other tools fail… At least it worked with the file that lead me to this page ๐Ÿ˜‰

    gs -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -sOutputFile=out-file.pdf in-file.pdf

    YMMV.

    This should work, but on some files it doesn’t… Which lead me to this page ๐Ÿ˜‰

    pdftk in-file.pdf output out-file.pdf compress

  29. almost 4 years from your post and still useful.

    pdftops & ps2pdf: 20mb -> 133mb -> 1.5mb.

  30. simple and effective, thank you ๐Ÿ™‚

    I’m curious how often I can use this on one file….

  31. Pingback: Reducing PDF file size « Unforgettable Virtual Park

  32. 28MB -> 14MB
    Nice!

  33. 322 MB to 2.2 GB to 177 MB, thumbs up!!
    Although, I can say that there is a loss of quality.

  34. it worked great for me from a 85mb to 182mb and then to a final 5.3! thank you.

  35. Very helpful and simple…thank you

  36. Reblogged this on baichuns and commented:
    This work for me! thanks

  37. this works for me! thanks

  38. For my pdf with many images

    pdf2ps & ps2pdf
    525MB -> 1.7GB -> 335MB
    Adobe acrobat pro reduced size saving
    525MB -> 107MB
    convert foo.pdf -colors 2 -density 150×150 bar.pdf
    525MB -> 57MB (but none of the text is legible)
    convert foo.pdf -density 175×175 bar.pdf
    525MB -> 67MB (but none of the text is legible)
    convert foo.pdf -density 225×225 bar.pdf
    525MB -> 67MB (but none of the text is legible – no different than above)

    Unfortunately will have to stick w/ adobe ๐Ÿ˜ฆ
    Thanks for the tips though! Seems to be best for more text than images pdfs

  39. In my case this didn’t worked neither using pdf2ps nor pdftops. The pdf I am trying to compress is a scanned document. Thanks if anyone has some interesting comment about it.

  40. 7 years later, still useful.
    Cheers mate!

  41. This works like a charm. Thank you.

  42. I guess the ghostscript solution is better than the pdf2ps and back ps2pdf because there the actual text does not get lost

  43. Reblogged this on John Athanasiou and commented:
    This is something I had no idea about. It’s a bit messy (links inside pdf are broken since it’s exported to another file) but fast and simple way to decrease a pdf’s size by ~50%

  44. You will lose jpeg image quality. See

    better to use
    “`
    gs -sDEVICE=pdfwrite -sOutputFile=output.pdf -dAutoFilterColorImages=false -c “<< /ColorImageDict <> >> setdistillerparams” -f input.pdf
    “`
    to preserve acceptable image quality.

    See for acceptable ColorImageDict values.

  45. This is brilliant. Added it to my IT Notes and gave you credit. Thanks.

  46. Very useful, worked for me. Thanks!!!
    4MB -> 600K

  47. Otbained from PDF 892,2 kb โ†’ PS 13,4 Mb โ†’ PDF 456,5 kb but the embedded text in the original PDF had disappeared. (I had an image of a text, plus the text embedded. The result is pure image of the text, with nothing to copy as text and paste.)

  48. 10 years after… It works! ๐Ÿ™‚
    Thanks!

  49. Thank you! Works better than online PDF shrinkers.
    Thanks for keeping this online all this time too.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s