Print PDF in special layouts

Posted on Thu 20 February 2020 in linux • 1 min read

Assuming you have a PDF and want to print that as a brochure or you want to print one A5 page two times on an A4 page. A useful tool for that is pdfjam, which you might already have installed. At least in Archlinux it is included in the texlive-core package.

Brochure or Book printing of a document

pdfbook input.pdf

Print single page A5 document two times on A4 sheet

pdfnup --outfile out.pdf input.pdf input.pdf

This can also be done as a script which handles input file rotation correctly


[[ -z $infile ]] && echo 'Input file required!' && exit 2
[[ -z $outfile ]] && echo 'Output file required!' && exit 3
[[ $(pdfinfo $infile | grep Pages | grep -o '[0-9]\+') -gt 1 ]] && echo 'One single page documents!' && exit 1

wdir=$(mktemp -d)

dims=("${(@f)$(pdfinfo $infile | grep 'Page size' | grep -o '[0-9]\+\.[0-9]\+')}")
landscape=$(bc <<< "$dims[1] >= $dims[2]")

if [ $landscape -eq 1 ]
    pdf90 --quiet --outfile $wdir/rot.pdf $infile
    cp $infile $wdir/rot.pdf

pdfnup --quiet --outfile $wdir/out.pdf $wdir/rot.pdf $wdir/rot.pdf

if [ $landscape -eq 1 ]
    pdf270 --quiet --outfile $outfile $wdir/out.pdf
    cp $wdir/out.pdf $outfile

rm -rf $wdir

which can be used easily like

./a5-to-double-a4 input.pdf out.pdf

In the script above some rotation commands are also used: pdf90 and pdf270