Booklet: a script for making A5 booklets on A4 paper
Here's a little TeX script I wrote to scratch an itch of mine: how to consolidate an A5 PDF document into an A4 PDF ready to throw through a duplex printer and bind into a booklet? I've done it manually in my highschool days and it's no fun. The first and last page have to be adjacent on one sheet, as do the second page and the one before last. It quickly gets messy from there. So I wrote this little script to automate it for me. It takes an A5 PDF file as input and places the proper pages adjacently, until the PDF runs out. The result is an A4 document ready for binding. Schematically:

% Run this file with pdftex, it produces a file named
% booklet.pdf in the current directory. Alfred Klomp, 2006.
\def\bookletfile{/home/alfred/mydocument.pdf}
\newcount\numpages
\newcount\halfnumpages
\newcount\fcount % forwards count, starts at page 1 and counts up
\newcount\bcount % backwards count, starts at last page and counts down
\newcount\leftright % is the page with the lowest number on the left?
\newcount\pagea % pageno. to be placed on the left
\newcount\pageb % pageno. to be placed on the right
\pdfpagewidth 297mm % landscape A4 format
\pdfpageheight 210mm
\pdfximage {\bookletfile}
\numpages=\pdflastximagepages
\halfnumpages=\numpages
\divide\halfnumpages by 2
\fcount=1
\bcount=\numpages
\leftright=0
\pdfhorigin=0bp
\pdfvorigin=0bp
\loop
\ifnum\leftright=0
\pagea=\bcount
\pageb=\fcount
\leftright=1
\else
\pagea=\fcount
\pageb=\bcount
\leftright=0
\fi
\shipout\hbox to \pdfpagewidth {%
\pdfximage height \pdfpageheight page \pagea {\bookletfile}%
\pdfrefximage\pdflastximage%
\hfil%
\pdfximage height \pdfpageheight page \pageb {\bookletfile}%
\pdfrefximage\pdflastximage%
}
\advance\fcount by 1
\advance\bcount by -1
\ifnum\bcount>\halfnumpages\repeat
\end
Usage
Linux users: change the line with the filename into the one you'd like to page, then run:
pdftex booklet.tex
This will produce a file called booklet.pdf in your current directory.
Windows users: change the line with the filename into the absolute path of your PDF
file, using forward slashes, such as C:/alfred/input.pdf. Then go to
the directory containing pdfTeX and run something like:
pdftex c:\alfred\booklet.tex
You'll find a file named booklet.pdf somewhere, either in the current directory or in the directory where the script is located.
A6 booklets, A4 booklets, A3 booklets, A2 booklets....
This script is by no means restricted to A5 booklets. All it does is take an input file and embed its pages in the correct order into a new document. You'll notice that the script doesn't assume any paper size for the input and doesn't have to, because it just sticks the input pages in the top left and right corners of the page respectively. In fact, the only sizes known to the script are the height and width of the output, in this case A4 landscape. You're free to redefine those sizes if you want to make different sized booklets, like A6 booklets on A5 paper (210mm wide, 148mm high), or A4 booklets on A3 paper (420mm wide, 297mm high), or some other weird configuration.
Last modified: 2008-04-15 at 13:09:42
Contact