17

I need a way to size down and compress batches of PDF files. I'd prefer to do this on Windows, but Linux would be fine if it meant a smoother workflow.

I know that there's programs such as NitroPDF and Acrobat that allow you to accomplish this, but I'm afraid it would have to be done on a file-by-file basis. These programs also aren't cheap and I'd prefer not to buy them just so I can use one or two features.

Background info: I use CamScanner to digitize receipts and invoices for entry into accounts (FreeAgent). CamScanner pdfs are all A4 sized and multi-page ones often exceed the 2MB attachment limit.

0

2 Answers 2

17

I'm suggesting a command line tool here, which can be easily batched with loops in built-in scripting languages in Windows, Linux, OS X, etc.


ImageMagick supports PDFs and has a resize option with its convert tool. I've never used it personally, but you can try to play around with that.

You can also use the compress option (there's an example here):

Rotate a PDF

$ convert -rotate 270 -density 300x300 -compress lzw in.pdf out.pdf

This assumes a TIFF-backed PDF. The density parameter is important because otherwise ImageMagick down-samples the image (for some reason). Adding in the compression option helps keep the overall size of the PDF smaller, with no loss in quality.

For multipage PDFs, you may want to use pdftk, then use mogrify from ImageMagick to convert each page in place:

$ pdftk in.pdf burst
$ mogrify -rotate 270 -density 300x300 -compress lzw pg_*.pdf
$ pdftk pg*.pdf cat output out.pdf
$ rm pg*.pdf


To convert PDF files with ImageMagick, you need to have GhostScript installed.


ImageMagick can convert multipage PDFs. While mogrify will convert in place, I recommend you use convert so you can keep the originals in case of accident.


I've done some testing on your provided sample PDF. This worked quite well for me:

convert -density 200 -compress jpeg -quality 20 test.pdf test2.pdf

Density defaults to 72 DPI. By setting it higher we can get a higher resolution and therefore acceptable quality. It looked alright at 150, and was a little smaller, but if you want to cater for a range of PDFs 200 should work.

JPEG compression should either auto choose a level or default to 92 on a scale of 1 to 100 with 100 being the best. Setting it at 20, it looks almost as good as the original (a little fuzzier and the small text at the bottom is a little hard to read, but it was originally anyway).

These options bring your 1.7MB sample down to 0.5MB, while keeping it readable. You can experiment a little.

If you want a smaller size (both of the file and of the image/PDF), you can use -resize #%, e.g. -resize 75%. On your example PDF, this makes the small print at the bottom pretty much unreadable, though.

If you're still tight for space, especially for the multipage PDFs, you could compress further by adding the files to a ZIP (or other) archive. This brought the file size down to 0.43MB on that test PDF (reducing the JPEG compression quality has a much more drastic effect). You could also split the PDF file into pages with pdftk, as @glallen suggested in his edit, or split the archive and recombine at the other end.

2MB is also a rather small attachment limit, you may want to look into other email providers. From memory, GMail provides over 10MB per email.

These options, and more, are fully documented on their website.

7
  • That seems like an excellent approach, but I don't think my pdfs are TIFF-backed. CamScanner first takes JPEG photos and then converts them to PDF for export. Processing these PDFs does seem to downsample them like the blog post indicates, no matter the compression. Converting from PDF to TIFF and then back into PDF does seem to preserve quality but inflates the file size and messes up the page size and density.
    – Stan
    May 28, 2012 at 11:06
  • @paganompu As I understand, you want to reduce the file size. How about compressing as JPEG? How did resize turn out? Do you have a (non-sensitive) sample file we could play with? Is the down-sampling acceptable?
    – Bob
    May 28, 2012 at 11:34
  • JPEG would've been fine, but I need to upload multi-page documents and I can only have one attachment per account entry. Downsampling is severe: this test document is unreadable after converting with lossless compression.
    – Stan
    May 28, 2012 at 12:20
  • @paganompu I've experimented a little myself, and I've updated the answer with a working command. See here for a test on your file.
    – Bob
    May 29, 2012 at 7:45
  • That command works like a charm. Seems like my "downsampling" was due to omitting density ... I swear I can read and follow instructions! Bit embarrassed now :S Thank you for taking the time to help me Bob
    – Stan
    May 29, 2012 at 9:50
21

So convert from ImageMagick will produce rasterized PDF and many people would be interested in keeping vector graphic and text untouched so only embedded images are compressed. So good alternative to making compression is using gs from package ghostscript example of usage:

gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -sOutputFile=out.pdf in.pdf

in above command parameter: -dPDFSETTINGS=/ebook is important. It can have 3 values:

-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)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .