1#!/bin/bash 2 3# Copyright (C) 2014-2016 OpenDevise Inc. and the Asciidoctor Project 4 5# Permission is hereby granted, free of charge, to any person obtaining a copy 6# of this software and associated documentation files (the "Software"), to deal 7# in the Software without restriction, including without limitation the rights 8# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9# copies of the Software, and to permit persons to whom the Software is 10# furnished to do so, subject to the following conditions: 11 12# The above copyright notice and this permission notice shall be included in 13# all copies or substantial portions of the Software. 14 15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21# THE SOFTWARE. 22 23# Optimizes and compresses the specified PDF using Ghostscript (gs). 24# 25# [NOTE] 26# You need at least Ghostscript 9.10 in order for page labels defined in the 27# PDF to be preserved (e.g., front matter pages numbered using roman numerals). 28 29if [ -z $1 ]; then 30 echo "Please supply a PDF file to optimize" 31 exit 1 32fi 33 34if [ -z $GS ]; then 35 GS=gs 36fi 37 38FILE=$1 39FILE_BASENAME=${FILE%.pdf} 40FILE_OPTIMIZED=$FILE_BASENAME-optimized.pdf 41FILE_PDFMARK= 42if [ -f "$FILE_BASENAME.pdfmark" ]; then 43 FILE_PDFMARK="$FILE_BASENAME.pdfmark" 44fi 45DOWNSAMPLE_IMAGES=true 46if [ -z $IMAGE_DPI ]; then 47 #IMAGE_DPI=150 48 IMAGE_DPI=300 49fi 50 51# /prepress defaults (see http://ghostscript.com/doc/current/Ps2pdf.htm) 52# -d{Color,Gray,Mono}ImageDownsampleType=/Bicubic 53# -dAutoFilter{Color,Gray}Images=true 54# -dOptimize=true 55# -dEmbedAllFonts=true 56# -dSubsetFonts=true 57# -dColorConversionStrategy=/LeaveColorUnchanged 58# -dUCRandBGInfo=/Preserve 59# -dCompressPages=true 60# 61# other unused settings 62# -r72 63# 64# for info about pdfmarks, see http://milan.kupcevic.net/ghostscript-ps-pdf 65# 66# to convert to grayscale, add the following (though doesn't always work) 67# 68# -dProcessColorModel=/DeviceGray \ 69# -dColorConversionStrategy=/Gray \ 70 71ERRFILE=$FILE_BASENAME-ERRS.optimize 72"$GS" -q -dNOPAUSE -dBATCH -dSAFER -dNOOUTERSAVE \ 73 -sDEVICE=pdfwrite \ 74 -dPDFSETTINGS=/prepress \ 75 -dPrinted=false \ 76 -dCannotEmbedFontPolicy=/Warning \ 77 -dDownsampleColorImages=$DOWNSAMPLE_IMAGES \ 78 -dColorImageResolution=$IMAGE_DPI \ 79 -dDownsampleGrayImages=$DOWNSAMPLE_IMAGES \ 80 -dGrayImageResolution=$IMAGE_DPI \ 81 -dDownsampleMonoImages=$DOWNSAMPLE_IMAGES \ 82 -dMonoImageResolution=$IMAGE_DPI \ 83 -sOutputFile="$FILE_OPTIMIZED" \ 84 "$FILE" $FILE_PDFMARK 2> $ERRFILE 85 86status=$? 87if test $status -ne 0 ; then 88 echo "$0: $GS return status = $status, aborting" 89elif grep -q Error $ERRFILE ; then 90 echo "$0: $GS succeeded but found Error in $ERRFILE (follows), aborting" 91 echo '---------- Errors from $GS ----------' 92 grep Error $ERRFILE 93 echo '-------------------------------------' 94 status=1 95else 96 rm -f $ERRFILE 97fi 98 99exit $status 100