惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Application and Cybersecurity Blog
Application and Cybersecurity Blog
H
Help Net Security
罗磊的独立博客
博客园 - Franky
小众软件
小众软件
博客园 - 三生石上(FineUI控件)
博客园 - 叶小钗
爱范儿
爱范儿
月光博客
月光博客
博客园 - 聂微东
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
量子位
GbyAI
GbyAI
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
大猫的无限游戏
大猫的无限游戏
Y
Y Combinator Blog
Martin Fowler
Martin Fowler
Hugging Face - Blog
Hugging Face - Blog
The GitHub Blog
The GitHub Blog
T
Threat Research - Cisco Blogs
Spread Privacy
Spread Privacy
Hacker News: Ask HN
Hacker News: Ask HN
B
Blog RSS Feed
Know Your Adversary
Know Your Adversary
T
Troy Hunt's Blog
I
InfoQ
L
Lohrmann on Cybersecurity
Recent Announcements
Recent Announcements
H
Hacker News: Front Page
Scott Helme
Scott Helme
Jina AI
Jina AI
云风的 BLOG
云风的 BLOG
Engineering at Meta
Engineering at Meta
The Register - Security
The Register - Security
C
CERT Recently Published Vulnerability Notes
MongoDB | Blog
MongoDB | Blog
Help Net Security
Help Net Security
MyScale Blog
MyScale Blog
F
Fortinet All Blogs
Project Zero
Project Zero
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
J
Java Code Geeks
AWS News Blog
AWS News Blog
阮一峰的网络日志
阮一峰的网络日志
T
Threatpost
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Apple Machine Learning Research
Apple Machine Learning Research
B
Blog
Cloudbric
Cloudbric

DigitalOcean Community Tutorials

Mastering grep with Regular Expressions for Efficient Text Search It's Time to Break Up with Your Cloud: Why AI Teams are Switching We Built a Private-Document AI App to Test Platform Security. Here Is What We Could Actually Verify. PostgreSQL Explained: A Complete Beginner-to-Advanced Guide How To Install and Configure Postfix on Ubuntu How To Build a Web Application Using Flask in Python 3 Build AI Reading List with DigitalOcean Functions and Mistral How To Concatenate Strings in Python How to Allow MySQL Remote Access Securely How To Install and Use Docker on Rocky Linux How To Build a Multi-Agent AI System with Docker Agent DSPy Use Cases: Build Optimized LLM Pipelines How To Submit AJAX Forms with jQuery Build an AI-Powered GPU Fleet Optimizer with the DigitalOcean AI Platform ADK Monitor GPU Utilization in Real Time: A Complete Guide Reduce File Size of Images in Linux - CLI and GUI methods How To Set Up a Private Docker Registry on Ubuntu How To Troubleshoot Terraform: Errors and Fixes How to Use Go Modules Python Multiprocessing Example: Process, Pool & Queue Convert Class Components to Functional Components with React Hooks How To Install and Configure Ansible on Ubuntu LLM Tokenizers Simplified: BPE, SentencePiece, and More How To Monitor System Authentication Logs on Ubuntu How to Use Traceroute and MTR to Diagnose Network Issues How to Deploy Postgres to Kubernetes Cluster Importing Packages in Go: A Complete Guide Create RAID Arrays with mdadm on Ubuntu How To Make an HTTP Server in Go How To Set Up Time Synchronization on Ubuntu How To Use Struct Tags in Go apt-key Deprecation: Add Repositories with GPG on Ubuntu Linux ps Command: 20 Real-World Examples Python struct.pack and struct.unpack for Binary Data Deadlock in Java: Examples, Detection, and Prevention How To Use Find and Locate to Search for Files on Linux Structured Resume Skill Extraction Using Mistral-7B Inference How to Use the Python Main Function How to Set Up NemoClaw on a DigitalOcean Droplet with 1-Click Build an End-to-End RAG Pipeline for LLM Applications From Single to Multi-Agent Systems: Key Infrastructure Needs Back Up Data to Object Storage Using Restic How to Generate Videos with LTX-2.3 on DigitalOcean GPU Droplets How To Install LAMP Stack (Apache, MySQL, PHP) on Ubuntu How to Download Files with cURL How To Use Variadic Functions in Go Generate UUIDs with uuidgen on Linux How To Use EJS to Template Your Node Application How to Install Node.js on Ubuntu (Step-by-Step Guide) MongoDB Indexes: Improve Query Performance with Node.js LLM Tool Calling with DigitalOcean AI Platform and Databases What are Text Diffusion Models? - An Overview Crafting a Game from Scratch with GPT-5.4 Building Long-Term Memory in AI Agents with LangGraph and Mem0 How To Install PHP 7.4 and Set Up a Local Development Environment on Ubuntu 20.04 Build a GraphQL API in Go to Upload Files to Spaces How To Lint and Format Code with ESLint in Visual Studio Code Train YOLO26 for Retail Object Detection on DigitalOcean GPUs How To Work with JSON in MySQL How to Use the JavaScript .map() Method Building a Scalable App with MongoDB Using DigitalOcean's MCP Server How to Create an SSH Key in Linux: Easy Step-by-Step Guide Measure MySQL Query Performance with mysqlslap How To Use *args and **kwargs in Python 3 Nemotron 3 helped me find the perfect dish rack? A2A vs MCP - How These AI Agent Protocols Actually Differ How To Install and Manage Supervisor Docker Container Images with Watchtower on Ubuntu Getting Started with Qwen3.5 Vision-Language Models How To Create a New Sudo-Enabled User on Ubuntu How to Use Ansible to Install and Set Up Docker on Ubuntu How To Enable Remote Desktop Protocol Using xrdp on Ubuntu 22.04 How To Convert a String to a List in Python How To Check If a String Contains Another String in Python How to Read a Properties File in Python Python Command Line Arguments: sys.argv, argparse, getopt Mastering Grep command in Linux/Unix: A Beginner's Tutorial Understanding Python Data Types How to Implement a Stack in C With Code Examples Python os.system() vs subprocess: Run System Commands How To Install and Use Docker Compose on Ubuntu How to Add and Delete Users on Ubuntu How To Order Query Results in Laravel Eloquent How To Define and Use Handlers in Ansible Playbooks How To Install and Use SQLite on Ubuntu How To Install and Use Homebrew on macOS How To Manage DateTime with Carbon in Laravel and PHP How To Install Git on Ubuntu How To Install and Secure Redis on Ubuntu How To Build and Install Go Programs on Linux Using ldflags to Set Version Information for Go Applications How To Build a Node.js Application with Docker How To Add JavaScript to HTML How To Reset Your MySQL or MariaDB Root Password How To Add Images in Markdown How To Set Up a Production Elasticsearch Cluster with Ansible How To Set Up a Firewall Using firewalld on CentOS Understanding Systemd Units and Unit Files How To Set Up Replication in MySQL How To Use the .htaccess File
Reduce PDF File Size in Linux: Tools and Methods
Vijaykrishna Ram · 2026-04-06 · via DigitalOcean Community Tutorials

Introduction

Large PDF files slow down uploads, hit email attachment limits, and eat storage on shared servers. On Linux, you have several reliable ways to bring those sizes down — from a single Ghostscript command that takes seconds to run, to GUI tools that need no terminal at all.

The most direct approach is Ghostscript (gs) with the -dPDFSETTINGS=/ebook preset, which handles most PDFs well and works from any distro’s package manager. For a shorter one-liner, ps2pdf wraps the same engine. If the PDF is mostly images, ImageMagick’s convert command lets you control density and quality directly. For lossless cleanup without touching pixels, cpdf -squeeze removes redundant objects and metadata bloat. And if you prefer not to use the terminal at all, PDF Arranger and LibreOffice Draw both export compressed PDFs from the desktop.

These methods work on all major Linux distributions. Readers new to the Linux terminal can start with An Introduction to Linux Basics. For retrieving PDFs from remote hosts before compressing them locally, see Downloading Files on the Linux Command Line.

Key Takeaways

  • The fastest reliable one-liner for general sharing is Ghostscript with /ebook: gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -sOutputFile=out.pdf in.pdf.
  • Use Ghostscript or ps2pdf for mixed text and image PDFs; both accept -dPDFSETTINGS presets for consistent, repeatable output. Use ImageMagick when the PDF is image-only and you need quick density and JPEG quality control. Use cpdf -squeeze for lossless structural cleanup with no pixel resampling. Use GUI export for one-off desktop jobs where scripting adds no value.
  • Scanned PDFs store each page as a full-resolution bitmap with no text layer, so reducing DPI and color depth is the primary lever, not font or metadata compression.
  • Batch compression iterates a Ghostscript command over all PDF files in a directory; always write output to a separate path to avoid truncating the source file in place.
  • Confirm gains with ls -lh file.pdf or du -sh file.pdf before and after.
  • For most readers and attachments, /ebook is the best general-purpose Ghostscript preset between size and readability.
  • On Debian-derived systems, ImageMagick often ships with PDF coder rights disabled; adjust policy.xml before convert reads or writes PDFs.

What Causes Large PDF File Sizes on Linux

Before picking a compression method, it helps to know what is making your specific PDF large. Run du -sh file.pdf to confirm the file size, then use pdfinfo to get a quick summary of page count, dimensions, and embedded metadata:

pdfinfo input.pdf
Title:           Q3 Report
Pages:           42
Page size:       595.28 x 841.89 pts (A4)
File size:       76534291 bytes
Optimized:       no
PDF version:     1.6

If the file is not optimized and has many pages at large dimensions, image resampling via Ghostscript is likely the right first step. If the file is small in page count but still large, embedded images or font bloat are the more probable cause. The three root causes below map directly to the methods in this tutorial.

Image-Heavy PDFs

Embedded high-resolution images are the leading cause of oversized PDFs on Linux systems. A single slide deck exported at 300 dpi for print, or a photograph-heavy report, can run to hundreds of megabytes before any compression. To check which images are embedded and at what resolution, install poppler-utils if it is not already present, then run pdfimages:

Debian / Ubuntu / Linux Mint:

sudo apt install poppler-utils

Fedora:

sudo dnf install poppler-utils

Arch Linux:

sudo pacman -S poppler

openSUSE:

sudo zypper install poppler-tools

Then run:

pdfimages -list input.pdf
page   num  type   width height color comp bpc  enc interp  object ID
-------------------------------------------------------------------
   1     0 image    2480  3508  rgb     3   8  jpeg   no       6  0
   2     1 image    2480  3508  rgb     3   8  jpeg   no       8  0

If images are at 300 dpi or higher and the PDF is intended for screen reading, resampling to 150 dpi with Ghostscript’s /ebook preset removes most of the excess without changing how the document reads on screen.

Scanned Document PDFs

Each scanned page is a rasterized image, not selectable text, until you run OCR. Scans at 300 dpi to 600 dpi are common for archival quality, so a hundred-page document can reach hundreds of megabytes with no text layer for font compression to exploit. Reducing the effective resolution or converting to grayscale when the source is black-and-white text brings file size down significantly. To confirm whether a PDF is scan-heavy rather than image-heavy, run pdfimages -list input.pdf as shown in the Image-Heavy PDFs section above.

Embedded Fonts and Metadata

PDFs often carry more than their visible content: duplicate font streams from careless merges, unused color profiles, verbose XMP metadata blocks, and leftover object references from incremental edits. None of this is visible to the reader, but it all counts toward file size. If image resampling alone does not reduce the file enough, a structural cleanup pass with cpdf -squeeze or a fresh Ghostscript rewrite removes this hidden overhead. Use pdfinfo to spot unusually high object counts or unoptimized flags as a starting signal.

Run Ghostscript with -dPDFSETTINGS=/ebook (or another preset) to rewrite the PDF with smaller image data and a cleaner internal structure. It is available in the default package repositories for all major Linux distributions, works on mixed text-and-image PDFs, and produces consistent output across runs using the same flags.

Install Ghostscript on Linux

Debian / Ubuntu / Linux Mint:

sudo apt install ghostscript
gs --version
10.02.1

Your installed patch version may differ; expect one version line such as 10.xx.x. For the full list of flags and configuration options, see the official Ghostscript documentation.

Fedora:

sudo dnf install ghostscript

Arch Linux:

sudo pacman -S ghostscript

openSUSE:

sudo zypper install ghostscript

Basic Ghostscript Compression Command

gs \
  -sDEVICE=pdfwrite \
  -dCompatibilityLevel=1.4 \
  -dPDFSETTINGS=/ebook \
  -dNOPAUSE \
  -dQUIET \
  -dBATCH \
  -sOutputFile=output.pdf \
  input.pdf
  1. -sDEVICE=pdfwrite: selects the PDF output device.
  2. -dCompatibilityLevel=1.4: targets PDF 1.4 compatibility for broad reader support.
  3. -dPDFSETTINGS=/ebook: applies the ebook compression preset (150 dpi, a practical quality/size balance).
  4. -dNOPAUSE: disables pause prompts between pages.
  5. -dQUIET: suppresses informational output.
  6. -dBATCH: exits Ghostscript after processing completes.
  7. -sOutputFile=output.pdf: specifies the output file path.
  8. input.pdf: the source file to compress.

Note: Never point -sOutputFile at the same path as input.pdf. Ghostscript may produce a zero-byte or corrupted file if the input is overwritten during writing.

Understanding Ghostscript -dPDFSETTINGS Presets

Preset DPI Typical Size Reduction Use Case
/screen 72 70-85% Web display, email preview, smallest size
/ebook 150 40-60% General sharing, email attachments, e-readers
/printer 300 10-30% Desktop printing, internal documents
/prepress 300 5-15% Commercial printing, color-accurate output
/default varies Minimal General-purpose; may increase file size

Note: /default can increase file size on PDFs that were already optimized because it does not force aggressive downsampling. If any preset produces an output file larger than the input, the source PDF is already well-optimized for images. In that case, run cpdf -squeeze input.pdf -o output.pdf instead for a lossless structural pass that removes object overhead without altering image quality.

Ghostscript Compression Example with Before and After File Sizes

ls -lh input.pdf
-rw-r--r-- 1 user user 73M Jan 10 09:00 input.pdf

Run compression:

gs \
  -sDEVICE=pdfwrite \
  -dCompatibilityLevel=1.4 \
  -dPDFSETTINGS=/ebook \
  -dNOPAUSE \
  -dQUIET \
  -dBATCH \
  -sOutputFile=output.pdf \
  input.pdf
ls -lh output.pdf
-rw-r--r-- 1 user user 14M Jan 10 09:01 output.pdf

In this example a 73 MB source PDF becomes 14 MB with /ebook, an 81% reduction.

After compression, open the output file and verify that text is readable and images are acceptable at normal zoom. For scanned documents, check that page edges and fine print are not visibly degraded. If quality is insufficient, rerun the command with /printer instead of /ebook and compare the result.

Method 2: Compress PDF Using ps2pdf and pdf2ps

ps2pdf is the right choice when you want the same Ghostscript compression as Method 1 but prefer a shorter command. It ships as part of the ghostscript package, so no separate install is needed. For standard office documents and export-generated PDFs, it produces identical output to the full gs invocation with the same preset.

How pdf2ps and ps2pdf Work

pdf2ps converts a PDF into a PostScript file, then ps2pdf passes that stream through Ghostscript’s PDF writer to produce a compressed PDF. Most workflows call ps2pdf directly with a -dPDFSETTINGS flag, which skips the intermediate file and runs the same engine in one step.

Step-by-Step Commands

Single-step form:

ps2pdf -dPDFSETTINGS=/ebook input.pdf output.pdf
ls -lh output.pdf
-rw-r--r-- 1 user user 14M Jan 10 09:05 output.pdf

The output size matches the Ghostscript result from Method 1 for the same input file because ps2pdf uses the same underlying engine. The difference is convenience: fewer flags to type for straightforward jobs. For fine-grained control over color profiles, font embedding, or image filters, use the full gs command from Method 1 instead.

Limitations of This Method

  • Complex transparency, soft masks, or layered artwork may not round-trip through PostScript without visual shifts.
  • Vector curves pass through an intermediate representation, which can change subtle geometry compared with direct PDF-to-PDF rewriting.
  • Fine control over color, font embedding, and image filters is narrower than a hand-built gs command line.
  • Interactive forms, JavaScript actions, or multimedia annotations may lose behavior after conversion.

Method 3: Compress PDF Using ImageMagick

Use ImageMagick to rasterize pages at a chosen dpi and JPEG quality when the PDF is already image-like or when you standardize on convert in existing automation.

Install ImageMagick

# Debian / Ubuntu / Linux Mint
sudo apt install imagemagick
convert --version
Version: ImageMagick 6.9.11-60 Q16 x86_64

Warning: On many Debian and Ubuntu systems, ImageMagick’s default security policy blocks PDF read and write operations. Before running the convert command, edit /etc/ImageMagick-6/policy.xml (or /etc/ImageMagick-7/policy.xml) and change the PDF policy line from rights="none" to rights="read|write".

Note: ImageMagick 7 renamed the convert binary to magick. If convert returns a command not found error, run magick convert instead. To check which version is installed, run magick --version or convert --version.

<!-- Change this line: -->
<policy domain="coder" rights="none" pattern="PDF" />

<!-- To this: -->
<policy domain="coder" rights="read|write" pattern="PDF" />

Compress PDF with the convert Command

convert -density 150 input.pdf -quality 85 output.pdf
  • -density 150: treats the input as 150 dpi when rasterizing page content.
  • -quality 85: JPEG quality for compressed image data inside the PDF; lower numbers mean smaller files and more visible artifacts.

Stronger size cut, with more quality loss:

convert -density 100 input.pdf -quality 60 output.pdf
ls -lh output.pdf
-rw-r--r-- 1 user user 11M Jan 10 09:12 output.pdf

For installation options, policy details, and broader examples, read How To Resize Images with ImageMagick on Ubuntu.

When to Use ImageMagick Over Ghostscript

ImageMagick fits image-only PDFs where you already think in terms of dpi and JPEG quality, and you want a short convert line without reading Ghostscript dictionaries. Ghostscript gives preset-based control and tends to preserve text vector paths in mixed documents more predictably. For scripted server work or repeatable command-line pipelines, Ghostscript is the safer default.

Method 4: Compress PDF Using cpdf

Use cpdf when you need to reduce file size without any change to image quality. It targets structural overhead — redundant objects, duplicate streams, and metadata bloat — rather than resampling page content.

Install cpdf on Linux

cpdf is commercial software with a free community edition distributed as a static binary from Coherent Graphics. Download the current Linux build from the official GitHub binaries tree, then place it on your PATH.

wget https://github.com/coherentgraphics/cpdf-binaries/raw/master/Linux-Intel-64bit/cpdf
chmod +x cpdf
sudo mv cpdf /usr/local/bin/
cpdf -version
cpdf Version 2.7

Note: Confirm the latest binary URL on the official cpdf GitHub repository before scripting downloads, paths can move between releases.

Basic cpdf Compression Command

cpdf -squeeze input.pdf -o output.pdf

-squeeze removes redundant objects, unused resources, and duplicate data streams. It does not resample or alter page images, so the visual output is identical to the source. If the file still looks the same but is smaller, the savings came from structural overhead rather than image data.

Note: Size reduction from -squeeze varies significantly by file. PDFs bloated by merged duplicates, unused font streams, or verbose metadata may shrink by 30–50%. Image-heavy scans with no structural redundancy will see minimal change; use Ghostscript with a -dPDFSETTINGS preset for those files.

cpdf Use Cases and Limitations

  • Strong when metadata bloat, duplicate fonts, or unused objects explain most of the bytes.
  • Weak compared with Ghostscript when the PDF is a pile of high-resolution scans, because no dpi change occurs.
  • Useful when stakeholders forbid visible quality change: -squeeze stays lossless relative to source imagery.
  • Pair with Ghostscript later if you still need raster downsampling after a clean structural pass.

For users who prefer a graphical interface, two tools cover most compression workflows without requiring terminal access.

PDF Arranger

Debian / Ubuntu / Linux Mint:

sudo apt install pdfarranger

Fedora, Arch Linux, openSUSE, and other distros (via Flatpak):

flatpak install flathub com.github.jeromerobert.pdfarranger
flatpak run com.github.jeromerobert.pdfarranger

Note: Flatpak must be installed and the Flathub remote must be configured before running the install command. On Fedora, run sudo dnf install flatpak first. On Arch Linux, install flatpak via sudo pacman -S flatpak.

PDF Arranger is a GTK tool for merging, splitting, rotating, and reordering pages. File → Export writes a fresh PDF that often drops duplicated resources from careless merges. Remove blank or redundant pages before export, then pass the result through Ghostscript if you still need smaller images.

LibreOffice Draw Export Settings

Open the PDF in LibreOffice Draw, then follow these steps:

  1. Choose File → Export as PDF.
  2. In the General tab, set Quality to 60–70% for a significant size reduction with acceptable visual output. Leave it at 90% if the document contains charts or fine text that must remain sharp.
  3. In the Images tab, select JPEG compression and set the quality slider to 60–75%. Uncheck Lossless compression if it is enabled.
  4. In the General tab, uncheck Embed standard fonts if the PDF will be viewed on modern systems that already include common fonts.
  5. Click Export, confirm the filename, and save.

Check the output size with ls -lh output.pdf after export. For a typical office-originated PDF, this approach reduces file size by 30–60% without visible degradation at normal reading zoom levels. This method works best for PDFs that originated as office documents with vector text and charts. For pure scanned pages, Ghostscript with /ebook produces better results.

When to Use GUI Methods

  • Local desktop seats where a shell session is inconvenient.
  • One-off sends where automation adds no value.
  • Combined tasks such as crop, reorder, and export in a single sitting.
  • Avoid GUI-only flows on headless servers, CI runners, or automated batch schedules.

How to Compress Scanned PDF Files in Linux

For most scanned PDFs, this Ghostscript command is the right starting point:

gs \
  -sDEVICE=pdfwrite \
  -dCompatibilityLevel=1.4 \
  -dPDFSETTINGS=/ebook \
  -dNOPAUSE \
  -dQUIET \
  -dBATCH \
  -sOutputFile=output_scanned.pdf \
  scanned_input.pdf

The /ebook preset targets 150 dpi, which is sufficient for screen reading and typical office review. If you need to understand why scanned PDFs require a different approach than text-based PDFs, or how to push compression further with DPI and color depth controls, the subsections below cover both.

Why Scanned PDFs Are Larger

Each page is a full-page bitmap at scanner resolution, often 300 dpi to 600 dpi, so file size scales with pixel count and color channels. Without OCR there is no underlying text for font compression to exploit, which is why scanned books blow past text-export PDFs.

Ghostscript Command for Scanned PDFs

The /ebook command shown at the top of this section targets 150 dpi, which is the right starting point for most scans intended for screen reading. Use /screen instead when the smallest possible file size is the priority and some readability loss is acceptable. To go further with explicit DPI control or grayscale conversion, see the subsection below.

Reducing DPI and Color Depth for Scanned Documents

Exact dpi control:

gs \
  -sDEVICE=pdfwrite \
  -dCompatibilityLevel=1.4 \
  -r150 \
  -dNOPAUSE \
  -dQUIET \
  -dBATCH \
  -sOutputFile=output_150dpi.pdf \
  scanned_input.pdf

-r150 fixes the raster resolution at 150 dpi regardless of other PDFSETTINGS interactions, useful when you standardize archive submissions.

Monochrome or gray text scans:

gs \
  -sDEVICE=pdfwrite \
  -dCompatibilityLevel=1.4 \
  -dPDFSETTINGS=/ebook \
  -sColorConversionStrategy=Gray \
  -dProcessColorModel=/DeviceGray \
  -dNOPAUSE \
  -dQUIET \
  -dBATCH \
  -sOutputFile=output_gray.pdf \
  scanned_input.pdf

Color overhead disappears on pages that are visually black ink on white. For monochrome text scans, grayscale output typically removes another 30-50% of bytes compared with keeping a color container.

How to Batch Compress Multiple PDF Files in Linux

Batch compression jobs start by gathering files. For shell structure, quoting, and execution bits, see How To Write a Bash Script. See How To Use Find and Locate to Search for Files on Linux to locate PDFs recursively before you run the loop.

Shell Script for Batch Ghostscript Compression

#!/bin/bash
# batch_compress_pdf.sh
# Compresses all PDF files in the current directory using Ghostscript.
# Usage: ./batch_compress_pdf.sh [output_directory]

PRESET="/ebook"
OUTPUT_DIR="${1:-./compressed}"

mkdir -p "$OUTPUT_DIR"

for input_file in ./*.pdf; do
    [ -f "$input_file" ] || continue

    filename="$(basename "$input_file")"
    output_file="$OUTPUT_DIR/$filename"

    original_size=$(du -sh "$input_file" | cut -f1)

    gs \
      -sDEVICE=pdfwrite \
      -dCompatibilityLevel=1.4 \
      -dPDFSETTINGS="$PRESET" \
      -dNOPAUSE \
      -dQUIET \
      -dBATCH \
      -sOutputFile="$output_file" \
      "$input_file"

    compressed_size=$(du -sh "$output_file" | cut -f1)
    echo "Compressed: $filename | Before: $original_size | After: $compressed_size"
done

echo "Done. Compressed files saved to $OUTPUT_DIR"

Running the Script and Verifying Output Sizes

  1. Make the script executable:

    chmod +x batch_compress_pdf.sh
    
  2. Run from the directory that contains the PDFs:

    ./batch_compress_pdf.sh
    
  3. Example log lines:

    Compressed: report.pdf | Before: 45M | After: 9.2M
    Compressed: invoice.pdf | Before: 12M | After: 2.1M
    Compressed: manual.pdf | Before: 73M | After: 14M
    Done. Compressed files saved to ./compressed
    
  4. Custom output directory:

    ./batch_compress_pdf.sh /home/user/pdf_output
    

Use How To Use Find and Locate to Search for Files on Linux when PDFs live in subdirectories and you need a list before copying them into one folder for this script.

Match the tool to whether the PDF is text-heavy, scan-heavy, or bloated by objects, then pick CLI versus GUI based on where you run the job.

Tool Method Type Compression Control Interface Package Availability Best For
Ghostscript Lossy High (5 presets + flags) CLI All major distros (apt, dnf, pacman, zypper) General compression, scripting, scanned PDFs
ps2pdf Lossy Medium (inherits gs presets) CLI Included with Ghostscript Quick compression, simple one-liners
ImageMagick Lossy Medium (density, quality flags) CLI All major distros Image-only PDFs, existing ImageMagick workflows
cpdf Lossless Low (squeeze only) CLI Manual binary install Lossless deduplication, metadata cleanup
PDF Arranger Lossy Low (export settings) GUI apt / Flatpak (all distros) Desktop one-off compression, page management
LibreOffice Lossy Medium (export dialog) GUI All major distros Office-originated PDFs, desktop workflows

Frequently Asked Questions (FAQ)

Q: What is the best command to reduce PDF file size in Linux?

A: The most reliable method is Ghostscript with the -dPDFSETTINGS=/ebook preset, which balances quality and file size for most use cases. Run: gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf

Q: Does compressing a PDF in Linux reduce its quality?

A: It depends on the preset. The /screen preset reduces image quality noticeably (72 dpi), while /ebook and /printer preserve readable quality. PDFs containing only text typically show no visible quality change after compression.

Q: How do I compress a PDF in Linux Mint specifically?

A: Ghostscript is available in Linux Mint’s default repositories. Install it with sudo apt install ghostscript and use the standard gs command. PDF Arranger is also available via the Software Manager for GUI-based compression.

Q: Can I compress multiple PDF files at once in Linux?

A: Yes. Use a bash for loop or the batch script in the “How to Batch Compress Multiple PDF Files in Linux” section above. The script processes all .pdf files in the current directory and logs before/after sizes to stdout.

Q: How do I reduce the file size of a scanned PDF in Linux?

A: Scanned PDFs are image-heavy. Use Ghostscript with -dPDFSETTINGS=/ebook or the -r150 flag to reduce the output DPI from the original scan resolution. For grayscale text scans, add -sColorConversionStrategy=Gray to reduce file size further.

Q: Is ImageMagick a good alternative to Ghostscript for PDF compression?

A: ImageMagick works for image-only PDFs but provides less control over compression settings than Ghostscript. On Debian and Ubuntu, you must also update the ImageMagick PDF security policy before the convert command will accept PDF input.

Q: How do I check PDF file size before and after compression in Linux?

A: Use ls -lh filename.pdf for a quick human-readable size, or du -sh filename.pdf for disk usage. Run both commands before and after compression to confirm the reduction.

Q: Does Ghostscript work on all major Linux distributions?

A: Yes. Ghostscript is in the default repositories for Debian, Ubuntu, Fedora, Arch Linux, openSUSE, and Linux Mint. The install command varies: apt install ghostscript, dnf install ghostscript, pacman -S ghostscript, or zypper install ghostscript.

Conclusion

Start with pdfinfo and pdfimages -list to understand what is driving the file size, then match the tool to the problem. Ghostscript with /ebook covers most cases. cpdf -squeeze handles the rest without touching image quality. For everything else, the tool comparison table points to the right method.

Creative CommonsThis work is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License.