+
AMDG
Goodman Coat of Arms

Goodman's Oak

Miscellaneous Thoughts, Projects, and Ruminations on Life, the Universe, and Everything

A Tale of PDFs

Donald P. Goodman III 8 Apr 1207 (8 Apr 2023)

So in addition to code, I like books, and I wrote the shell script makebook to make it possible, even easy, to impose multiple pages of a pdf onto a single sheet for folding and binding into a physical book. I've done precisely this with many such books, mostly small ones but occasionally real book-length texts.

Until recently (v3.0, to be precise), makebook has used pdftk to do some of this pdf grunt-work under the hood. pdftk is a fine program, with a lot of power and capability, its only downsides being (1) it's written in Java and C++, (2) it's rather slow, and (3) its licensing is more confused than I'd generally like, with a “free” tier and a “pro” tier.

Due to these issues, mostly the licensing one (which makes it tough to get pdftk working on my OpenBSD system), I decided that I would replace the pdftk lines in makebook with qpdf, another pdf library that is, unfortunately, also written in C++. The upside is that qpdf is very clearly freely licensed (Apache 2.0), and also that OpenBSD has a package for it.

I did struggle with one thing: qpdf does not, as far as I could tell, have a means to both rotate and recollate pages in the same step. This meant that makebook had to run qpdf once to rotate the necessary pages, and then again to collate them in the correct order. I was very afraid that this would make makebook, already pretty slow due to the complexity of the operations it's performing, even slower.

But no fear! Indeed, qpdf was about three times faster than pdftk, even when it has to be run twice. I have a script which runs each type of signature on very similar documents; the timing on them couldn't be clearer:

Screenshot showing the
			 timing of a script running multiple instances of
			 makebook using pdftk
Screenshot showing timing of makebook with pdftk
Screenshot showing the
			 timing of a script running multiple instances of
			 makebook using qpdf
Screenshot showing timing of makebook with qpdf

Too often, using purely free software causes performance hits; these are usually going to be worth it, but it's still regrettable. Here, though, using the freer product also tripled the speed of the script, which was delightful and satisfying.

I hope, if you need to impose pages, you'll consider makebook; it does its job and does it well.