Compressing a PDF offline from the terminal can be done using several tools. Here are the best methods:
1. Using Ghostscript (gs)
Ghostscript is a powerful tool for manipulating PDFs and can significantly reduce file size by downsampling images and optimizing the PDF structure.
Install Ghostscript
- Linux (Debian/Ubuntu):
sh
sudo apt install ghostscript
- Mac (Homebrew):
sh
brew install ghostscript
- Windows (Chocolatey):
sh
choco install ghostscript
Compress PDF with Ghostscript
sh
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf
-dPDFSETTINGS Options:
/screen (lowest quality, smallest file)
/ebook (medium quality, good compression)
/printer (high quality, larger file)
/prepress (highest quality, largest file)
Advanced Compression (Custom DPI & Compression)
sh
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dDownsampleColorImages=true -dColorImageResolution=150 -dColorImageDownsampleType=/Bicubic -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf
2. Using pdf2ps + ps2pdf (Alternative Method)
If Ghostscript is not available, you can use pdf2ps and ps2pdf (part of Ghostscript).
sh
pdf2ps input.pdf temp.ps
ps2pdf -dPDFSETTINGS=/ebook temp.ps output.pdf
rm temp.ps
3. Using qpdf (Lossless Compression)
qpdf is a lightweight tool for lossless PDF compression (removes redundant data without quality loss).
Install qpdf
- Linux (Debian/Ubuntu):
sh
sudo apt install qpdf
- Mac (Homebrew):
sh
brew install qpdf
Compress PDF with qpdf
sh
qpdf --stream-data=compress input.pdf output.pdf
4. Using pdfsizeopt (Advanced Optimization)
pdfsizeopt is a Python-based tool that optimizes PDFs by compressing images and removing unnecessary metadata.
Install pdfsizeopt
sh
pip install pdfsizeopt
Compress PDF with pdfsizeopt
sh
pdfsizeopt input.pdf output.pdf
Summary of Best Methods
| Method | Compression Type | Best For |
|---|
Ghostscript (gs) | Lossy (adjustable) | Best for high compression |
qpdf | Lossless | Best for preserving quality |
pdfsizeopt | Advanced optimization | Best for fine-tuning |
For maximum compression, use Ghostscript with /screen or /ebook settings.
For lossless compression, use qpdf.
Would you like help with a specific use case?