All posts
Compress PDF via CLI
Rebuild a PDF with ps2pdf using the /ebook preset.
Open online converterPackages to install
ps2pdf ships with ghostscript. Presets control how aggressively images inside the PDF are compressed.
ghostscript
sudo apt-get update
sudo apt-get install -y ghostscriptHow 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 -dPDFSETTINGS=/ebook input.pdf output.pdfDockerfile example
Build a minimal Ubuntu image with the required packages and run the conversion inside the container.
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 -t compress-pdf .
docker run --rm -v "$PWD:/work" -w /work compress-pdf \
ps2pdf -dPDFSETTINGS=/ebook input.pdf output.pdfExample commands
Run these locally after installing the packages, or inside the container from the Dockerfile.
ps2pdf -dPDFSETTINGS=/ebook input.pdf output.pdfps2pdf -dPDFSETTINGS=/screen input.pdf output.pdf