best way to compress pdf offline from terminal
- User
best way to compress pdf offline from terminal
- Assistant
Compressing a PDF offline from the terminal is a common task, especially on Linux and macOS systems. Here are some of the best methods using popular command-line tools:
1. Using
ghostscript(gs)Ghostscript is a powerful tool for PDF processing and is available on most Unix-like systems.
Install Ghostscript
- Ubuntu/Debian:
sudo apt-get install ghostscript - macOS (Homebrew):
brew install ghostscript
Compress PDF Command
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook \ -dNOPAUSE -dQUIET -dBATCH \ -sOutputFile=output.pdf input.pdfPDFSETTINGS options:
/screen(lowest quality, smallest size)/ebook(medium quality, good compression)/printer(high quality)/prepress(highest quality)/default(a balance)
Example:
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen \ -dNOPAUSE -dQUIET -dBATCH \ -sOutputFile=compressed.pdf original.pdf
2. Using
qpdfQPDF is another lightweight tool for PDF manipulation.
Install QPDF
- Ubuntu/Debian:
sudo apt-get install qpdf - macOS (Homebrew):
brew install qpdf
Compress PDF Command
qpdf --linearize input.pdf output.pdfNote: QPDF is mainly for linearizing and optimizing PDFs, not as aggressive as Ghostscript for compression.
3. Using
pdftkPDFTK can also compress PDFs, but not as effectively as Ghostscript.
Install PDFTK
- Ubuntu/Debian:
sudo apt-get install pdftk - macOS (Homebrew):
brew install pdftk-java
Compress PDF Command
pdftk input.pdf output output.pdf compress
4. Using
mutool(from MuPDF)MuPDF's
mutoolcan also compress PDFs.Install MuPDF
- Ubuntu/Debian:
sudo apt-get install mupdf-tools - macOS (Homebrew):
brew install mupdf
Compress PDF Command
mutool clean -gg -d input.pdf output.pdf
Summary Table
Tool Command Example Best For Ghostscript gs -sDEVICE=pdfwrite -dPDFSETTINGS=/ebook -sOutputFile=out.pdf in.pdfBest compression QPDF qpdf --linearize in.pdf out.pdfFast, simple, less compression PDFTK pdftk in.pdf output out.pdf compressBasic compression MuPDF mutool clean -gg -d in.pdf out.pdfGood for cleaning PDFs
Recommendation
Ghostscript is generally the best for strong compression and flexibility.
For most users, this command is the go-to:gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook \ -dNOPAUSE -dQUIET -dBATCH \ -sOutputFile=compressed.pdf original.pdf
Tip: Always check the output quality to ensure it meets your needs!
Let me know if you need a script or want to batch-compress multiple PDFs. - Ubuntu/Debian:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.