-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpdf-fixup
More file actions
executable file
·344 lines (313 loc) · 8.11 KB
/
pdf-fixup
File metadata and controls
executable file
·344 lines (313 loc) · 8.11 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#!/bin/bash
# SPDX-FileCopyrightText: 2021 - 2024 sudorook <daemon@nullcodon.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <https://www.gnu.org/licenses/>.
set -euo pipefail
ROOT="$(dirname "${0}")"
source "${ROOT}"/globals
! check_command awk pdftk pdftoppm pdfinfo convert && exit 3
function adjust_page_numbers {
local offset
local length
offset=$((START - 1))
awk -i inplace -v s="${START}" -v e="${END}" \
-f <(cat << EOF
{
if (a ~ "InfoBegin") {
getline c
while (c !~ "InfoValue") {
if (getline c == 0) break
}
a=c
}
else if (a ~ "PageMediaBegin") {
if (e == "end") {
if (\$2 >= s) {
print a
print
getline c
while (c !~ "Begin") {
print c
if (getline c == 0) break
}
a=c
} else {
getline c
while (c !~ "Begin") {
if (getline c == 0) break
}
a=c
}
}
else {
if ((\$2 >= s) && (\$2 <= e)) {
print a
print
getline c
while (c !~ "Begin") {
print c
if (getline c == 0) break
}
a=c
} else {
getline c
while (c !~ "Begin") {
if (getline c == 0) break
}
a=c
}
}
}
else if (a ~ "PageLabelBegin") {
b=\$0
if (e == "end") {
if (\$2 >= s) {
print a
print b
getline c
while (c !~ "NumStyle") {
print c
if (getline c == 0) break
}
print c
getline a
} else {
getline c
while (c !~ "NumStyle") {
if (getline c == 0) break
}
getline a
}
}
else {
if ((\$2 >= s) && (\$2 <= e)) {
print a
print b
getline c
while (c !~ "NumStyle") {
print c
if (getline c == 0) break
}
print c
getline a
} else {
getline c
while (c !~ "NumStyle") {
if (getline c == 0) break
}
getline a
}
}
}
else if (a ~ "BookmarkBegin") {
b=\$0
getline c
getline
if (e == "end") {
if (\$2 >= s) {
print a; print b; print c; print;
}
}
else {
if ((\$2 >= s) && (\$2 <= e)) {
print a; print b; print c; print;
}
}
a=\$0
# getline a
}
else {
if (\$1 !~ "Begin\$") {
print
}
a=\$0
}
}
EOF
) "${DATA}"
sed -i \
-e '/^PageMedia\(Rect\|Dimensions\):/s/,//g' \
-e 's/-0.[0-9]\+/0/g' "${DATA}"
# Offset the page number metadata
awk -i inplace -v d="${offset}" '/BookmarkPageNumber:/{$2-=d}1' "${DATA}"
awk -i inplace -v d="${offset}" '/PageMediaNumber:/{$2-=d}1' "${DATA}"
awk -i inplace -v d="${offset}" '/PageLabelNewIndex:/{$2-=d}1' "${DATA}"
if [[ "${END}" = end ]]; then
length="$(sed -n "s/^NumberOfPages: \([0-9]\+\)$/\1/p" "${DATA}")"
length="$((length - offset))"
sed -i "s/^NumberOfPages: \([0-9]\+\)$/NumberOfPages: ${length}/g" "${DATA}"
else
length="$((END - offset))"
sed -i "s/^NumberOfPages: \([0-9]\+\)$/NumberOfPages: ${length}/g" "${DATA}"
fi
}
function clean_meta_tags {
sed -i \
-e "/InfoBegin/,+2d" \
-e "/PdfID0/d" \
-e "/PdfID1/d" \
-e "s///g" \
-e "/^$/d" \
-e "/^BookmarkTitle:/N;s/\n\?\(\nBookmarkLevel.*\)\?/\1/g" \
-e "/^BookmarkTitle:/s/\t\+/\ /g" \
-e "s/\(\ \ \+\)/\ /g" \
"${DATA}"
}
function compare_page_dimensions {
local in="${1}"
local p1="${2}"
local p2="${3}"
local p1_size
local p2_size
p1_size="$(pdfinfo -f "${p1}" -l "${p1}" "${1}" |
sed -n "s/^Page.*size:\ \+\(.*\)/\1/p")"
p2_size="$(pdfinfo -f "${p2}" -l "${p2}" "${1}" |
sed -n "s/^Page.*size:\ \+\(.*\)/\1/p")"
[[ "${p1_size}" = "${p2_size}" ]] && return 0 || return 1
}
function normalize_cover {
# Assumes the first page is the cover and the reference page is the 10th one.
local in
local dir="${BUILD}"
local p01="${START}"
local p01_file
# local p01_size
local p10="$((START + 10))"
local p10_file
local p10_size
local pend="${END}"
local pend_file
if compare_page_dimensions "${TMP}" 1 10; then
show_info "No page normalization needed."
else
show_info "Different cover dimensions detected. Normalizing..."
pushd "${dir}" > /dev/null
in="$(basename "${TMP}")"
p01_file="${in%.*}-01"
p10_file="${in%.*}-10"
pend_file="${in%.*}-$((p01 + 1))_end"
pdftk "${TMP}" cat "${p01}" output "${p01_file}.pdf"
pdftk "${TMP}" cat "${p10}" output "${p10_file}.pdf"
pdftk "${TMP}" cat "$((p01 + 1))-${pend}" output "${pend_file}.pdf"
pdftoppm "${p01_file}.pdf" "${p01_file}" -png -singlefile
pdftoppm "${p10_file}.pdf" "${p10_file}" -png -singlefile
# p01_size="$(file -b "${p01_file}.png" | cut -d "," -f 2 | tr -d "[:space:]")"
p10_size="$(file -b "${p10_file}.png" | cut -d "," -f 2 | tr -d "[:space:]")"
convert "${p01_file}.png" -resize "${p10_size}!" "${p01_file}v2.png"
convert "${p01_file}v2.png" "${p01_file}v2.pdf"
pdftk "${p01_file}v2.pdf" "${pend_file}.pdf" cat output "${in}"
popd > /dev/null
fi
}
function print_usage {
show_header "Usage: pdf-fixup"
show_listitem " -s|--start first page number (default: 1)"
show_listitem " -e|--end last page number (default: end)"
show_listitem " -k|--keep flag to keep intermediate files (default: false)"
show_listitem " -o|--output output file name (optional)"
show_listitem " -n|--normalize resize 1st page to match others (default: false)"
}
START=1
END=end
KEEP=false
NORMALIZE=false
OPTIONS=e:hkno:s:
LONGOPTIONS=end:,help,keep,normalize,output:,start:
PARSED=$(getopt -o "${OPTIONS}" --long "${LONGOPTIONS}" -n "${0}" -- "${@}")
eval set -- "${PARSED}"
while [ ${#} -ge 1 ]; do
case ${1} in
-e | --end)
END="${2}"
shift 2
;;
-h | --help)
print_usage
exit
;;
-k | --keep)
KEEP=true
shift 1
;;
-o | --output)
OUTPUT="${2}"
shift
;;
-s | --start)
START="${2}"
shift 2
;;
-n | --normalize)
NORMALIZE=true
shift 1
;;
--)
shift
break
;;
*)
show_error "ERROR: invalid flag."
exit 3
;;
esac
done
if [[ ${#} -ne 1 ]]; then
show_error "ERROR: please specify 1 input file."
exit 3
else
if ! [ -f "${1}" ]; then
show_error "ERROR: input '${1}' not found. Exiting."
exit 3
fi
if ! [[ "$(file -b "${1}")" =~ ^PDF ]]; then
show_error "ERROR: input '${1}' is not a PDF. Exiting."
exit 3
fi
INPUT="${1}"
NAME=${1%.*}
EXTENSION=${1##*.}
fi
if ! [[ -v OUTPUT ]]; then
OUTPUT="${NAME} new.${EXTENSION}"
fi
BUILD="$(mktemp -d)"
TMP="${BUILD}/tmp-$$.pdf"
DATA="${BUILD}/data-$$.txt"
trap 'rm -rf "${BUILD}"; exit' INT TERM ERR EXIT
# Force UTF-8 locale, if possible.
LANG="$(locale -a | grep "utf-\?8" | head -n 1)"
if [ -n "${LANG}" ]; then
export LC_ALL="${LANG}"
else
show_warning "Failed to force UTF-8 locale."
fi
pdftk "${INPUT}" cat "${START}"-"${END}" output "${TMP}"
pdftk "${INPUT}" dump_data_utf8 output "${DATA}"
clean_meta_tags
adjust_page_numbers
"${NORMALIZE}" && normalize_cover
if ! ${KEEP}; then
pdftk "${TMP}" update_info_utf8 "${DATA}" output "${OUTPUT}"
rm -f "${TMP}" "${DATA}"
else
mv "${TMP}" "$(basename "${TMP}")"
mv "${DATA}" "$(basename "${DATA}")"
TMP="$(basename "${TMP}")"
DATA="$(basename "${DATA}")"
show_info "To re-assemble, run:" >&2
show_listitem "pdftk ${TMP@Q} update_info_utf8 ${DATA@Q} output ${OUTPUT@Q} && rm ${TMP@Q} ${DATA@Q}" >&2
fi