11

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
    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. Jan 7, 2017 at 19:08

2 Answers 2

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 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 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. 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 Feb 2, 2022 at 16:49
  • @Freedom_Ben I am a Go expert, so I might be able to help
    – Zombo
    Feb 3, 2022 at 1:55

Your Answer

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

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