The Never-Ending PDF Size Struggle#
Whenever I need to upload PDF files to online application sites or anywhere online, I always encounter the dreaded size limit problem. Whether it’s job applications, visa documents, or academic submissions, there’s always that frustrating moment when you see “File size exceeds 5MB limit” or similar messages.
The problem gets exponentially worse when your PDF contains images. A simple document with a few screenshots or scanned pages can easily bloat the file size like 20MB+. I’ve tried various approaches over the years custom filters, macOS’s built-in ColorSync Utility, online compression tools but they sometime work and sometimes don’t. Mac’s in built reduce file size filter is good but it don’t reduce the file size dramatically.
Enter Ghostscript: The Game Changer#
I recently encountered this problem again when I was trying to upload the documents on online system. After trying my usual methods with disappointing results, I decided to give Ghostscript a proper shot. What happened next absolutely blew my mind: it reduced a 16MB PDF to just 866KB without any noticeable loss in quality.
Installation and Usage#
For Mac Users#
Installation via Homebrew#
# Install Ghostscript using Homebrew
brew install ghostscript
Basic Compression Command#
# Basic compression (good balance of size and quality)
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dQUIET -dBATCH -sOutputFile=compressed.pdf input.pdf
Ghostscript offers different quality presets via the -dPDFSETTINGS
parameter: /screen
(smallest size), /ebook
(balanced), /printer
(higher quality), and /prepress
(highest quality). The /screen
setting works best for most online applications and document submissions.
For Windows Users#
I haven’t personally tested Ghostscript on Windows, but the process should be similar:
- Download Ghostscript from the official website
- Install the Windows version
- Use the same commands in Command Prompt or PowerShell, replacing
gs
withgswin64c
orgswin32c
depending on your system
Understanding the Command Parameters#
-sDEVICE=pdfwrite
: Specifies PDF output format-dCompatibilityLevel=1.4
: Sets PDF version for maximum compatibility-dPDFSETTINGS=/screen
: Compression preset (screen, ebook, printer, prepress)-dNOPAUSE
: Doesn’t pause between pages-dQUIET
: Suppresses non-error messages-dBATCH
: Exits after processing-sOutputFile=
: Specifies output filename
Tips for Both Technical and Non-Technical Users#
For Terminal-Comfortable Users#
Create a function in your shell configuration for easy access:
compress_pdf() {
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dQUIET -dBATCH -sOutputFile="${1%.*}_compressed.pdf" "$1"
}
Usage: compress_pdf document.pdf
creates document_compressed.pdf
Enhanced Wrapper Function (Recommended)#
If you want a more robust solution, I’ve created an improved wrapper function that includes error handling, help documentation, quality options, and compression statistics:
compress_pdf() {
# Show help
if [[ "$1" == "help" || "$1" == "--help" || "$1" == "-h" ]]; then
echo "compress_pdf - Compress PDF files using Ghostscript"
echo ""
echo "Usage:"
echo " compress_pdf <input.pdf> [quality]"
echo ""
echo "Quality options (default: screen):"
echo " screen - Smallest size, good for viewing on screen"
echo " ebook - Medium size, good for e-readers"
echo " printer - Good quality for printing"
echo " prepress - Highest quality for professional printing"
echo ""
echo "Examples:"
echo " compress_pdf document.pdf"
echo " compress_pdf document.pdf ebook"
return 0
fi
# Check if file provided
if [[ -z "$1" ]]; then
echo "Error: Please provide a PDF file"
echo "Use: compress_pdf help"
return 1
fi
# Check if file exists
if [[ ! -f "$1" ]]; then
echo "Error: File '$1' not found"
return 1
fi
local input_file="$1"
local quality="${2:-screen}"
local output_file="${input_file%.*}_compressed.pdf"
# Validate quality setting
case "$quality" in
screen|ebook|printer|prepress)
;;
*)
echo "Error: Invalid quality '$quality'. Use: screen, ebook, printer, or prepress"
return 1
;;
esac
echo "Compressing '$input_file' (quality: $quality) -> '$output_file'"
local input_size=$(stat -f%z "$input_file" 2>/dev/null)
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/"$quality" \
-dNOPAUSE -dQUIET -dBATCH -sOutputFile="$output_file" "$input_file"
if [[ -f "$output_file" ]]; then
local output_size=$(stat -f%z "$output_file" 2>/dev/null)
local reduction=$((100 - (output_size * 100 / input_size)))
local input_readable=$(numfmt --to=iec-i --suffix=B "$input_size")
local output_readable=$(numfmt --to=iec-i --suffix=B "$output_size")
echo "Success! Reduced from ${input_readable} to ${output_readable} (${reduction}% smaller)"
else
echo "Error: Compression failed"
return 1
fi
}
This enhanced version provides:
- Built-in help:
compress_pdf --help
- Quality options:
compress_pdf document.pdf ebook
- Error handling for missing/invalid files
- Compression statistics showing size reduction
- Input validation for quality settings
For Non-Technical Users#
- Start with the
/screen
preset - it provides the best compression for most use cases - Always keep your original file as a backup
- Test the compressed file to ensure it meets your quality requirements