|
| 1 | +#!/bin/bash - |
| 2 | + |
| 3 | +# This script applies the pdfsizeopt compressor to a pdf. |
| 4 | +# The pdfsizeopt compressor is open source and available at https://github.com/pts/pdfsizeopt. |
| 5 | +# We include it here in a single archive. |
| 6 | + |
| 7 | +# strict error handling |
| 8 | +set -o pipefail # trace ERR through pipes |
| 9 | +set -o errtrace # trace ERR through 'time command' and other functions |
| 10 | +set -o nounset # set -u : exit the script if you try to use an uninitialized variable |
| 11 | +set -o errexit # set -e : exit the script if any statement returns a non-true return value |
| 12 | + |
| 13 | +scriptDir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" |
| 14 | +echo "Script directory is '$scriptDir'." |
| 15 | + |
| 16 | +currentDir="$(pwd)" |
| 17 | +echo "$(date +'%0Y-%0m-%0d %0R:%0S'): We are working in directory: '$currentDir'." |
| 18 | + |
| 19 | +fileIn="$(readlink -f "$1")" |
| 20 | +echo "$(date +'%0Y-%0m-%0d %0R:%0S'): The full input document path is '$fileIn'." |
| 21 | + |
| 22 | +fileOut="$2" |
| 23 | +echo "$(date +'%0Y-%0m-%0d %0R:%0S'): We will filter the input document '$fileIn' to '$fileOut'." |
| 24 | + |
| 25 | +tempDir="$(mktemp -d)" |
| 26 | +echo "$(date +'%0Y-%0m-%0d %0R:%0S'): We will use the temporary directory '$tempDir'." |
| 27 | + |
| 28 | +cd "$tempDir" |
| 29 | + |
| 30 | +echo "$(date +'%0Y-%0m-%0d %0R:%0S'): Unpacking pdfsizeopt to '$tempDir'." |
| 31 | +tar -C "$tempDir" -xf "$scriptDir/pdfsizeopt.tar.xz" |
| 32 | +pdfsizeoptdir="$tempDir/pdfsizeopt" |
| 33 | +chmod 777 -R "$pdfsizeoptdir" |
| 34 | + |
| 35 | +export PATH="$pdfsizeoptdir:$PATH" |
| 36 | +echo "$(date +'%0Y-%0m-%0d %0R:%0S'): PATH is now '$PATH'." |
| 37 | + |
| 38 | +pdfsizeopttemp="$pdfsizeoptdir/pdft" |
| 39 | +mkdir "$pdfsizeopttemp" |
| 40 | +echo "$(date +'%0Y-%0m-%0d %0R:%0S'): pdfsizeopt will use temp dir '$pdfsizeopttemp'." |
| 41 | + |
| 42 | +"$pdfsizeoptdir/pdfsizeopt.single" --do-double-check-type1c-output=yes --tmp-dir="$pdfsizeopttemp" "$fileIn" "$fileOut" |
| 43 | + |
| 44 | +echo "$(date +'%0Y-%0m-%0d %0R:%0S'): Now deleting temporary directory '$tempDir'." |
| 45 | +rm -rf "$tempDir" |
| 46 | + |
| 47 | +echo "$(date +'%0Y-%0m-%0d %0R:%0S'): Successfully finished filtering '$fileIn' to '$fileOut using pdfsizeopt." |
0 commit comments