Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save brontolinux/0e44ee6741605408d88d14712533825a to your computer and use it in GitHub Desktop.
Save brontolinux/0e44ee6741605408d88d14712533825a to your computer and use it in GitHub Desktop.
Compress PDF files with ghostscript

This can reduce files to ~15% of their size (2.3M to 345K, in one case) with no obvious degradation of quality.

ghostscript -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/printer -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf

Other options for PDFSETTINGS:

  • /screen selects low-resolution output similar to the Acrobat Distiller "Screen Optimized" setting.
  • /ebook selects medium-resolution output similar to the Acrobat Distiller "eBook" setting.
  • /printer selects output similar to the Acrobat Distiller "Print Optimized" setting.
  • /prepress selects output similar to Acrobat Distiller "Prepress Optimized" setting.
  • /default selects output intended to be useful across a wide variety of uses, possibly at the expense of a larger output file.

Source: http://ghostscript.com/doc/current/Ps2pdf.htm

@brontolinux
Copy link
Author

My take: shell function

#!/bin/bash

function pdfcompress
{
	if [ $# -ne 3 ]
	then
		echo "Usage: pdfcompress level input.pdf output.pdf"
		return 1
	fi

	LEVEL=$1
	INPUT=$2
	OUTPUT=$3


	case $LEVEL in
		screen | ebook | printer | prepress | default )
			ghostscript \
				-sDEVICE=pdfwrite \
				-dCompatibilityLevel=1.4 \
				-dPDFSETTINGS=/${LEVEL} \
				-dNOPAUSE \
				-dQUIET \
				-dBATCH \
				-sOutputFile="${OUTPUT}" \
				"${INPUT}" ;;

		* )
			echo "Invalid level ${LEVEL}"
			echo "Valid levels are screen, ebook, printer, prepress, default"
			return 2 ;;
	esac
}

pdfcompress "$@"

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