Linux Applications

How to Compress PDF File in Linux for Free


When you have a massive PDF file or a large number of PDF files to transfer, it is a better practice to compress PDF files. In Linux, there are various methods for compressing the PDF files like command-line tools and GUI tools for free.

This post will have a brief and step-by-step guide on how to compress a PDF file in Linux for free through the command line using GhostScript. GhostScript’s installation process and usage method are demonstrated on the Ubuntu 20.04 LTS system, and it can work on every other Debian-based operating system.

Compress PDF File in Linux Using GhostScript

Ghost Script is a command-line utility used for compressing the PDF files and for performing other PDF-related tasks.

Installation of GhostScript

To install GhostScript on Ubuntu or other Debian-based operating systems, it is a better practice to update and upgrade the system’s packages.

$ sudo apt update && sudo apt upgrade -y

Execute the command provided below to begin the installation of GhostScript:

$ sudo apt install ghostscript

The GhostScript will be installed, and after the successful installation of Ghostscript, it’s time to understand the usage of GhostScript in Ubuntu.

Usage of Ghost Script Command

The syntax for compressing a PDF file using the GhostScript command is given below:

$ gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf

Most of the options need to stay as they are; our concern is with only two options:

-dPDFSETTINGS Option

For high-quality 300 DPI output, use /prepress setting.

For medium-quality output around 150 DPI, use /ebook setting.

For low-quality output around 72 DPI, use /screen setting.

-s OutputFile option

Provide the name of the output file that you want to give.

Lastly, at the end of the command, write down the PDF file you need to compress.

After executing the GhostScript command for compressing the PDF file, you will get the compressed PDF file within a few seconds based on the file size provided.

Example

Suppose we have a file.pdf in the downloads directory and we want to compress it, go to the specific directory where the file is placed.

Execute the GhostScript command provided below to compress the file.pdf:

$ gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dQUIET -dBATCH -sOutputFile=compressed.pdf file.pdf

Do not forget to change the PDF settings according to your choice. Input file name, and output file.

Once the file is compressed, execute the command given below to view the change in the size of the two files:

$ ls -la

You can verify that the compressed file is smaller in size as compared to the original PDF file.

Conclusion

GhostScript is a command-line tool used for compressing PDF files in Linux. In this post, we have learned how to install the GhostScript on Ubuntu, how to compress a PDF file using the GhostScript, and how to use it and alter settings to extract the compressed PDF of our own choice.

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.