HOWTO print a TeX PDF as a booklet
By llauren on Jan 30, 2006 in english, geek
Do this:
pdftops -level2 book.pdf book.0.ps && pstops "4:0@1.2(-0.5cm,-3.5cm),1@1.2(-3cm,-3.5cm),2@1.2(-1cm,-3.5cm),3@1.2(-3cm,-3.5cm)" book.0.ps | psbook | psnup -2 | pstops "2:0@1.0(-0.00cm,0.0cm),1U@1.0(21cm,29.7cm)" > book.ps
Last friday, i wanted to use less paper to print out a 256 page book on mobile programming. I wanted to print a booklet. I wanted this so bad that i spent most of the friday trying to figure out how to do it.
So as not to be the only one benefiting from my mammoth waste of time, i’ll share my findings.
I’m sure you could do more McDinking with the settings but at the stage i came up with the one liner above, i’d completely had it (and i still needed to print, cut and bind the damn thing). I should also note that on Windows, with proper HP drivers, i have been able to accomplish the same magic by just ticking a checkbox, but no such checkbox exists on this do-it-yourself system of a Linux. Whee.
Here’s an explanation of my behemoth one-liner, broken into digestable steps.
First, you take a PDF file created from a TeX source. The operation will be a lot simpler if you start with something formatted for a full A4 page; TeX does this very nice formatting commended by the Church of Knuth that makes text easier to read. If you’re just going to output A4 (or Letter) printouts, i strongly suggest you stick to the original settings. Also, if you have the original TeX source, you can do a lot of the tweakings elsewhere than here and save yourself from a lot of work. Here i started with a PDF file with no access to the source.
pdftops -level2 book.pdf book.0.ps
Pdftops translates the PDF file into a PostScript file. Since our printer (and oldish Laserjet) coughs on overly complex PostScript Level 3, i asked the file to be translated into older PostScript. TEX (there, i wrote it correctly once — now can you leave me alone?) formats the page to work beautifully on an unadultered size, but in this case, i’m going to zoom it down to fit the page on half a sheet. In its original size, TeX leaves rather giant margins around the text, which, i stress, is fine if used as is, but on an A5, the text gets rather small for my eyes.
So i zoom.
pstops "4:0@1.2(-0.5cm,-3.5cm),1@1.2(-3cm,-3.5cm),2@1.2(-1cm,-3.5cm),3@1.2(-3cm,-3.5cm)" book.0.ps book.1.ps
This magnifies the page by 20%, then (yes, after scaling) moves the resulting page a various amount of centimetres to the left, and 3½ cm down (which means it actually moves 1.2 * 3.5 cm down). The four-phase translations (Xcm,-3.5cm) are a result from much tweaking, McDinking and frustration.
Then comes my favourite command.
psbook book.1.ps book.2.ps
Psbook re-orders the pages of the original PDF stream to be book(let)-printable. It felt like a miracle to find it.
After that, pages can be re-shuffled so that two pages are fit on one sheet:
psnup -2 book2.ps book.3.ps
But we’re not done just yet. Since many duplex printers (which is a fancy name for a printer that can print on both sides of a paper) print the reverse side of the paper upside down, we have to accommodate for this. Again we use the pstops command:
pstops "2:0@1.0(-0.00cm,0.0cm),1U@1.0(21cm,29.7cm)" book.3.ps book.ps
The second argument (1U…) rotates the page upside-down, then offsets it so that the resulting output actually hits the page. It’s quite intuitive if you’re a transformation matrix.
The whole exercise leaves you with a book.ps, which you can send to the printer using
lpr -Pprinter book.ps
or translate back into PDF if you want to confuse somebody else.
thanks for this recipe, works like a charm
me | Feb 18, 2007 | Reply