-
Notifications
You must be signed in to change notification settings - Fork 10
/
pdfsplitk
executable file
·74 lines (61 loc) · 2.17 KB
/
pdfsplitk
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
#!/bin/bash
# Copyright 2013, Raphael Reitzig
#
# pdfsplitk 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.
#
# pdfsplitk 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 pdfsplitk. If not, see <http://www.gnu.org/licenses/>.
# Splits PDFs into constant-sized chunks.
# Requires `pdftk`, `pdfinfo`, `awk` and `acroread` if PS output is desired.
#
# Call with four parameters:
# * First is the input file (`*.pdf`).
# * Second it the number of pages per chunk.
# * Third is the target directory.
# * Fourth is `ps` if you want PS instead of PDF output (optional).
if [ ${#} -lt 3 ];
then
echo "Usage: pdfsplitk <file.pdf> <pages per chunk> <target dir> [ps]";
exit;
fi
declare -i pagesper number count counter start end;
tmp="/tmp/pdfsplitk";
file=$1;
filename=${file%.pdf};
pagesper=$2;
target=$3;
mode=$4;
if [[ ! -d "${target}" ]]; then
mkdir "${target}";
fi
if [[ -d "${target}" ]]; then
mkdir ${tmp};
cp $file ${tmp}/;
number=`pdfinfo "${tmp}/${file}" 2>&1 | grep Pages | awk ' /\ddd+/; { print $2 }'`;
count=$((number / pagesper));
echo "[pdfsplitk] Creating ${count} documents with ${pagesper} pages each in ${target}.";
echo "";
counter=0;
while [[ $count -gt $counter ]]; do
echo -e "\033M[$((100*counter/count))%]";
start=$((counter*pagesper + 1));
end=$((start + pagesper - 1));
counterstring=`printf '%04d' ${counter}`;
if [[ ${mode} == "ps" ]]; then
acroread -toPostScript -size a4 -start ${start} -end ${end} -pairs "${tmp}/${file}" "${target}/${filename}_${counterstring}.ps";
else
pdftk "${tmp}/${file}" cat ${start}-${end} output "${target}/${filename}_${counterstring}.pdf";
fi
counter=$((counter + 1));
done
echo -e "\033M[Done]";
rm -rf ${tmp};
fi