Compress (bulk) pdf with Ghostcript

Table of Contents

One liner with ghostscript GS to make your pdf much smaller. Options for PDFSETTINGS are (in rank of output quality):

  1. /prepress
  2. /ebook
  3. /screen
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/prepress -dNOPAUSE -dQUIET -dBATCH -sOutputFile=compressed_PDF_file.pdf input_PDF_file.pdf

En Masse Compression: 972M -> 457M

I applied this with a script upon my 972M directory of hundreds of research pdfs, and the end result was less than half the size (457M). I converted each PDF to text, which is useful for most of them, and then moved each PDF to an “uncompressed” directory, then compressed that uncompressed/FILE. This is because ghostscript actively uses its File IO stream while it is compressing, so it can’t actually do overwrites. I can then inspect the results and delete the uncompressed dir. All the files I checked were actually imperceptibly different in quality, despite being a fraction the size.

#!/bin/bash
adir="uncompressed";
mkdir $adir;
for f in $(ls *.pdf)
do
    af="$adir/$f";

    pdftotext "$f" &&
    mv $f $af &&
    echo "did pdftotext on $f" &&
    gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dBATCH -dQUIET -sOutputFile="$f" $af &&
    echo "Compressed pdf for $f";    
done
exit 0;

Resources

https://itsfoss.com/compress-pdf-linux/

Tory Anderson avatar
Tory Anderson
Full-time Web App Engineer, Digital Humanist, Researcher, Computer Psychologist