-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge-pdf.sh
97 lines (87 loc) · 1.91 KB
/
merge-pdf.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
#!/usr/bin/env bash
# Copyright 2018 mrl5
# Distributed under the terms of the GNU General Public License v3
# Creates new pdf from multiple pdfs
#
# $1: output file
# $2: input-1 file
# $3: input-2 file
# $n: input-n file
#
# Globs are also supported:
# $1: output file
# $2: input-* files
#
# Example 1: 'sh merge-pdf.sh /path/to/ouotput.pdf /path/to/input-1.pdf /path/to/input-2.pdf /path/to/input-n.pdf'
# creates output.pdf from input-1.pdf, input-2.pdf, input-n.pdf
#
# Example 2: 'sh merge-pdf.sh /path/to/ouotput.pdf /path/to/input-*.pdf'
# creates output.pdf from files matching pattern input-*.pdf
# checks if enough arguments were provided
if [ $# -lt 2 ]; then
echo "Expected minimum 2 arguments. Please specify output file and input files."
exit 1
else
OUTPUT="$1"
# stores args $2, $3, $4, ..., $n
INPUT_GROUP="${@:2}"
fi
# checks if output file exists
check_output() {
ow_msg="$OUTPUT already exists. Do you want to overwrite it? [yes/no]: "
abort_msg="Aborting."
if [ -f "$OUTPUT" ]; then
# ask to overwrite
read -p "$ow_msg" answr
while [[ -z $answr ]]; do
answr="yes"
done
# Convert $answr to lower cases
ovrwrt=${answr,,}
case $ovrwrt in
no)
echo $abort_msg
exit 1
;;
n)
echo $abort_msg
exit 1
;;
yes)
;;
y)
;;
*)
echo "Wrong answer."
check_output
;;
esac
fi
}
# checks if input files exist
check_inputs() {
for file in $INPUT_GROUP; do
if [ ! -f $file ]; then
echo "$file doesn't exist. Aborting."
exit 1
fi
done
}
# merges multiple .pdf files into one using GhostScript
merge() {
echo Creating $OUTPUT ...
echo ""
gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE="$OUTPUT" -dBATCH $INPUT_GROUP
# checks if last command returned error
if [ $? -eq 0 ]; then
echo ""
echo "Done. New PDF: $OUTPUT"
else
echo ""
echo "Something went wrong. $OUTPUT may be corrupted."
fi
}
# main()
check_output
check_inputs
merge