Compress PDF Files w/ Linux The Easy Way

For those of you who are always sharing graphics intensive pdf files, having a way to compress the final output before sharing it via whatever medium you choose is important. A great example would be that one of my UI designers creates these 31+M UI flow diagrams in pdf format and before I share it with clients, I always compress them to a sharable size.

As most of you Linux gurus know, there’s always the gs command that looks something like this:

gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.6 -dPDFSETTINGS=/ebook
-dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf
"input.pdf"

That seems like an awful long command to remember every time you wanted to compress a pdf file but not to worry, there’s a python script out there that can help simplify things!

Download the script and throw it into a bin folder that’s defined in your shell environment. This way, you are able to execute the cpdf script like a command from anywhere and you won’t need to define the full path of files when defining your input and output file arguments.

So let’s take a look at what it looks like when we use the cpdf script.

Here’s the test.pdf file and it’s size:

jeremy@laptop:~/test$ ll
total 31M
drwxrwxr-x 2 jeremy jeremy 4.0K May 1 14:59 .
drwxr-xr-x 94 jeremy jeremy 4.0K Apr 30 21:45 ..
-rw-rw-r-- 1 jeremy jeremy 31M Apr 29 23:32 test.pdf

Now let’s try to compress that 31M test.pdf file:

jeremy@laptop:~/test$ cpdf ebook test.pdf compressed.pdf

It will take a while since it’s a decently large file but then it will eventually finish and now we can check how large the compressed pdf file is:

jeremy@laptop:~/test$ ll
total 35M
drwxrwxr-x 2 jeremy jeremy 4.0K May 1 15:00 .
drwxr-xr-x 94 jeremy jeremy 4.0K Apr 30 21:45 ..
-rw-rw-r-- 1 jeremy jeremy 4.0M May 1 15:02 compressed.pdf
-rw-rw-r-- 1 jeremy jeremy 31M Apr 29 23:32 test.pdf

The compressed.pdf file is only 4M! That’s a huge improvement! How’s the quality though? You should check for yourself. However, when I opened my compressed.pdf, not only did it load faster, the quality difference is almost unnoticeable by the naked eye.

You can also explore what preset types are available and what they mean by doing the following:

jeremy@laptop:~/test$ cpdf types
screen - selects low-resolution output similar to the Acrobat Distiller "Screen Optimized" setting.
ebook - selects medium-resolution output similar to the Acrobat Distiller "eBook" setting.
printer - selects output similar to the Acrobat Distiller "Print Optimized" setting.
prepress - selects output similar to Acrobat Distiller "Prepress Optimized" setting.
default - selects output intended to be useful across a wide variety of uses, possibly at the expense of a larger output file.

There you have it! Happy compressing!