Skip to content

4tocall/PDF-Compression-Script-Bash

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

πŸ“„ PDF Compression Script (Bash)

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.


βœ… Features

  • Compress any .pdf file 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

βš™οΈ Requirements

  • Ghostscript (must be installed)

To install it:

# macOS (with Homebrew)
brew install ghostscript

# Ubuntu/Debian
sudo apt install ghostscript

πŸ› οΈ Usage

compress /path/to/file.pdf [quality]
  • file.pdf: Required – the path to the PDF file to compress
  • quality: Optional – choose a value between 1, 2, or 3
    • 1 – 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.


πŸ§ͺ Example

compress ~/Documents/Report.pdf 2

Output:

⏳ 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

πŸ’‘ Zsh Alias (Optional)

You can create an alias to use compress from anywhere in your terminal:

  1. Open your .zshrc:
nano ~/.zshrc
  1. 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"
  1. Save and reload your terminal:
source ~/.zshrc

Then just run:

compress myfile.pdf 2

πŸ“ Output

The compressed file is saved in the same folder as the original, with _compressed.pdf (or _compressed_2.pdf, etc.) appended to the filename.


🧩 License

MIT License – do whatever you want, just give credit if you reuse parts of the code.

About

Compress any .pdf file directly from the terminal

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages