All posts

Compress PDF via CLI

Rebuild a PDF with ps2pdf using the /ebook preset.

Open online converter

Packages to install

ps2pdf ships with ghostscript. Presets control how aggressively images inside the PDF are compressed.

ghostscript
apt install
sudo apt-get update
sudo apt-get install -y ghostscript

How to run the command

ps2pdf -dPDFSETTINGS=/ebook input.pdf output.pdf. /ebook is a screen-friendly default, /screen is smaller, /printer is gentler for print.

ps2pdf
ps2pdf -dPDFSETTINGS=/ebook input.pdf output.pdf

Dockerfile example

Build a minimal Ubuntu image with the required packages and run the conversion inside the container.

Dockerfile
FROM ubuntu:24.04

RUN apt-get update && \
    apt-get install -y --no-install-recommends \
        ghostscript && \
    rm -rf /var/lib/apt/lists/*

WORKDIR /work
docker build && docker run
docker build -t compress-pdf .
docker run --rm -v "$PWD:/work" -w /work compress-pdf \
  ps2pdf -dPDFSETTINGS=/ebook input.pdf output.pdf

Example commands

Run these locally after installing the packages, or inside the container from the Dockerfile.

Example 1
ps2pdf -dPDFSETTINGS=/ebook input.pdf output.pdf
Example 2
ps2pdf -dPDFSETTINGS=/screen input.pdf output.pdf