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.

About these ads

36 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 :-(

  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

  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. Here’s a bitcookie 8635 for you for your great tip.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s