-
Notifications
You must be signed in to change notification settings - Fork 1
/
shrinkpdf.sh
175 lines (157 loc) · 5.02 KB
/
shrinkpdf.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/bin/sh
# https://github.com/aklomp/shrinkpdf
# Licensed under the 3-clause BSD license:
#
# Copyright (c) 2014-2022, Alfred Klomp
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# 3. Neither the name of the copyright holder nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
shrink ()
{
if [ "$grayscale" = "YES" ]; then
gray_params="-sProcessColorModel=DeviceGray \
-sColorConversionStrategy=Gray \
-dOverrideICC"
else
gray_params=""
fi
# Allow unquoted variables; we want word splitting for $gray_params.
# shellcheck disable=SC2086
gs \
-q -dNOPAUSE -dBATCH -dSAFER \
-sDEVICE=pdfwrite \
-dCompatibilityLevel="$4" \
-dPDFSETTINGS=/screen \
-dEmbedAllFonts=true \
-dSubsetFonts=true \
-dAutoRotatePages=/None \
-dColorImageDownsampleType=/Bicubic \
-dColorImageResolution="$3" \
-dGrayImageDownsampleType=/Bicubic \
-dGrayImageResolution="$3" \
-dMonoImageDownsampleType=/Subsample \
-dMonoImageResolution="$3" \
-sOutputFile="$2" \
${gray_params} \
"$1"
}
get_pdf_version ()
{
# $1 is the input file. The PDF version is contained in the
# first 1024 bytes and will be extracted from the PDF file.
pdf_version=$(cut -b -1024 "$1" | awk 'BEGIN { found=0 }{ if (match($0, "%PDF-[0-9]\\.[0-9]") && ! found) { print substr($0, RSTART + 5, 3); found=1 } }')
if [ -z "$pdf_version" ] || [ "${#pdf_version}" != "3" ]; then
return 1
fi
}
check_input_file ()
{
# Check if the given file exists.
if [ ! -f "$1" ]; then
echo "Error: Input file does not exist." >&2
return 1
fi
}
check_smaller ()
{
# If $1 and $2 are regular files, we can compare file sizes to
# see if we succeeded in shrinking. If not, we copy $1 over $2:
if [ ! -f "$1" ] || [ ! -f "$2" ]; then
return 0;
fi
ISIZE="$(wc -c "$1" | awk '{ print $1 }')"
OSIZE="$(wc -c "$2" | awk '{ print $1 }')"
if [ "$ISIZE" -lt "$OSIZE" ]; then
echo "Input smaller than output, doing straight copy" >&2
cp "$1" "$2"
fi
}
check_overwrite ()
{
# If $1 and $2 refer to the same file, then the file would get
# truncated to zero, which is unexpected. Abort the operation.
# Unfortunately the stronger `-ef` test is not in POSIX.
if [ "$1" = "$2" ]; then
echo "The output file is the same as the input file. This would truncate the file." >&2
echo "Use a temporary file as an intermediate step." >&2
return 1
fi
}
usage ()
{
echo "Reduces PDF filesize by lossy recompressing with Ghostscript."
echo "Not guaranteed to succeed, but usually works."
echo
echo "Usage: $1 [-g] [-h] [-o output] [-r res] infile"
echo
echo "Options:"
echo " -g Enable grayscale conversion which can further reduce output size."
echo " -h Show this help text."
echo " -o Output file, default is standard output."
echo " -r Resolution in DPI, default is 72."
}
# Set default option values.
grayscale=""
ofile="-"
res="72"
# Parse command line options.
while getopts ':hgo:r:' flag; do
case $flag in
h)
usage "$0"
exit 0
;;
g)
grayscale="YES"
;;
o)
ofile="${OPTARG}"
;;
r)
res="${OPTARG}"
;;
\?)
echo "invalid option (use -h for help)"
exit 1
;;
esac
done
shift $((OPTIND - 1))
# An input file is required.
if [ -z "$1" ]; then
usage "$0"
exit 1
else
ifile="$1"
fi
# Check if input file exists
check_input_file "$ifile" || exit $?
# Check that the output file is not the same as the input file.
check_overwrite "$ifile" "$ofile" || exit $?
# Get the PDF version of the input file.
get_pdf_version "$ifile" || pdf_version="1.5"
# Shrink the PDF.
shrink "$ifile" "$ofile" "$res" "$pdf_version" || exit $?
# Check that the output is actually smaller.
check_smaller "$ifile" "$ofile"