Sunday, September 16, 2007

Voodoo Programming

One of the reasons I’m a consultant is because I love to solve problems; this does not mean, however, that I enjoy all the problems I solve, nor that the pursuit is always rewarding in itself. This week I got stuck in a mind-bender that had all the satisfying crunch of a soggy pretzel.

I was writing code to produce a simple label in PDF format. The code isn’t tricky, in fact, it’s a simplified version of what I use to generate invoices. For the label project I used Inkscape to create a dashed cut-out box (think coupon) measuring 11mm high and 95mm wide, along with some ancillary text; I saved this as label.pdf. The code I wrote accepted two lines of text from a web form and wrote these inside the cut-out box using PDF::API2 and sent the resulting PDF to the browser. The result should have been a two line label ready to be printed and cut out. It wasn’t.


Despite careful calculations, the text missed. I double checked my math to no avail. I did voodoo, cast runes, and waved a dead chicken, with surprisingly little success. Once I had some success by saving the Inkscape file to postscript (*.ps), then converting it with ps2pdf; it only worked the one time, ditto with encapsulated postscript (*.eps). After wasting most of a day in this fashion, I finally succeeded in getting my text to print in somewhat reliable positions, but only upside down and reversed-- note that I don’t mean reversed from right to left, the individual characters were reversed. Fortunately, this behavior should have been impossible, which meant I was close to solving my problem.


As used here, the word “solving” has what may be a non-intuitive use. To “solve” this problem I really only needed to avoid it, which is to say that this problem was an impediment to creating a PDF and removing said impediment was the only requirement of a successful solution, understanding the cause was optional. A few quick tests proved that the source of my woes was Inkscape’s PDF and (E)PS output. I recreated my template with Scribus and it worked flawlessly. I still don’t know with certainty that the fault lies with Inkscape and not PDF::API2, and I don’t really want to know that much about PDF internals. Inkscape is still my preferred vector drawing program just as Scribus is my preferred layout program, it just happens that for creating PDF templates for use with PDF::API2, Scribus is the path of least resistance.