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