Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: June 13, 2025
PDF (Portable Document Format) files are widely used for sharing documents due to their platform independence and formatting capabilities. However, the size of PDF files can sometimes be large, making them difficult to share or store.
In this tutorial, we will explore techniques for optimizing PDF file sizes on Linux to reduce their sizes without compromising the quality or content.
Optimizing PDF files has several benefits, including:
Linux supports the ghostscript, qpdf, and exiftool tools for optimizing PDF files.
ghostscript provides various options to optimize PDF file sizes. Let’s take a closer look.
Before installing tools in Linux, we use sudo apt update to update our system.
On Debian-based distributions, we install ghostscript using:
$ sudo apt install ghostscript poppler-utils
On Fedora/CentOS-based distributions, we run:
$ sudo dnf install ghostscript poppler-utils
To optimize a PDF using ghostscript, we use the gs command:
gs [switches][input]
Here’s a breakdown of the different components of its syntax:
Let’s check out some switches in an example:
$ gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf
Let’s break down what each of these switches means:
qpdf is another useful tool for optimizing PDF file sizes on Linux. It provides advanced optimization or compression options to further reduce the size of PDF files.
After updating our system with sudo apt update, we install qpdf based on our Linux distribution:
On Debian-based distributions, we run:
$ sudo apt install qpdf
On Fedora/Centos-based distributions, we run:
$ sudo dnf install qpdf
The syntax for optimizing documents is:
qpdf [options] [input] [output]
Here’s a breakdown of the different components of the qpdf syntax:
The arguments can be in any order, but the input filename must precede the output filename.
For example, let’s compress the document.pdf file:
$ qpdf --compress-streams=y --object-streams=generate document.pdf qpdf_compressed.pdf
Let’s take a closer look at the arguments:
PDF files can contain metadata such as author names, creation dates, and other information that may contribute to the file size. We can remove this metadata to reduce the PDF file size further using exiftool.
We first need to install exiftool.
On Debian-based distributions, we install exiftool by running:
$ sudo apt install libimage-exiftool-perl
On Fedora/Centos-based distributions, we run:
$ sudo dnf install perl-Image-ExifTool
The syntax for optimization is:
exiftool [options] [input]
For example, to remove metadata from a PDF file document.pdf using exiftool, we execute:
$ exiftool -all:all= document.pdf
In this example, -all:all= specifies the tag name to be modified.
Note that, unlike ghostscript and qpdf, exiftool doesn’t create a new PDF file but optimizes the same PDF document in place.
Let’s summarize the differences between these three tools:
| Basis | ghostscript | qpdf | exiftool |
|---|---|---|---|
| Focus | PostScript and PDF files | PDF optimization and manipulation | metadata manipulation |
| Supported formats | PostScript (PS), Encapsulated PostScript (EPS), PDF | wide range of file formats, including PDF | |
| Functionality | interpreter for PostScript and PDF page description languages | PDF file manipulation and optimization | metadata manipulation |
| Output | a new PDF | a new PDF file | the original PDF is optimized |
In this article, we explored three popular tools for reducing PDFs: ghostscript, qpdf, and exiftool. Optimizing the sizes of PDF files has many benefits, such as more efficient sharing and using less storage.