I want to reassemble my PDF documents to print them on A5 using the A4 format from my printer. Also I need to print two sites (slides) on each A5 page which of course should be double sided. Hence the A4 page is in landscape format. Than I want to cut it in the middle. A short example for 9 slides:
first A4 first back second A4
[1][5] [3][7] [9][-]
[2][6] [4][8] [-][-]
After cutting it it would get me slides 1-4, 5-8, 9 on a two-sided A5 paper.
Printing this using "Booklet Printing" has not worked yet. I thought of reassembling the PDF pages automatically with a shell script using pdftk and calculates mod 8 as changing the order manually is not an option. After I rearanged the order I can easily print the slides using the options of my printer.
How could that be done or is there an easier way to it? Thanks
as far I understand, you are trying to Impose 4 pages on every portrait A4, that will give to you 4 A6 single pages (two A5 sheets for A4 page) since dividing the A4 area into 4 parts, means to divide every side by two, so we'll have:
29.7 cm /2 = 14.8 cm (approximated)
21 cm divided by two = 10.5 cm
is is obvious, then, that in the same A4 portrait area, can find place, 4 A6 portrait single pages, and 2 A5 landscape A5 pages both, like in picture:
so, You don't want be able to make a booklet into A6 format, but only to cut portrait A4 into half to produce 2 A5 sheets for A4 page? Are you not interested to cut further these A5 landscape pages to have an A6 booklet to bind?
Since, as far I understand, it seems you are looking for a script able to produce a booklet made imposing 4 A6 pages for each side of an A4 sheet, recalculating in right order the imposition sequence, to have the correct front/back matching, take a look to my script
note: before to start, resize to A6 your input multipage pdf (that has A4 size)
this can be done with he help of
Multivalent.jar
java -cp path...to/Multivalent.jar tool.pdf.Impose -dim 1x1 -paper A6 input.pdf
then take the resulting output file (remember to work on a copy) rename this into your starting input file and use this script
to perform this task, and to to assure that your pdf multipage file has the right number of pages needed to perform imposition, (8 or a integer multiple of 8), you will use this script based on pdftk (at least 1.41 version that has stamp feature and Multivalent.jar - -https://rapidgator.net/file/c6bd7f31bf8885bcaa69b50ffab7e355/Multivalent20060102.jar.html (latest free version with tools included).
usage: namescript file.pdf multivalentpath
the multivalentpath is relative: if you downloaded Multivalent.jar into your home, then the multivalentpath will be /home/. You can also customize the script and replace directly the value for second argument (multivalentpath, so you don't need to type every time the multivalentpath; in this case, replace multivalentpath=$2
with
multivalentpath=/home/Multivalent.jar
the script also will add the crop marks to final output pdf
9 pages pdf sample
resulting file
view animation or resulting output file with crop marks added to make easy cut two times the amount of sheets (horizontally first and vertically then)
once you finished to print and put the last sheet on other sheets, take a look to your last imposed sheet
this need to be cutted (I highlighted using a dash line) in half horizzontally, in order to be able to close the two series of sheets (upper and lower) one another (the upper over the lower) obtaining a single series of sheets from the two original series of sheets,
at this stage, you can furtherly cut following the dashed line the sheet to be able to collate single A6 sheets and biding with glue, clip or other, or using two points of stapler an bend the booklet you have produced
note: the blank pages you seen, are placed automatically after last written page of your original pdf, they are needed to, as you rightly stated in your question, to perform the right imposition sequence, they don't interfere with sequential logic order, in resulting binded booklet the order, in the case of our example, will be:
1,2,3,4,5,6,7,8,9,10,11,1,2,13,14,15,16
the pages in bold, will be left blank and only needed to perform the right imposition, impossible to perform if the number of pages is not a multiple of 8
#!/bin/bash
#
#
############################
#
# use: namescript file.pdf multivalentpath
#
############################
#
#
#
#
file=$1
multivalentpath=$2
pages="`pdftk $file dump_data | grep NumberOfPages | cut -d : -f2`"
echo $pages
halfpages="`echo -n $(( $pages / 2 ))`"
echo $halfpages
incr="$(echo "scale=0; $halfpages+1" |bc -l)"
dividedby4="$(echo "scale=0; $pages/8" |bc -l)"
lastupperpage="$(echo "scale=0; $pages-2" |bc -l)"
u="u"
#first case
h="$(pdfinfo $file | grep "Page size" | cut -d x -f1 | tr 'Page size:' ' ' | xargs)"
w="$(pdfinfo $file | grep "Page size" | cut -d x -f2 | tr 'pts' ' ' | xargs)"
echo $h
echo $w
doubleheight="$(echo "scale=0; $h * 2" |bc -l)"
doublewidth="$(echo "scale=0; $w * 2" |bc -l)"
echo $doubleheight
echo $doublewidth
if [ $(( $pages % 8 )) -eq 0 ]
then
echo " the file has already a number of pages multiple by eight"
sequence="$(for ((x=$halfpages, y=$incr, z=$pages, w=1;x>=4, y<=4, z>=2, w<=$lastupperpage;x--, y++, z--, w++)); do echo "$x$u $y$u;$z $w"; done | tr ";" "\n" | tr " " "," | awk -F "," '{ print $2 "," $1; getline; print; getline; print; getline; print $2 "," $1 }' | tr "\n" "," | cut -d "," -f 1-`seq -s, 1 $pages`)"
echo "sequence is $sequence"
java -cp "$multivalentpath"Multivalent.jar tool.pdf.Impose -verbose -dim 2x2 -paper "$doubleheight"x"$doublewidth"pt -layout "$sequence" $file
cat << EOF |uudecode
begin-base64 644 /tmp/grid.pdf
JVBERi0xLjMKJcfsj6IKNSAwIG9iago8PC9MZW5ndGggNiAwIFIvRmlsdGVy
IC9GbGF0ZURlY29kZT4+CnN0cmVhbQp4nFWPuw7CUAiGd56C2USEw+EUnsDZ
OhonbzHRwTr4+p5Wq20YgP//wuWBTFyQ+xiLwx1WbYOXJ+xQEhbcV/EIBV8g
eIWBxXYNSyfR5IIlXEkV7+CaSKz8lBtsIdydfAIFZyeTGbT7LslhjKIRhXJF
v20dSTJwg+CZKYWOPv+dJrNS9tEpVpTEP76lILfJHaYNk+TZHYJ9dBfIjRtF
/cQlSLH+SWzxy90JzgvYwBusBz89ZW5kc3RyZWFtCmVuZG9iago2IDAgb2Jq
CjE3OAplbmRvYmoKNCAwIG9iago8PC9UeXBlL1BhZ2UvTWVkaWFCb3ggWzAg
MCA1OTUgODQyXQovUGFyZW50IDMgMCBSCi9SZXNvdXJjZXM8PC9Qcm9jU2V0
Wy9QREZdCi9FeHRHU3RhdGUgOCAwIFIKPj4KL0NvbnRlbnRzIDUgMCBSCj4+
CmVuZG9iagozIDAgb2JqCjw8IC9UeXBlIC9QYWdlcyAvS2lkcyBbCjQgMCBS
Cl0gL0NvdW50IDEKPj4KZW5kb2JqCjEgMCBvYmoKPDwvVHlwZSAvQ2F0YWxv
ZyAvUGFnZXMgMyAwIFIKPj4KZW5kb2JqCjcgMCBvYmoKPDwvVHlwZS9FeHRH
U3RhdGUKL09QTSAxPj5lbmRvYmoKOCAwIG9iago8PC9SNwo3IDAgUj4+CmVu
ZG9iagoyIDAgb2JqCjw8L1Byb2R1Y2VyKEVTUCBHaG9zdHNjcmlwdCA4MTUu
MDQpCi9DcmVhdGlvbkRhdGUoRDoyMDEyMTIwMTIzNDMzNCkKL01vZERhdGUo
RDoyMDEyMTIwMTIzNDMzNCk+PmVuZG9iagp4cmVmCjAgOQowMDAwMDAwMDAw
IDY1NTM1IGYgCjAwMDAwMDA0NzIgMDAwMDAgbiAKMDAwMDAwMDU5MCAwMDAw
MCBuIAowMDAwMDAwNDEzIDAwMDAwIG4gCjAwMDAwMDAyODIgMDAwMDAgbiAK
MDAwMDAwMDAxNSAwMDAwMCBuIAowMDAwMDAwMjYzIDAwMDAwIG4gCjAwMDAw
MDA1MjAgMDAwMDAgbiAKMDAwMDAwMDU2MSAwMDAwMCBuIAp0cmFpbGVyCjw8
IC9TaXplIDkgL1Jvb3QgMSAwIFIgL0luZm8gMiAwIFIKL0lEIFso1xqpwgd/
HzDmRPwLQT3dEyko1xqpwgd/HzDmRPwLQT3dEyldCj4+CnN0YXJ0eHJlZgo3
MDEKJSVFT0YK
====
EOF
pdftk ${file%%.pdf}-up.pdf stamp /tmp/grid.pdf output gridded.pdf && mv gridded.pdf ${file%%.pdf}-up.pdf && echo "finished" && exit 0
else
echo "number of pages is not a multiple of 8, adding needed blank pages to complete the imposition sequence"
heightxwidth="`pdfinfo -box $file| grep MediaBox | cut -d : -f2 | awk '{print $3 FS $4}'`"
echo "%PDF-1.4
1 0 obj
<<
/CreationDate (D:20121202145344)
/Producer (text2pdf v1.1 (\251 Phil Smith, 1996))
/Title (blank.txt)
>>
endobj
2 0 obj
<<
/Type /Catalog
/Pages 3 0 R
>>
endobj
4 0 obj
<<
/Type /Font
/Subtype /Type1
/Name /F1
/BaseFont /Courier
>>
endobj
5 0 obj
<<
/Font << /F1 4 0 R >>
/ProcSet [ /PDF /Text ]
>>
endobj
6 0 obj
<<
/Type /Page
/Parent 3 0 R
/Resources 5 0 R
/Contents 7 0 R
>>
endobj
7 0 obj
<<
/Length 8 0 R
>>
stream
BT
/F1 10 Tf
1 0 0 1 50 798 Tm
12 TL
()'
ET
endstream
endobj
8 0 obj
44
endobj
3 0 obj
<<
/Type /Pages
/Count 1
/MediaBox [ 0 0 595 841 ]
/Kids [ 6 0 R ]
>>
endobj
xref
0 9
0000000000 65535 f
0000000009 00000 n
0000000132 00000 n
0000000524 00000 n
0000000181 00000 n
0000000259 00000 n
0000000330 00000 n
0000000410 00000 n
0000000506 00000 n
trailer
<<
/Size 9
/Root 2 0 R
/Info 1 0 R
>>
startxref
609
%%EOF" | sed -e "s/595 841/$heightxwidth/g">/tmp/blank.pdf
fi
if [ $(( $pages / 8 )) -eq 0 ]
then val0="`echo "scale=0; 8-$pages" | bc -l`"
else val1="`echo "scale=0; $pages/8" | bc -l`"; echo $val1
fi
if [ $(( $pages / 8 )) -eq 0 ] ; then echo "case 2: adding $val0 blank pages" ; sleep 1s && numpages=`for ((a=1; a <= $val0; a++)); do echo -n " B1 "; done` && pdftk A=$file B=/tmp/blank.pdf cat A $numpages output pagesadded.pdf && mv pagesadded.pdf $file
#new variable values for second case
unset pages
unset halfpages
unset incr
unset dividedby4
unset lastupperpage
pages="`pdftk $file dump_data | grep NumberOfPages | cut -d : -f2`"
halfpages="`echo -n $(( $pages / 2 ))`"
incr="$(echo "scale=0; $halfpages+1" |bc -l)"
dividedby4="$(echo "scale=0; $pages/8" |bc -l)"
lastupperpage="$(echo "scale=0; $pages-2" |bc -l)"
echo $pages
else
val2="`let ADDITION=$val1+1; echo $ADDITION`"
val3="`let MULTIPLICATION=$val2*8; echo $MULTIPLICATION`"
val4="`echo "scale=0; $val3-$pages" |bc -l`"
echo " case 3: adding $val4 blank pages" ; sleep 1s
numpages="`for ((a=1; a <= $val4; a++)); do echo -n " B1 "; done`"
echo $numpages
pdftk A=$file B=/tmp/blank.pdf cat A $numpages output pagesadded.pdf && mv pagesadded.pdf $file
fi
#new variable values for third case
unset pages
unset halfpages
unset incr
unset dividedby4
unset lastupperpage
pages="`pdftk $file dump_data | grep NumberOfPages | cut -d : -f2`"
halfpages="`echo -n $(( $pages / 2 ))`"
incr="$(echo "scale=0; $halfpages+1" |bc -l)"
dividedby4="$(echo "scale=0; $pages/8" |bc -l)"
lastupperpage="$(echo "scale=0; $pages-2" |bc -l)"
sequence="$(for ((x=$halfpages, y=$incr, z=$pages, w=1;x>=4, y<=4, z>=2, w<=$lastupperpage;x--, y++, z--, w++)); do echo "$x$u $y$u;$z $w"; done | tr ";" "\n" | tr " " "," | awk -F "," '{ print $2 "," $1; getline; print; getline; print; getline; print $2 "," $1 }' | tr "\n" "," | cut -d "," -f 1-`seq -s, 1 $pages` | xargs)"
echo $sequence
java -cp "$multivalentpath"Multivalent.jar tool.pdf.Impose -verbose -dim 2x2 -paper "$doubleheight"x"$doublewidth"pt -layout "$sequence" $file
cat << EOF |uudecode
begin-base64 644 /tmp/grid.pdf
JVBERi0xLjMKJcfsj6IKNSAwIG9iago8PC9MZW5ndGggNiAwIFIvRmlsdGVy
IC9GbGF0ZURlY29kZT4+CnN0cmVhbQp4nFWPuw7CUAiGd56C2USEw+EUnsDZ
OhonbzHRwTr4+p5Wq20YgP//wuWBTFyQ+xiLwx1WbYOXJ+xQEhbcV/EIBV8g
eIWBxXYNSyfR5IIlXEkV7+CaSKz8lBtsIdydfAIFZyeTGbT7LslhjKIRhXJF
v20dSTJwg+CZKYWOPv+dJrNS9tEpVpTEP76lILfJHaYNk+TZHYJ9dBfIjRtF
/cQlSLH+SWzxy90JzgvYwBusBz89ZW5kc3RyZWFtCmVuZG9iago2IDAgb2Jq
CjE3OAplbmRvYmoKNCAwIG9iago8PC9UeXBlL1BhZ2UvTWVkaWFCb3ggWzAg
MCA1OTUgODQyXQovUGFyZW50IDMgMCBSCi9SZXNvdXJjZXM8PC9Qcm9jU2V0
Wy9QREZdCi9FeHRHU3RhdGUgOCAwIFIKPj4KL0NvbnRlbnRzIDUgMCBSCj4+
CmVuZG9iagozIDAgb2JqCjw8IC9UeXBlIC9QYWdlcyAvS2lkcyBbCjQgMC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