-
Notifications
You must be signed in to change notification settings - Fork 0
/
unshitPDF.sh
executable file
·78 lines (69 loc) · 1.72 KB
/
unshitPDF.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
#!/bin/bash
if [ "$1" = "--help" -o "$1" = "-h" -o '(' $# -ne 1 -a $# -ne 3 ')' ]; then
cat <<- EOF
This script rearranges PDFs that have multiple pages printed on the
same sheet, like this:
_________
| 1 | 2 |
---------
| 3 | 4 | page 1
---------
| 5 | 6 |
---------
into a single-page single-sheet pdf, like this:
_____
| 1 | page 1
-----
-----
| 2 | page 2
-----
.
.
it uses the 'mutool' program to split pages.
it uses the 'qpdf' program to reorder the pages generated by mutool
On archlinux mutool is in the mupdf-tools package
EOF
echo
echo USAGE: $0 pdf_file [columns_num rows_num]
echo default value for columns and rows per page is 2
exit 1
fi
tmpd="$(mktemp -d mutool.XXXXXX --tmpdir)"
# reading parameters
pdfin="$1"
cols="$2"
[ -z "$cols" ] && cols=2
rows="$3"
[ -z "$rows" ] && rows=2
# splits pages into vertical pieces
if [ $rows -ne 1 ]; then
mutool poster -x "$rows" "$pdfin" "$tmpd/h.pdf"
else
mv "$pdfin" "$tmpd/h.pdf"
fi
cd "$tmpd"
# splits pages into horizontal pieces
if [ "$cols" -gt 1 ]; then
# mutool splits columns in reverse order for some obscure reason
mutool poster -y "$cols" h.pdf v.pdf
npages=$(mutool info v.pdf | egrep '^Pages: ' | cut -d ' ' -f 2)
echo $npages pages to elaborate
let r=0
pages=''
while [ $npages -gt 0 ]; do
let c=$cols
while [ $c -gt 0 ]; do
pages="$pages,$((($r*$cols)+$c))"
let c--
let npages--
done
let r++
done
#echo ${pages:1}
qpdf --empty --pages v.pdf ${pages:1} -- final.pdf
else
mv h.pdf final.pdf
fi
cd - > /dev/null
mv "$tmpd/final.pdf" "$pdfin"
rm -r "$tmpd"