Thursday 12 July 2007

Pdf Trials

As soon as a programmer creates a program which stores information, at some point someone is going to want get at it (the information that it), print it, send it to someone else or just take a copy. In other words one requires a portable document.

In our little in-house programs we have been using Excel a lot, it is ideal for presenting raw data to IT savvy managers as quite often the first thing they want to do is chop the data around, turn it upside down, rattle it and maybe analyse it against another spreadsheet. Excel begins to struggle when it comes to printing and presenting a better looking sort of document so we have begun to look at creating PDF documents for some of our reporting and printing requirements.

The first thing you find when you are looking for a program to create PDF files is a library called PDFLIB made by a company called ... PDFLIB. They have products for programmers who use C, Java, Delphi and our own favourite PHP. The upside is that it is quite comprehensive offering the vast majority of the elements defined in the PDF format, the downside is that it costs about £1000. Given the price tag I decided to dig a little deeper and turned up a product called FPDF where the F stands for Free, a much more palatable entry in the IT budget.

So Having installed the library effortlessly and tried a few of the examples I was impressed, the functions are limited but actually it seemed to cover everything I needed. The next step was to dive into probably the most complicated PDF file we have and up until the very last element it was all going so well, attempting to write vertical text knocked the wheels off the wagon however. Was vertical text going to cost a grand!?
Back to Google with a fresh cuppa and given that the PDF Format is freely published I thought maybe I should try my hand at extending the FPDF lib and adding the function for rotated text myself. The problem is it has been 15 years since my A level maths teacher force fed me matrix transformations and Adobe might have published the format and described the matrix transformations but they don't really 'throw you a bone' when it comes to programming them.

More tea and some inspiration from a tunnocks tea cake turned up a pear module which is in a slightly fragile beta 0.2 state but essentially builds on FPDF and includes a rotate_text function - File_PDF. Being a pear module, installation was easy but could I get it to work! Alas no. However could I lift the bonnet on both these php projects and force them into an unholy union, why yes sir! So this is my contribution to the open source community, if you are using FPDF and need the ability to rotate some text paste the following code into the fpdf.php file and call it using the last line of code.


function writeRotie($x,$y,$txt,$text_angle,$font_angle = 0)
{
if ($x < 0) {
$x += $this->w;
}
if ($y < 0) {
$y += $this->h;
}

/* Escape text. */
$text = $this->_escape($txt);

$font_angle += 90 + $text_angle;
$text_angle *= M_PI / 180;
$font_angle *= M_PI / 180;

$text_dx = cos($text_angle);
$text_dy = sin($text_angle);
$font_dx = cos($font_angle);
$font_dy = sin($font_angle);

$s= sprintf('BT %.2f %.2f %.2f %.2f %.2f %.2f Tm (%s) Tj ET', $text_dx, $text_dy, $font_dx, $font_dy,$x * $this->k, ($this->h-$y) * $this->k, $text);
if($this->underline && $txt!='')
$s.=' '.$this->_dounderline($x,$y,$txt);
if($this->ColorFlag)
$s='q '.$this->TextColor.' '.$s.' Q';
$this->_out($s);
}


// Write 'Soup' at grid ref 50x50 at a 90 degrees rotation
$pdf->writeRotie(50,50,"Soup",90,0);



As ever one hour of the day accomplished 95% of the job, the rest of the day was spent chasing around after a seemingly straight forward willow the wisp of a function.

A view from the rack is the personal blog of an IT manager who works for a pub company - hence beer