In this post, I will explain how to batch compress PDF files using Ghostscript. Basically, here I will walk you through a step by guide that involves a Python script. You can run this script on any platform and it will compress all the PDF files efficiently. The best part is that it can reduce the size of a given PDF file by 50-60% or more. The entire process runs on your computer, so you will own your data and it can handle thousands of PDF files.
There are online PDF compressors as well as software out there to do the same thing. But in my experience, Ghostscript does the best PDF compression. Also, it supports multiple modes that you can specify to keep the quality of the output file high or low. If you are a programmer, then you can also create a web app or microservice around it to compress PDF files in one of the best ways possible.
How to Batch Compress PDF Files using Ghostscript?
It is very easy to get started with Ghostscript to compress PDF files. Just make sure that you have installed it already on your PC along with Python. Also, make sure that
gs commands are working in the terminal or command prompt.
- Now, you just copy the entire code from this GitHub Gist and save it in a file. Rename the file to any name, but for convenience add “.py” extension.
- Move the script file that you created in the earlier step in the folder that contains all the PDF files that you want to compress.
- Open the command prompt or Terminal window in the folder where you have the input PDF files. Run the script like this and then wait for the compression process to finish.
- After the script has run, you will find all the compressed files in the “compressed” folder that will be created in the current working directory.
Note: If you are not satisfied with the output quality, then edit the script and change ‘
-dPDFSETTINGS=/screen‘ to ‘
In this way, you can use this simple and powerful Python script to bulk compress PDF files. I have used Linux for this tutorial, but it will work in the same way on macOS and Windows. Just make sure that you have Ghostcrript and Python in PATH and then run the script to optimize PDFs in bulk.
For some of my assignments(~60 MB) I needed to compress the PDF files to around 10 MB and I literally tried all the online tools that I could find with disappointments. Finally, I stumbled upon a Stack Overflow thread and found this script. After a little tweaking it to my needs, it worked like magic and I thought why not share it with others? So, if you are looking for the best free way to optimize/compress PDFs in batch, then go with Ghostscript. The steps I have mentioned here will help you.