Compressing pdf files with ghostscript
Photo by Eduardo Casaj├║s Gorostiaga / Unsplash

Compressing pdf files with ghostscript

Recently I bought a bundle of graphic novels written by the fantastic Neil Gaiman, author of The Sandman, Coraline, many more.

Those novels were delivered as gigantic pdf files most of which were well over 1GB and that meant the old iPad I wanted to read them on was just not able to open them. Obviously, the ideal solution in this scenario is to buy a new iPad, yay!

But what if I could solve this problem without spending almost a thousand euros... perhaps I could just optimise these pdf files. Now I know that Acrobat can do that but then I need to spend hundreds of euros on an Adobe subscription and, honestly, I'd rather buy a new iPad. I did try opening the pdf files in every browser and printing to pdf, but that just killed the pdf printer.

So, I did some searching for open-source pdf tools and I was reminded of ghostscript which is 'an interpreter for the PostScript® language and PDF files' that has been around for over 30 years. And not only is it great at converting pdf files, but it has a specific switch, dPDFSETTINGS=/ebook, that can be used to output PDF files optimised for an e-reader. Here's what I did.

Install ghostscript

winget install ArtifexSoftware.GhostScript
brew install ghostscript
sudo apt install ghostscript

Convert the PDF files

gswin64c.exe -sDEVICE=pdfwrite -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -sOutputFile="output.pdf" input.pdf
ghostscript -sDEVICE=pdfwrite -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -sOutputFile="output.pdf" input.pdf

And once the conversion process completed, it takes a while, I had gone from having a 1.3GB pdf that my iPad couldn't even open to a 68MB pdf that opens in seconds.

There are a lot of switches available for the conversion process if your pdf is more complicated than mine were.