How-To GuidesMarch 24, 2026
Meidy Baffou·LazyPDF

How to Compress PDF Files on Linux with Ghostscript

Ghostscript is the gold standard for PDF compression on Linux. It's powerful, configurable, and available on every major Linux distribution. When you need to reduce PDF file sizes — whether for email attachments, web uploads, or storage efficiency — Ghostscript gives you precise control over the compression settings and output quality. But Ghostscript's command syntax is verbose and its flags are unintuitive for newcomers. Knowing which flags to use and what each setting does makes the difference between excellent compression and botched output. This guide cuts through the complexity and gives you the exact commands you need for every compression scenario on Linux. Beyond basic compression, this guide covers advanced topics: understanding `-dPDFSETTINGS` profiles and when to use each, controlling image DPI and color modes, batch compression scripts for directories of PDFs, and common pitfalls to avoid (like the ICC color profile bug that breaks certain images). For quick, one-off compressions where you don't want to open a terminal, LazyPDF's browser-based compress tool in Firefox or Chromium is a fast alternative. But for automation, batch processing, or maximum control, Ghostscript on the command line is unmatched.

Basic PDF Compression with Ghostscript on Linux

The essential Ghostscript compression command follows a consistent pattern. Here are the most important commands to know:

  1. 1Install Ghostscript if needed: `sudo apt install ghostscript` (Ubuntu/Debian) or `sudo dnf install ghostscript` (Fedora).
  2. 2Basic compression: `gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -sOutputFile=compressed.pdf input.pdf`
  3. 3For maximum compression (smallest file, lower quality): replace `/ebook` with `/screen`
  4. 4For print-quality output: replace `/ebook` with `/prepress`
  5. 5To compress in-place (overwrite original): `gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -dPDFSETTINGS=/ebook -sOutputFile=temp.pdf input.pdf && mv temp.pdf input.pdf`

Understanding Ghostscript PDFSETTINGS Profiles

The `-dPDFSETTINGS` flag is the single most important compression control in Ghostscript. It sets a bundle of quality parameters optimized for different use cases. `/screen` — Lowest quality, smallest file size. Images are downsampled to 72 DPI, suitable only for on-screen viewing. Use this for PDFs you share via messaging apps or attach to casual emails where visual quality doesn't matter much. `/ebook` — Medium quality, good file size reduction. Images downsampled to 150 DPI. This is the recommended setting for most PDFs — it produces files that look excellent on screen and acceptable when printed. File sizes are typically 50–80% smaller than the original. `/printer` — Good quality, moderate compression. 300 DPI images. Use for PDFs that will be printed but don't require perfect color accuracy. `/prepress` — Highest quality, minimal compression. 300+ DPI with color profiles preserved. Use when you need to send a PDF to a professional printer or preserve maximum fidelity. `/default` — Ghostscript's default settings, which are conservative. Not recommended for compression use — it often doesn't reduce file size meaningfully. For most Linux users sending PDFs by email or uploading to web portals, `/ebook` is the right choice — it balances file size reduction with quality that looks good on any screen.

Batch Compress Multiple PDFs on Linux

Ghostscript becomes especially powerful when combined with shell scripting for batch operations. Here's a practical script that compresses all PDFs in a directory and saves them to a separate `compressed/` subdirectory: ```bash #!/bin/bash mkdir -p compressed for f in *.pdf; do echo "Compressing $f..." gs -dBATCH -dNOPAUSE -q \ -sDEVICE=pdfwrite \ -dCompatibilityLevel=1.4 \ -dPDFSETTINGS=/ebook \ -sColorConversionStrategy=RGB \ -sOutputFile="compressed/$f" "$f" orig=$(du -sh "$f" | cut -f1) comp=$(du -sh "compressed/$f" | cut -f1) echo " $f: $orig → $comp" done ``` Note the `-sColorConversionStrategy=RGB` flag — this is critical. Without it, Ghostscript may fail on PDFs containing ICC color profiles, producing broken output. Always include this flag in compression commands. This script reports the file size before and after compression for each file, giving you immediate feedback on the compression results.

Troubleshooting Ghostscript PDF Compression Issues on Linux

Several common problems trip up Linux users when compressing PDFs with Ghostscript. Broken images after compression: This is almost always caused by ICC color profile handling. The fix is to add `-sColorConversionStrategy=RGB` to your command. Do NOT use `-sColorConversionStrategy=LeaveColorUnchanged` — this is the flag that causes image corruption with certain ICC-embedded images. No file size reduction: If the output is the same size as the input, the PDF may already be well-compressed, or you may be using `/prepress` settings which prioritize quality over compression. Try `/ebook` or `/screen` settings. PDF version incompatibilities: Some PDFs created with newer software use features that older Ghostscript versions don't handle correctly. Update Ghostscript with `sudo apt upgrade ghostscript`. On Ubuntu, the official Ghostscript PPA usually has a more recent version than the default repositories. Slow compression: Large PDFs with many high-resolution images take time. A 100-page scanned document might take 30–120 seconds. This is normal — Ghostscript is doing substantial image processing work. For very large batch jobs, consider running Ghostscript with `nice` to avoid impacting system performance: `nice -n 10 gs ...`.

Frequently Asked Questions

What's the best Ghostscript PDFSETTINGS for emailing PDFs on Linux?

Use `/ebook` for the best balance of quality and file size reduction. It produces files that look great on screen (which is how email recipients usually view PDFs) while typically reducing file size by 50–80%. Only use `/screen` if you need the absolute smallest file and visual quality is less important.

Why does Ghostscript produce blurry images when compressing PDFs on Linux?

Blurry images result from aggressive image downsampling. If you're using `/screen` settings, images are downsampled to 72 DPI which looks poor when printed. Switch to `/ebook` (150 DPI) or `/printer` (300 DPI) for better image quality while still achieving good compression.

Can I compress PDFs on Linux without Ghostscript?

Yes. LazyPDF's browser-based compress tool in Firefox or Chromium is a fast alternative that requires no installation. For command-line alternatives, `ps2pdf` (which uses Ghostscript internally) or Imagemagick's convert command can also reduce PDF sizes, though Ghostscript directly offers the most control.

How do I compress a PDF on Linux without losing text quality?

Text in PDFs is vector-based and is never degraded by Ghostscript compression regardless of settings. Only raster images (photos, scans) are downsampled. For documents that are primarily text with a few small images, even `/screen` compression will leave the text perfectly sharp while reducing file size significantly.

Need quick PDF compression on Linux without the command line? Try LazyPDF in your browser.

Try It Free

Related Articles