12

I'm looking for a Linux command line util that inputs a merged PDF (between 1mb and 500mb) and outputs an optimized/linearized version. Each and every page uses the same set of ~8 images, and the same font. Running one of my test files through Adobe Acrobat Pro took the file from 34MB to 1.6MB in a matter of about 30 seconds. I'm not really expecting performance like that, but something even half as fast/efficient would be wonderful.

I need it for a batched job running on Linux, which is why it needs a CLI. I've tried pdftk, gs, qpdf, pdftops, and pdfsizeopt. The first 4 are either far too slow or don't fully optimize the pdf files. pdfsizeopt seems promising, but I'm having trouble with consistency -- some of my pdfs will work, others will throw errors.

Any ideas other than what I've already listed? I wouldn't even mind purchasing something if it did the job.

2
  • Linearization and file size are not directly related... can you clarify precisely what you want?
    – yms
    Commented Jun 17, 2014 at 19:00
  • @Alec Sanger: If my answer was helpful to you, then please consider marking it as the accepted answer so others may more easily find it in the future. This is also a polite way to thank the person answering your question for helping you out. Commented Jan 7, 2017 at 19:08

3 Answers 3

10

I personally use Ghostscript to optimize the size of my PDF files. You can specify an optimization level and it will shrink the pictures. This also has the (IMHO desirable) side-effect of removing elements that are outside the page margins.

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

The levels you can use are:

-dPDFSETTINGS=/screen (screen-view-only quality, 72 dpi images)
-dPDFSETTINGS=/ebook (low quality, 150 dpi images)
-dPDFSETTINGS=/printer (high quality, 300 dpi images)
-dPDFSETTINGS=/prepress (high quality, color preserving, 300 dpi imgs)
-dPDFSETTINGS=/default (almost identical to /screen)
2
  • For anyone wondering, this yields much better compression than pdfsizeopt Commented Nov 13, 2019 at 23:57
  • I may be the only one who got bit by this, but it was bad enough I thought I'd leave a comment. gs is a very common alias (especially for git status). If you're getting an error message, double check that it's not aliased type gs or use the full name ghostscript instead of gs . Also confirmed working on Fedora (Linux) ghostscript v9.55.0 Commented Feb 1, 2022 at 21:39
2

PdfCpu works great:

pdfcpu optimize in.pdf out.pdf

https://pdfcpu.io/core/optimize

3
  • pdfcpu choked on a pdfs that were created from a google doc with error: dereferenceObject: problem dereferencing object 69: pdfcpu: parse: duplicate key . However running it through GhostScript first (using other answer) first and then pdfcpu optimize afterward yielded better results than ghostscript alone. Commented Feb 1, 2022 at 21:45
  • are you a dev or otherwise affiliated with the project? I can't share the pdf from yesterday but I can try to reproduce it with a pdf that is redistributable Commented Feb 2, 2022 at 16:49
  • @Freedom_Ben I am a Go expert, so I might be able to help
    – Zombo
    Commented Feb 3, 2022 at 1:55
0

mutool is what I've been using for the past few years

# smallest output possible
mutool clean -ggggl -dfiz IN.pdf OUT.pdf

# slightly but not significantly larger
mutool clean -gggg -l IN.pdf OUT.pdf

https://mupdf.com

New contributor
Honore Doktorr is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Dec 9 at 17:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.