-
Notifications
You must be signed in to change notification settings - Fork 2
/
pdf-split
executable file
·115 lines (101 loc) · 3.02 KB
/
pdf-split
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
#!/bin/bash
# SPDX-FileCopyrightText: 2018 - 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 pdfinfo pdfseparate && exit 3
function print_usage {
show_header "pdfsplit options:"
show_listitem " -i|--input <PDF input>"
show_listitem " -f|--first <# of first page to split>"
show_listitem " -l|--last <# of last page to split>"
show_listitem " -j|--join <flag to join split PDF pages together (def: false)"
}
OPTIONS=hi:f:l:
LONGOPTIONS=help,input:,first:,last:
PARSED=$(getopt -o "${OPTIONS}" --long "${LONGOPTIONS}" -n "${0}" -- "${@}")
eval set -- "${PARSED}"
while [ $# -ge 1 ]; do
case "${1}" in
-i | --input)
IN="${2}"
OUT="${2%.*}"
PAGES=$(pdfinfo "${2}" | grep "^Pages:" | sed -n "s/^Pages:\s*\([0-9]\+\)/\1/p")
shift 2
;;
-f | --FIRST)
FIRST="${2}"
shift 2
;;
-l | --LAST)
LAST="${2}"
shift 2
;;
-h | --help)
print_usage
exit 0
;;
--)
shift
break
;;
*)
show_error "ERROR: invalid option."
exit 3
;;
esac
done
# Checks
if ! [[ -v IN ]]; then
show_error "ERROR: No input file specified. Exiting."
exit 3
elif ! [[ -f "${IN}" ]]; then
show_error "ERROR: Input ${IN@Q} not found. Exiting."
exit 3
fi
FIRST="${FIRST:-1}"
LAST="${LAST:-${PAGES}}"
if ((LAST > PAGES)); then
show_warning "WARNING: ${LAST@Q} exceeds number of pages ${PAGES@Q}."
LAST=${PAGES}
fi
if ((FIRST > PAGES)); then
show_error "Starting page doesn't exist. The file only has ${PAGES} pages."
exit 3
fi
if ((FIRST > LAST)); then
show_error "What are you playing at?"
exit 3
fi
# Split the PDF out into the individual files. The if statements are needed so
# that the filenames will get sorted in numerical order. Otherwise, the glob
# used to join the pages at the end won't work. For example, 02 is sorted
# before 10, but 2 is placed after.
if ((FIRST >= 10)); then
pdfseparate -f "${FIRST}" -l "${LAST}" "${IN}" "${OUT}-%d.pdf"
elif ((LAST >= 10)); then
if ((LAST < 10)); then
pdfseparate -f "${FIRST}" -l "${LAST}" "${IN}" "${OUT}-0%d.pdf"
fi
if ((LAST >= 10)); then
pdfseparate -f "${FIRST}" -l 9 "${IN}" "${OUT}-0%d.pdf"
pdfseparate -f 10 -l "${LAST}" "${IN}" "${OUT}-%d.pdf"
fi
else
pdfseparate -f "${FIRST}" "${IN}" "${OUT}-0%d.pdf"
fi