-
Notifications
You must be signed in to change notification settings - Fork 1
/
bookletize-pdf.sh
executable file
·48 lines (39 loc) · 1.21 KB
/
bookletize-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
#!/bin/bash
###############################################################################
# Reorder pages in a PDF document for printing as booklet
# using duplex printing.
#
# The first page is assumed to be the front cover and
# the last page the back cover.
#
# mutool is required.
# https://mupdf.com/docs/index.html
###############################################################################
# Load helper functions
SCRIPT=`realpath $0`
SCRIPTPATH=`dirname $SCRIPT`
source $SCRIPTPATH/helper_functions.sh
function join_by {
local IFS="$1"; shift; echo "$*";
}
if [ ! $# -eq 2 ]; then
colorprintf red "Please supply source and target file names.\n"
printf "Ex:\n $0 source.pdf target.pdf \n\n"
exit 1
fi
docname=$1
newdocname=$2
numpages=$(mutool info $docname | grep "Pages: " | cut -d ":" -f2 | xargs)
printf "Number of pages is $numpages\n"
reordering=()
endnum=$(($numpages/2))
for pagenum in $(seq 1 $endnum); do
if [ $((pagenum%2)) -eq 0 ];
then
reordering+=("$pagenum,$(($numpages + 1 - $pagenum))")
else
reordering+=("$(($numpages + 1 - $pagenum)),$pagenum")
fi
done
printf "New page order:\n$(join_by , ${reordering[@]})\n"
mutool merge -o $newdocname $docname "$(join_by , ${reordering[@]})"