Printing reports...

Posted by Roy Hooper Sat, 17 Feb 2007 18:48:00 GMT

You’d think one of the things that would have been made easy in Visual Basic was creating/printing reports.

Unfortunately, that doesn’t seem to be the case. Printing support hasn’t changed much from the early days. They’ve just moved stuff around. You still have to do the hard work like word wrapping yourself. Which just seems to me to be reinventing the wheel.

Oh, you can do things like create a WebBrowser object, and print from that, but that seems reliable only for a single page at a time. Attempts to automate it were slower than 1/sec for a batch process, because I had to ensure I waited sufficient time for the page to render. Yes, there is an event that’s fired that you can use to time when to start the .Print() method… but you also have to get the timing right as to when you refresh the content to load a new page to print. Then there’s the standard IE print headers and footers. Without adding a commercial ActiveX component like ScriptX (which incidentally, seems not to be straightforward to use in VB2005), you have to modify the registry to remove them.

In the end, I gave up on trying to do the batch printing via VB. Crystal reports would have worked, I guess, but that’s just a different source of frustration. Oh, did I mention that to include bar codes reliably, I’d have to roll my own, or license a commercial font?

My solution? PDF::API2::Simple and PDF::API2, which allows me to easily create PDFs. PDF::API2 is poorly documented, but very powerful. It even includes a number of barcode formats, including 3of9 which I have been using, as well as code128 which I had been thinking about. PDF::API2::Simple provides an easier to use interface to the PDF::API2, including a very nicely written text placement routine that does word wrap for me. Fortunately, it also allows direct access to PDF::API2 so you can still place that barcode.

If you want to create barcodes with PDF::API2 you’ll want to check out AnnoCPAN where some rather helpful notes have been added.

Net result? Small files, fast generation of documents, and no frustrations surrounding reprint control, timing of print documents, overflowing the print spool, etc. I can now take all the work of creating a batch print job to the server end, and there’s no client software to install other than Acrobat, which everyone needs anyhow.

Trackbacks

Use the following link to trackback from your own site:
http://blog.royhooper.ca/trackbacks?article_id=31