PDF files are widely used as documents in organizations around the world. But sometimes your PDF file may be really large to store or transfer it. Did you know that you can reduce the size of PDF file to solve this problem? In this article, we will learn how to reduce PDF size in Linux. There are many tools available for this purpose. We will learn how to reduce PDF size using Ghostscript and ps2pdf tools.
How to Reduce PDF Size in Linux
We will look at how to reduce PDF size in Linux usint ps2pdf and ghostscript packages.
1. Using Ghostscript
You can use Ghostscript tool to easily compress PDF and reduce their size. It is basically an interpreter for Postscript and PDF documents. You can easily install it with the following command.
$ sudo apt install ghostscript
During installation, if you see any prompts, enter y to proceed.
After you have installed ghostscript, you can reduce PDF size with the following command syntax.
gs [options] sOutputFile=output.pdf input.pdf
In the above command, you need to specify the input file that you want to compress, and output file where you want the compressed PDF to be stored.
Here is a sample command to reduce size of PDF /home/ubuntu/data.pdf and save it to /home/ubuntu/new-data.pdf
$ gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dQUIET -dBATCH -sOutputFile=/home/ubuntu/new-data.pdf /home/ubuntu/data.pdf
Ghostscript provides various readymade PDF settings under -dPDFSETTINGS options to quickly shrink your document to popular formats. Here are the available values.
-dPDFSETTINGS=/screen Has a lower quality and smaller size. (72 dpi) -dPDFSETTINGS=/ebook Has a better quality, but has a slightly larger size (150 dpi) -dPDFSETTINGS=/prepress Output is of a higher size and quality (300 dpi) -dPDFSETTINGS=/printer Output is of a printer type quality (300 dpi) -dPDFSETTINGS=/default Selects the output which is useful for multiple purposes. Can cause large PDFS.
2. Using ps2pdf
ps2pdf command will convert PDF to Postscript and back again to PDF, causing more efficient compression.
ps2pdf is generally bundled with Ghostscript. When you install Ghostscript using the above mentioned command, it will also install ps2pdf. Here is the general command syntax to reduce PDF file size using ps2pdf.
$ ps2pdf [options] input.pdf output.pdf
In the above command, we need to specify input and output file paths after options. The PDF content will be stored in output.pdf after compression. If you don’t provide full file paths to input & output file it will refer to your current working directory, for input and output PDFs.
The options used for ghostscript above, are also available for ps2pdf command. Here is a sample command to reduce size of PDF /home/ubuntu/data.pdf and save it to /home/ubuntu/new-data.pdf. We also use -dPDFSETTINGS option in this example.
$ ps2pdf -dPDFSETTINGS=/ebook /home/ubuntu/data.pdf /home/ubuntu/new-data.pdf
In this article, we have learnt a couple of ways to reduce PDF file size in Linux. You can use them as terminal commands to manually shrink PDF documents, in shell scripts to automate PDF compression, or even setup cronjobs to regularly compress PDF documents.
Of course, there are many other libraries to help you compress PDFs. You can even use online tools like small pdf for this purpose. There are even some GUI tools like Densify, which are nothing but GUI front-ends to Ghostscript modules, to reduce PDF size. We have covered CLI tools to compress PDF documents in this article. You can embed them within your application or website to offer it as a feature or a part of a bigger module. If you want to compress just a few PDFs, it is advisable to use an online PDF compressor. If you want to bulk compress PDF documents, then you can use Ghostscript or ps2pdf to do it programmatically, instead of doing it manually.