When scanning an A5 booklet as individual A4 pages, but would still like to have them combined into an A5 PDF, the individual pages all end up in the ‘wrong’ place.
The script below will get it sorted automatically.
cat > a4scanned_to_a5booklet.sh <<EOF
#!/bin/bash
#
# a4scanned_to_a5booklet.sh scanned_front_pages.pdf scanned_back_pages.pdf booklet.pdf
# create a base filename that does not exist in the current directory
bfn="a4s2a5tmp"
while [ -f $bfn ]
do
bfn=${bfn}x
done
# check page numbers
fnp=$(pdfinfo "$1" | awk '/^Pages:/ {print $2}')
bnp=$(pdfinfo "$2" | awk '/^Pages:/ {print $2}')
n=$fnp
if [ $fnp -ne $bnp ]
then
echo "Error: Page number mismatch"
exit
fi
# split PDFs into images
echo "Splitting $1"
pdfimages $1 ${bfn}front
echo "Splitting $2"
pdfimages $2 ${bfn}back
# split each image into two and re-order pages
i=0
for f in ${bfn}front*
do
echo "Processing $f"
convert -rotate 270 -crop '50%x100%' +repage $f ${bfn}-%d
nn=$(printf "%05d" $((4*n-2*i)))
mv -i ${bfn}-0 ${bfn}-$nn
nn=$(printf "%05d" $((1+2*i)))
mv -i ${bfn}-1 ${bfn}-$nn
i=$((i+1))
done
i=0
for f in ${bfn}back*
do
echo "Processing $f"
convert -rotate 270 -crop '50%x100%' +repage $f ${bfn}-%d
nn=$(printf "%05d" $((2*n-2*i)))
mv -i ${bfn}-0 ${bfn}-$nn
nn=$(printf "%05d" $((2*n+1+2*i)))
mv -i ${bfn}-1 ${bfn}-$nn
i=$((i+1))
done
# combine pages to a single PDF
echo "Combining pages"
convert -compress jpeg -quality 80 ${bfn}-* $3
# delete temporary files
read -p "Are you sure you want to delete ${bfn}* (temporary files)? " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo "Deleting temporary files..."
rm ${bfn}*
fi
EOF
chmod +x a4scanned_to_a5booklet.sh
To use the script
a4scanned_to_a5booklet.sh scanned_front_pages.pdf scanned_back_pages.pdf booklet.pdf
The script does not do any parameter (or other) checks – you have been warned.