-
Notifications
You must be signed in to change notification settings - Fork 1
/
mbkl
255 lines (236 loc) · 9.29 KB
/
mbkl
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#!/bin/bash
if [ "$1" == '-h' ]||[ "$1" == '--help' ]||[ -z "$1" ]||$(echo 'cb sb sorep spl tstp' | grep -vq --regexp="$1"); then
echo "
## yet another linux shell script to Make BooKLets
## (pdf -> 'booklet'.pdf(a4))
## bash, ghostscript, find, head, tail, mktemp are required
## More help: $0 [cb,sb,sorep,spl,tstp] -h
## cb - complex booklet
## sb - simple booklet
## sorep - split to odd and reversed even pages
## spl - split to parts with custom number of pages
## tstp - test scale and position shift for selected page
## There are no checks, so the probability of failure is high.
## Author Andrew S. License GPL [github,gitlab].com/quarkscript
## to create a new pdf-file with only needed pages just run:
gs -o out.pdf -sDEVICE=pdfwrite -sPageList=1,5-10,12 -f source.pdf
"; exit 0; fi
############## functions #################
sb(){
if [ "$1" == '-h' ]||[ "$1" == '--help' ]||[ -z "$1" ]; then
echo "
## Shell script to create a simple booklet from pdf
## Use $0 sb 'filename' 'pages' 'xscale' 'yscale' 'xshift' 'yshift'
## $0 sb 'filename' '' '1.0' '1.0' '0' '0'
## Parameters except filename are optional,
## last 4 of them you can test with tstp function
## There are no checks, so the probability of failure is high.
## Author Andrew S. License GPL [github,gitlab].com/quarkscript
"; exit 0
fi
if [ -z "$3" ]; then xscale='1.0'; else xscale="$3"; fi
if [ -z "$4" ]; then yscale='1.0'; else yscale="$4"; fi
if [ -z "$5" ]; then xshift='0'; else xshift="$5"; fi
if [ -z "$6" ]; then yshift='0'; else yshift="$6"; fi
##
maxthreads=$(($(grep 'model name' /proc/cpuinfo --count)+1))
threads=0
##
if [ -z "$2" ]; then
echo get pages num
pages=$(gs -q -dNOSAFER -dNODISPLAY -dNumRenderingThreads=$maxthreads -c "($1) (r) file runpdfbegin pdfpagecount =" -f "$1" -o /dev/null)
else
pages=$2
fi
## extract scale and crop pages
for ((c0=1;c0<=$pages;c0+=1)); do
gs -o tmp.page.$c0.pdf -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -c "<</BeginPage{$xscale $yscale scale $xshift $yshift translate}>> setpagedevice" -sPageList=$c0 "$1" &
threads=$(($threads+1))
if [ "$threads" -eq "$maxthreads" ]; then
threads=0
wait
fi
done
threads=0; wait
## pages count must be multiple of 4
## make empty extra pages if needed
if [ $(($pages%4)) -ne 0 ]; then
for ((c0=1;c0<=$(($pages%4));c0+=1)); do
pages=$(($pages+1))
gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -sPAPERSIZE=a4 -sOutputFile="tmp.page.$pages.pdf"
done
fi
for ((c0=1;c0<=$pages;c0+=1)); do
#gs -o a5.tmp.page.$c0.pdf -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -dPDFFitPage -dFIXEDMEDIA -sPAPERSIZE=a5 -f tmp.page.$c0.pdf &
gs -o a4.tmp.page.$c0.ps -sDEVICE=ps2write -dNOPAUSE -dBATCH -dSAFER -dPSFitPage -dFIXEDMEDIA -sPAPERSIZE=a4 -f tmp.page.$c0.pdf &
threads=$(($threads+1))
if [ "$threads" -eq "$maxthreads" ]; then
threads=0
wait
fi
done
threads=0; wait
rm -f tmp.page.*.pdf
g2p(){
## glue 2 pages - internal func to make 2-up page
## input arguments: 'left page index' 'right page index' 'out page index'
#gs -o fin.page.$3.pdf -sDEVICE=ps2write -d.Nx=2 -d.Ny=1 -d.Landscape=true gsnup.ps -f a4.tmp.page.$1.ps a4.tmp.page.$2.ps
## because -d.Ny=1 gsnup.ps didn't work for me, there is some crutch
gs -o tmp.page.$3.ps -sDEVICE=ps2write -d.Landscape=true -d.Ny=2 gsnup.ps -f a4.tmp.page.$1.ps a4.tmp.page.$2.ps
gs -o fin.page.$3.pdf -sDEVICE=pdfwrite -g8420x5950 -c "<</BeginPage{1.415 1.415 scale 0 -420 translate}>> setpagedevice" -f tmp.page.$3.ps
rm -f tmp.page.$3.ps a4.tmp.page.$1.ps a4.tmp.page.$2.ps
}
pagelist=''
for ((c0=2;c0<=($pages/2);c0+=2)); do
pagelist+=" ""fin.page.$(($c0-1)).pdf"
g2p "$(($pages-$c0+2))" "$(($c0-1))" "$(($c0-1))" &
threads=$(($threads+1))
pagelist+=" ""fin.page.$c0.pdf"
g2p "$(($c0+0))" "$(($pages-$c0+1))" "$c0" &
threads=$(($threads+1))
if [ "$threads" -ge "$maxthreads" ]; then
threads=0
wait
fi
done
threads=0; wait
gs -o "${1%.*}_mbkl.pdf" -sDEVICE=pdfwrite -dNumRenderingThreads=$maxthreads -f $pagelist
rm -f fin.page.*.pdf
}
############## next function #################
sorep(){
if [ "$1" == '-h' ]||[ "$1" == '--help' ]||[ -z "$1" ]; then
echo "
## Split pdf files to Odd and Reversed Even Pages.
## Use $0 sorep 'filename' 'pages' ;pages is optional
## There are no checks, so the probability of failure is high.
## Author Andrew S. License GPL [github,gitlab].com/quarkscript
"; exit 0
fi
maxthreads=$(($(grep 'model name' /proc/cpuinfo --count)+1))
threads=0
if [ -z "$2" ]; then
echo get pages num
pages=$(gs -q -dNOSAFER -dNODISPLAY -dNumRenderingThreads=$maxthreads -c "($1) (r) file runpdfbegin pdfpagecount =" -f "$1" -o /dev/null)
else
pages=$2
fi
for ((jj=1;jj<=pages;jj+=1)); do
if [ $(($jj%2)) -eq 0 ]; then
if [ -z "$even_rvsd" ]; then
even_rvsd="tmp.page.$jj.pdf"
threads=$(($threads+1))
else
even_rvsd="tmp.page.$jj.pdf $even_rvsd"
threads=$(($threads+1))
fi
else
if [ -z "$odd" ]; then
odd="tmp.page.$jj.pdf"
threads=$(($threads+1))
else
odd="$odd tmp.page.$jj.pdf"
threads=$(($threads+1))
fi
fi
gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -sPageList=$jj -sOutputFile="tmp.page.$jj.pdf" "$1" &
if [ "$threads" -eq "$maxthreads" ]; then
threads=0
wait
fi
done
wait
gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -sOutputFile="${1%.*}_sp_ev_r.pdf" $even_rvsd &
gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -sOutputFile="${1%.*}_fp_odd.pdf" $odd &
wait
rm -f tmp.page.*.pdf
}
############## next function #################
spl(){
if [ "$1" == '-h' ]||[ "$1" == '--help' ]; then
echo "
## split PDF files into parts with a fixed number of pages.
## Use $0 spl 'filename' 'stepsize' 'pages' Parameters are optional.
## When filename not specified will be processed all PDF files;
## from current dir. By default stepsize=60
## There are no checks, so the probability of failure is high.
## Author Andrew S. License GPL [github,gitlab].com/quarkscript
"; exit 0
fi
if [ -z "$2" ]; then
stepsize=60
else
stepsize=$2
fi
if [ -z "$1" ]; then
## if filename not specified then call this script for each pdf from current dir
lstfl=$(mktemp XXXXXXXXXX.tmp)
find -maxdepth 1 -name "*.pdf" | sed 's|\.\/||g'>$lstfl
for ((lnm=1;lnm<=$(wc -l $lstfl | sed "s/ $lstfl//g");lnm+=1)); do
spl "$(head -n $lnm $lstfl 2>&1 | tail -n 1)" "$stepsize"
done
rm -f $lstfl
else
maxthreads=$(($(grep 'model name' /proc/cpuinfo --count)+1))
threads=0
count=0
filename="$1"
if [ -z "$3" ]; then
pages=$(gs -q -dNOSAFER -dNODISPLAY -dNumRenderingThreads=$maxthreads -c "($filename) (r) file runpdfbegin pdfpagecount =" -f "$filename" -o /dev/null)
else
pages="$3"
fi
while [ "$(($pages-($count)*$stepsize))" -ge "$stepsize" ]; do
gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -dFirstPage="$((($count)*$stepsize+1))" -dLastPage="$((($count+1)*$stepsize))" -sOutputFile="${filename%.*}_$(($count+1)).pdf" "$filename" &
count=$(($count+1))
## unleash simultaneous processing for independent parts
threads=$(($threads+1))
if [ "$threads" -eq "$maxthreads" ]; then
threads=0
wait
fi
done
if [ "$((($count)*$stepsize))" -lt "$pages" ] ; then
gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -dFirstPage="$((($count)*$stepsize+1))" -dLastPage="$pages" -sOutputFile="${filename%.*}_$(($count+1)).pdf" "$filename" &
fi
wait
fi
}
############## next function #################
cb(){
if [ "$1" == '-h' ]||[ "$1" == '--help' ]||[ -z "$1" ]; then
echo "
## make a couple of booklets with a fixed number of pages
## Use $0 cb 'filename' 'whole pages' 'parts size' \\
## 'xscale' 'yscale' 'xshift' 'yshift' (optional)
## There are no checks, so the probability of failure is high.
## Author Andrew S. License GPL [github,gitlab].com/quarkscript
"; exit 0; fi
spl "$1" "$3" "$2"
listfl=$(mktemp XXXXXXXXXX.tmp)
find -maxdepth 1 -name "*.pdf" | grep -v --regexp="$1" | grep --regexp="${1%.*}" | sed 's|\.\/||g'>$listfl
for ((lnum=1;lnum<=$(wc -l $listfl | sed "s/ $listfl//g");lnum+=1)); do
sb "$(head -n $lnum $listfl 2>&1 | tail -n 1)" "" "$4" "$5" "$6" "$7"
rm -f "$(head -n $lnum $listfl 2>&1 | tail -n 1)"
done
rm -f $listfl
}
############## next function #################
tstp(){
if [ "$1" == '-h' ]||[ "$1" == '--help' ]||[ -z "$1" ]||[ -z "$2" ]; then
echo "
## test scale and shift factors with selected page from pdf file
## Use $0 tstp 'filename' 'page number' 'xscale' 'yscale' 'xshift' 'yshift'
## last 4 parameters are optional
## There are no checks, so the probability of failure is high.
## Author Andrew S. License GPL [github,gitlab].com/quarkscript
"; exit 0; fi
if [ -z "$3" ]; then xscale='1.0'; else xscale="$3"; fi
if [ -z "$4" ]; then yscale='1.0'; else yscale="$4"; fi
if [ -z "$5" ]; then xshift='0'; else xshift="$5"; fi
if [ -z "$6" ]; then yshift='0'; else yshift="$6"; fi
#gs -sDEVICE=bbox -dNOPAUSE -dSAFER -dBATCH -dFirstPage=$2 -dLastPage=$2 "$1"
gs -dSAFER -dBATCH -dFirstPage=$2 -dLastPage=$2 -c "<</BeginPage{$xscale $yscale scale $xshift $yshift translate}>> setpagedevice" -f "$1"
}
############## exec function #################
"$1" "$2" "$3" "$4" "$5" "$6" "$7"