Compress PDF files using GhostScript
This post describes method how to automate process of compressing PDF files in Mac or Linux environments using GhostScript library.
First of all you need to install GhostScript library on your computer. On mac os you can do it with brew:
brew install ghostscriptOr using apt on Linux systems
sudo apt update
sudo apt install ghostscriptNow you can do converting using gs command in terminal. There are a lot of options, but here I would give you my solution.
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/default \
-dNOPAUSE -dQUIET -dBATCH -dDetectDuplicateImages \
-dCompressFonts=true -r150 -sOutputFile=output.pdf input.pdfIf you would like to make shortcut in your terminal to start this commands quicker, you can add this code to your ~/.zshrc file.
nano ~/.zshrcpdfcompress ()
{
mkdir -p compressed
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/default \
-dNOPAUSE -dQUIET -dBATCH -dDetectDuplicateImages \
-dCompressFonts=true -r150 -sOutputFile=compressed/$1 $1
}
pdfcompressall (){
for i in $(ls | grep .pdf); do pdfcompress "$i"; done
}This will allow you call GhostScript command as pdfcompress name_of_pdf.pdf to compress one pdf file, or using pdfcompressall to compress all pdf files in current folder.
