Created
March 28, 2017 09:37
-
-
Save janhoy/bfb1bed6a7ef6fd782ba9132ac5baab2 to your computer and use it in GitHub Desktop.
optimize-pdf with fallback to dockerized gs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Optimizes and compresses the specified PDF using Ghostscript (gs). | |
# If gs is not found on path, will attempt with dockerized gs | |
# | |
# [NOTE] | |
# You need at least Ghostscript 9.10 in order for page labels defined in the | |
# PDF to be preserved (e.g., front matter pages numbered using roman numerals). | |
if [ -z $1 ]; then | |
echo "Please supply a PDF file to optimize" | |
exit 1 | |
fi | |
FILE=$1 | |
FILE_DIR="$(dirname $FILE)" | |
FILE_BARE="$(basename $FILE)" | |
if [[ $(gs --version 2>/dev/null) =~ (9\.[1-9][0-9]|1[0-9]) ]] ; then | |
GS=gs | |
elif [[ $(docker run holmes/ghostscript:9.15 2>/dev/null) == "9.15" ]] ; then | |
GS="docker run -v $FILE_DIR:/solr holmes/ghostscript:9.15" | |
FILE_DIR=/solr | |
else | |
echo "Could not find Ghostscript or Docker, PDF not optimized" | |
exit 1 | |
fi | |
FILE_BASENAME=${FILE_BARE%.pdf} | |
FILE="$FILE_DIR/${FILE_BARE}" | |
FILE_OPTIMIZED="$FILE_DIR/${FILE_BASENAME}-optimized.pdf" | |
FILE_PDFMARK= | |
if [ -f "$FILE_BASENAME.pdfmark" ]; then | |
FILE_PDFMARK="$FILE_DIR/${FILE_BASENAME}.pdfmark" | |
fi | |
DOWNSAMPLE_IMAGES=true | |
if [ -z $IMAGE_DPI ]; then | |
IMAGE_DPI=300 | |
fi | |
echo "Reducing file size of $FILE using $GS, producing $FILE_OPTIMIZED" | |
$GS -q -dNOPAUSE -dBATCH -dSAFER -dNOOUTERSAVE \ | |
-sDEVICE=pdfwrite \ | |
-dPDFSETTINGS=/prepress \ | |
-dCannotEmbedFontPolicy=/Warning \ | |
-dDownsampleColorImages=$DOWNSAMPLE_IMAGES \ | |
-dColorImageResolution=$IMAGE_DPI \ | |
-dDownsampleGrayImages=$DOWNSAMPLE_IMAGES \ | |
-dGrayImageResolution=$IMAGE_DPI \ | |
-dDownsampleMonoImages=$DOWNSAMPLE_IMAGES \ | |
-dMonoImageResolution=$IMAGE_DPI \ | |
-sOutputFile="$FILE_OPTIMIZED" \ | |
"$FILE" $FILE_PDFMARK | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment