Skip to content

Instantly share code, notes, and snippets.

@ilosamart
Forked from slowkow/pdfoptim.sh
Created April 27, 2023 22:15
Show Gist options
  • Save ilosamart/38e87064fba1c98b653b21ee3f865a7e to your computer and use it in GitHub Desktop.
Save ilosamart/38e87064fba1c98b653b21ee3f865a7e to your computer and use it in GitHub Desktop.
Optimize PDF files with Ghostscript.
#!/usr/bin/env bash
# pdfoptim.sh
#
# Optimize a PDF with Ghostscript
#
# Usage
# -----
#
# bash pdfoptim.sh input.pdf output.pdf
#
# For example, a PDF file can be reduced from 136.8 MB to 41.2 MB without losing details.
if [[ "$#" -ne 2 ]]; then
echo "usage: $0 in.pdf out.pdf"
exit 1
fi
dpi=300
downsample=0.8
in_file="$1"
out_file="$2"
gs \
-o"${out_file}" \
-sDEVICE=pdfwrite \
-dDownsampleColorImages=true \
-dDownsampleGrayImages=true \
-dDownsampleMonoImages=true \
-dColorImageResolution=${dpi} \
-dGrayImageResolution=${dpi} \
-dMonoImageResolution=${dpi} \
-dColorImageDownsampleThreshold=${downsample} \
-dGrayImageDownsampleThreshold=${downsample} \
-dMonoImageDownsampleThreshold=${downsample} \
"${in_file}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment