Skip to content

Instantly share code, notes, and snippets.

@tsuchm
Last active May 23, 2018 12:46
Show Gist options
  • Save tsuchm/62c4866ac96276733598a5dbaa5ea18d to your computer and use it in GitHub Desktop.
Save tsuchm/62c4866ac96276733598a5dbaa5ea18d to your computer and use it in GitHub Desktop.
Script which converts the 2-up PDF file into the single page PDF file
#!/bin/sh
usage(){
cat <<EOF
This script converts the 2-up PDF file into the single page PDF file.
For example, http://www.city.yokohama.lg.jp/kankyo/midoriup/jigyo/mori/guidebook.pdf
is a PDF file which consists of A3 16 pages, each A3 page contains 2
A4 pages. In order to print the above PDF as a brochure, do the
following procedure.
(0) This script depends pdfinfo which is provided by poppler-utils
package and pdftk. Install them before executing this script.
sudo apt-get install poppler-utils pdftk
(1) Execute this script to split each A3 page into two A4 pages, as follows:
sh pdfunup.sh input.pdf A4_pages.pdf
(2) Sort some broken ordered pages.
pdftk A4_pages.pdf cat 2-32 1 output A4_sorted.pdf
(3) Riffle it, using https://github.com/ueokande/rifflepdf
rifflepdf -o A4_riffled.pdf -m pdftk A4_sorted.pdf
(4) Print the above PDF file in 2-up style and double-sided
printinting.
EOF
}
input=${1}
output=${2}
beginpage=${3}
endpage=${4}
if [ ! -f "${input}" -o -z "${output}" ]; then
usage
exit
fi
if [ -z "${beginpage}" ]; then
beginpage=1
fi
dir=`mktemp -d unfold.XXXXXXXX`
rmdir(){
rm -rf ${dir}
}
trap 'rmdir' INT TERM PIPE
maxpage=`pdfinfo ${input}|awk '/^Pages:/{print $2}'`
count=0
if [ ${beginpage} -gt 1 ]; then
lastpage=`expr ${beginpage} - 1`
for page in `seq 1 ${lastpage}`; do
pagepdf=${dir}/sep_${count}.pdf
count=`expr ${count} + 1`
echo pdftk ${input} cat ${page} output ${pagepdf}
pdftk ${input} cat ${page} output ${pagepdf}
done
fi
if [ -z "${endpage}" ]; then
lastpage=${maxpage}
else
lastpage=`expr ${endpage} - 1`
fi
for page in `seq ${beginpage} ${lastpage}`; do
pagepdf=${dir}/page_${page}.pdf
leftpdf=${dir}/sep_${count}.pdf
count=`expr ${count} + 1`
rightpdf=${dir}/sep_${count}.pdf
count=`expr ${count} + 1`
echo pdftk ${input} cat ${page} output ${pagepdf}
pdftk ${input} cat ${page} output ${pagepdf}
# For more detail of these commands, see https://superuser.com/questions/191373/linux-based-tool-to-chop-pdfs-into-multiple-pages
pagesize=`pdfinfo ${pagepdf}|awk '/^Page size:/{print $3,$5}'`
height=`echo ${pagesize}|cut -f2 -d' '|cut -f1 -d.`
width=`echo ${pagesize}|cut -f1 -d' '|cut -f1 -d.`
if ( pdfinfo ${pagepdf} | egrep -q '^Page[[:space:]]+rot:[[:space:]]+90' ); then
tmpvar=${height}
height=${width}
width=${tmpvar}
fi
height=`expr ${height} '*' 10`
offset=`expr ${width} '/' 2`
width=`expr ${width} '*' 5`
echo gs -q -dBATCH -o ${leftpdf} -sDEVICE=pdfwrite -g${width}x${height} -c '<</PageOffset [0 0]>> setpagedevice' -f ${pagepdf}
gs -q -dBATCH -o ${leftpdf} -sDEVICE=pdfwrite -g${width}x${height} -c '<</PageOffset [0 0]>> setpagedevice' -f ${pagepdf}
echo gs -q -dBATCH -o ${rightpdf} -sDEVICE=pdfwrite -g${width}x${height} -c '<</PageOffset [-'${offset}' 0]>> setpagedevice' -f ${pagepdf}
gs -q -dBATCH -o ${rightpdf} -sDEVICE=pdfwrite -g${width}x${height} -c '<</PageOffset [-'${offset}' 0]>> setpagedevice' -f ${pagepdf}
done
if [ ! -z "${endpage}" ]; then
for page in `seq ${endpage} ${maxpage}`; do
pagepdf=${dir}/sep_${count}.pdf
count=`expr ${count} + 1`
echo pdftk ${input} cat ${page} output ${pagepdf}
pdftk ${input} cat ${page} output ${pagepdf}
done
fi
count=`expr ${count} - 1`
for page in `seq 0 ${count}`; do
echo gs -q -sOutputFile=${dir}/resize_${page}.pdf -sDEVICE=pdfwrite -sPAPERSIZE=a4 -dNOPAUSE -dBATCH -dPDFFitPage ${dir}/sep_${page}.pdf
gs -q -sOutputFile=${dir}/resize_${page}.pdf -sDEVICE=pdfwrite -sPAPERSIZE=a4 -dNOPAUSE -dBATCH -dPDFFitPage ${dir}/sep_${page}.pdf
done
pdftk `seq 0 ${count}|xargs -n 1 printf "${dir}/resize_%d.pdf "` cat output ${output}
rmdir
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment