This Bash script compresses PDF files using Ghostscript, with three levels of quality to choose from.
Itβs ideal for reducing file sizes while maintaining decent readability and quality β perfect for archiving, emailing, or uploading documents.
- Compress any
.pdffile directly from the terminal - Choose between 3 compression levels
- Displays compression progress with readable and colorized output
- Prevents overwriting: auto-increments output filenames
- Works seamlessly with macOS and Linux
- Ghostscript (must be installed)
To install it:
# macOS (with Homebrew)
brew install ghostscript
# Ubuntu/Debian
sudo apt install ghostscriptcompress /path/to/file.pdf [quality]file.pdf: Required β the path to the PDF file to compressquality: Optional β choose a value between1,2, or31β Less compression, High quality (downsampling images)2β Recommended compression, Good quality (default)3β Extreme compression, Lower quality
π‘ If no quality is given or an invalid value is entered, it defaults to 2.
compress ~/Documents/Report.pdf 2Output:
β³ Starting compression of "Report.pdf" with quality setting: 2 (Recommended compression, Good quality)...
β
Compressed file: Report_compressed.pdf | Original size: 6.3M | Compressed size: 452K
You can create an alias to use compress from anywhere in your terminal:
- Open your
.zshrc:
nano ~/.zshrc- Add the following line (update the path if needed):
alias compress="/full/path/to/your/script.sh"For example:
alias compress="/Users/yourname/scripts/pdf_compress.sh"- Save and reload your terminal:
source ~/.zshrcThen just run:
compress myfile.pdf 2The compressed file is saved in the same folder as the original, with _compressed.pdf (or _compressed_2.pdf, etc.) appended to the filename.
MIT License β do whatever you want, just give credit if you reuse parts of the code.