A command-line tool to compress PDF files while maintaining reasonable quality.
PDF-Shrinker is a Node.js command-line utility that leverages Ghostscript to compress PDF files efficiently. It offers multiple compression levels, allowing you to balance file size reduction with output quality based on your specific needs.
- 🗜️ Multiple compression levels (from light to maximum)
- 📊 Real-time progress tracking
- 📏 Detailed compression statistics (before/after sizes, ratio)
- 🎮 Simple command-line interface
- 🚀 Fast processing
- 📝 Verbose mode for debugging
- Node.js (version 12 or later)
- Ghostscript (must be installed and available in your PATH)
# Clone the repository
git clone https://github.com/msoltanov/pdf-shrinker.git
cd pdf-shrinker
# Install dependencies
npm install
# Make the script executable
chmod +x index.jsFor offline/manual installation, download the following .tgz packages from the npm registry:
-
Download these packages from npmjs.com:
-
Copy the downloaded .tgz files to your project directory
-
Install each package manually:
npm install ./chalk-4.1.2.tgz
npm install ./cli-progress-3.12.0.tgz
npm install ./commander-13.1.0.tgzNote: Some packages might have dependencies that need to be installed separately. Check package.json inside each .tgz file for required dependencies.
Ensure Ghostscript is installed by running:
gs --versionIf not installed, you can install it using:
- Ubuntu/Debian:
sudo apt-get install ghostscript - macOS:
brew install ghostscript - Windows: Download from Ghostscript website
pdf-shrinker -i input.pdf -o output.pdfIf installed locally, run:
./index.js -i input.pdf -o output.pdf| Option | Description |
|---|---|
-i, --input <file> |
Input PDF file (required) |
-o, --output <file> |
Output PDF file (optional, defaults to input-compressed.pdf) |
-l, --level <1-5> |
Compression level (optional, defaults to 3) |
-v, --verbose |
Show detailed processing information |
-h, --help |
Display help information |
PDF-Shrinker offers four compression levels:
- Best quality output
- Minimal file size reduction
- Ideal for: Documents with many high-quality images that must remain high-resolution
- Good quality output
- Moderate file size reduction
- Ideal for: General purpose documents with mixed content
- Average quality but still acceptable for most purposes
- Significant file size reduction
- Ideal for: Documents that need to be emailed or stored with limited space
- Reduced quality but still acceptable for most purposes
- Significant file size reduction
- Ideal for: Documents that need to be emailed or stored with limited space
- Lowest quality
- Maximum file size reduction
- Ideal for: Archiving, when file size is critical and some quality loss is acceptable
pdf-shrinker -i large-document.pdf -o compressed-document.pdfpdf-shrinker -i large-document.pdf -o high-quality-compressed.pdf -l 1pdf-shrinker -i large-document.pdf -o max-compressed.pdf -l 5pdf-shrinker -i large-document.pdf -o compressed-document.pdf -vBased on testing, you can expect approximately:
- Level 1: 20-40% reduction in file size
- Level 2: 40-60% reduction in file size
- Level 3: 60-70% reduction in file size
- Level 4: 70-80% reduction in file size
- Level 5: 80-95% reduction in file size
Actual results may vary depending on the content of your PDF files.
- "Command not found": Ensure the tool is installed globally or you're running from the correct directory
- "Ghostscript not found": Make sure Ghostscript is installed and added to your PATH
- "Permission denied": Make sure the script is executable (
chmod +x index.js) - "Error: Input file not found": Check that the input file path is correct
If you encounter any issues, please:
- Run in verbose mode (
-v) to see detailed information - Check that Ghostscript is properly installed
- Ensure you have read/write permissions for the input and output locations
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.