Skip to content

Instantly share code, notes, and snippets.

@KimTholstorf
Last active February 9, 2022 13:14
Show Gist options
  • Save KimTholstorf/17b675b20426e08294cf7028809d188a to your computer and use it in GitHub Desktop.
Save KimTholstorf/17b675b20426e08294cf7028809d188a to your computer and use it in GitHub Desktop.
macOS and Linux PDC compesssion CLI

Simple shell function that use ghostscript to reduce the size of a PDF file.

On macOS install Homebrew. On linux use native packamanager.

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Install the ghostscript package brew install ghostscript or apt/tdnf/whatever install ghostscript

For ZSH edit $HOME/.zshrc and for Bash edit $HOME/.bash_profile and put this function at the bottom:

# Usage: pdfcompress [input pdf] [output pdf]
pdfcompress() {
    gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dPDFSETTINGS=/${3:-"screen"} -dCompatibilityLevel=1.4 -sOutputFile="$2" "$1"
}

Run with pdfcompress input.pdf output.pdf in Bash or ZSH.

In my experience this will reduce the original PDF with 66-75% without any significant loss of quality.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment