Tips & Tricks

How to Compress a PDF on a Linux Computer

Linux users who work with PDFs regularly encounter a familiar frustration: the compression tools and tutorials that dominate search results are written for Windows and macOS, and the command-line alternatives for Linux are scattered across different packages with inconsistent documentation. Compressing a PDF on Linux is entirely achievable using free, open-source tools, and in many cases the Linux command-line approach offers finer control over compression parameters than graphical tools on other platforms. The process involves choosing the right tool for the job and understanding the compression options that balance file size against visual quality.

How to Compress a PDF on a Linux Computer

Using Ghostscript for Command-Line PDF Compression

Ghostscript is the most powerful and widely available PDF processing tool on Linux, pre-installed on many distributions and available through every package manager. It can compress PDFs through a command-line invocation that specifies the output resolution, image compression settings, and PDF compatibility level. The basic compression command is gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -sOutputFile=compressed.pdf input.pdf. The PDFSETTINGS parameter controls the compression level across four preset values that Ghostscript interprets into specific downsampling and encoding choices.

The screen preset produces the smallest file by downsampling images to 72 DPI, suitable only for on-screen reading where print quality is not required. The ebook preset downsamples to 150 DPI, producing a reasonable balance for documents that will be read on tablets and laptops. The printer preset downsamples to 300 DPI, preserving print quality while still achieving meaningful compression. The prepress preset applies lossless compression and preserves color profiles for professional printing, producing the largest file among the presets. For most PDF Compression needs on Linux, the ebook or printer preset provides the right trade-off.

WukongPDF

Try Compress PDF

No installation needed. Works directly in your browser.

Get Started โ†’

Using ps2pdf as a Lightweight Alternative

The ps2pdf script, which ships as part of the Ghostscript package on most Linux distributions, provides a simpler command-line interface for the same underlying functionality. Running ps2pdf input.pdf output.pdf applies a default set of compression optimizations without requiring any parameter configuration. The default settings are equivalent to Ghostscript printer preset, producing a file suitable for both screen viewing and printing. This zero-configuration approach is useful for quick compression when you do not need to fine-tune the output.

For more control, ps2pdf accepts Ghostscript parameters through the command line. Running ps2pdf -dPDFSETTINGS=/ebook input.pdf output.pdf passes the ebook preset to Ghostscript. Running ps2pdf -dPDFSETTINGS=/screen -dDownsampleColorImages=true -dColorImageResolution=100 input.pdf output.pdf customizes the color image downsampling specifically. The Reduce PDF Size effect of these custom parameters can be more aggressive than any preset, but always test the output on a single file before applying custom settings to a batch.

Using qpdf for Structural Compression

Ghostscript and ps2pdf re-render the PDF content through the PostScript interpreter, which can introduce subtle rendering changes even at lossless settings. Qpdf takes a different approach, performing structural optimizations on the PDF file without re-rendering the page content. The command qpdf --linearize --object-streams=generate --recompress-flate input.pdf output.pdf removes redundant objects, regenerates compressed streams with better compression ratios, and reorganizes the file structure for more efficient reading. Qpdf compression is truly lossless, the page pixels are identical to the original.

Qpdf is particularly effective on PDFs that have been edited multiple times, because each editing session can leave behind unused objects and suboptimal stream compression. Running qpdf on a PDF that has passed through several rounds of edits often produces a file that is twenty to thirty percent smaller without any change to the visible content. The qpdf tool is available through the package manager on all major Linux distributions and processes files significantly faster than Ghostscript because it skips the rendering step entirely. For PDF Compression where pixel-perfect fidelity to the original is mandatory, qpdf is the preferred tool.

Writing a Shell Script for Batch PDF Compression

Compressing a single PDF from the command line is fast. Compressing a folder of fifty PDFs one at a time is tedious. A short shell script wraps the compression command in a loop that processes every PDF in a directory. The basic script is a for loop: for f in *.pdf; do gs -sDEVICE=pdfwrite -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -sOutputFile="compressed_$f" "$f"; done. This creates a compressed copy of each PDF with a prefix added to the filename, leaving the originals untouched.

Add error checking to the script for production use. Check that the input file exists and is readable before attempting compression. Check that the output file was created successfully and has a non-zero file size after compression. Log the original file size, the compressed file size, and the compression ratio for each file to a text file for later review. A well-written batch compression script saves hours of manual command-line work and provides a record of exactly which files were compressed, when, and by how much. Tools like WukongPDF offer batch Reduce PDF Size functionality through a browser interface for Linux users who prefer a graphical workflow.

Using PDF Compression in a Linux GUI

Linux users who prefer graphical interfaces to the command line have several options for PDF compression. PDF Arranger, available through most package managers, provides a visual interface for reordering, deleting, and rotating PDF pages, and its export function includes a compression option that downsamples images. Xournal++ opens PDFs in a notebook interface and can export annotated documents as compressed PDFs. LibreOffice Draw can open PDFs and export them with compression settings applied through the PDF export dialog.

Browser-based PDF compression tools work identically on Linux, Windows, and macOS because they run in the browser independent of the operating system. Open your preferred browser-based tool, upload the PDF, select the compression level, and download the result. This OS-agnostic approach is particularly convenient on Linux because it avoids the need to install, configure, and update command-line tools or hunt through package managers for the right compression utility.

Verifying Compression Results on Linux

After compressing a PDF, verify the output quality using Linux-native tools. The pdfinfo command, part of the poppler-utils package, displays the page count, page size, and PDF version of both the original and compressed files, confirming that the page structure is intact. The identify command from ImageMagick can extract individual pages as images and report their pixel dimensions, allowing you to check whether downsampling reduced the image resolution by the expected amount.

For a visual quality check, open the original and compressed PDFs side by side in two instances of Evince, Okular, or your preferred Linux PDF viewer. Zoom in to at least 200 percent on a page containing both text and images. Compare the sharpness of text edges and the presence or absence of JPEG compression artifacts in photographic regions. If the compressed version shows visible quality degradation, re-run the compression with a higher-quality preset or custom Ghostscript parameters that increase the downsampling resolution and the JPEG quality threshold.

Using ImageMagick for Image-Specific Compression

When a PDF file size is dominated by a few large images, compressing the entire PDF through Ghostscript may be overkill for the text and vector portions. ImageMagick can target specific images within a PDF. Extract the oversized images using pdfimages, part of the poppler-utils package. Compress each extracted image individually using the convert command with quality and resize parameters tuned for that specific image type. Then reassemble the compressed images into a new PDF using the img2pdf tool.

This surgical approach to compression preserves text sharpness completely because the text layer is never re-rendered. Only the images are modified. The file size reduction can be dramatic for PDFs that combine crisp vector text with unnecessarily high-resolution photographs. For example, a PDF with 1200 DPI photos embedded in a text document can be reduced by eighty percent or more by downsampling just the photos to 300 DPI while leaving the text untouched.

For Linux users who prefer graphical feedback during compression, PDF Compressor is a GTK-based application available through Flathub that provides a visual interface for Ghostscript compression with a side-by-side preview of the original and compressed output. It supports batch processing and saves compression profiles for reuse.

For PDFs that will be distributed via email on Linux, the command mutt supports attaching files directly from the command line. After compressing, verify the compressed file size with ls -lh and confirm it is under the recipient email server attachment limit, typically 25 MB for most providers. If the file is still too large, consider splitting the PDF into smaller sections using pdftk or qpdf before sending.

The poppler-utils package, available on all Linux distributions, includes pdftoppm which converts PDF pages to image formats. This can be useful as an intermediate step when you want to apply image-specific compression tools to individual pages before reassembling them.

For automated PDF compression in a server environment, Ghostscript can be called from cron jobs or systemd timers to process incoming PDFs on a schedule. The command-line approach makes automation straightforward.

Linux native PDF compression tools, combined with the flexibility of shell scripting, provide arguably the most powerful and customizable PDF compression environment available on any operating system.

This flexibility and control makes the Linux approach to PDF compression worth learning for anyone who handles large document collections regularly.

WukongPDF

Try Compress PDF

No installation needed. Works directly in your browser.

Get Started โ†’