Showing posts with label PHP Excel Parser. Show all posts
Showing posts with label PHP Excel Parser. Show all posts

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.

Wednesday, 4 April 2007

Some Excellent Manipulation

An interesting little job came up yesterday which involved formatting data on an excel spreadsheet, we have some lists which have to look pretty but are edited frequently and we were having to spend a lot of time ensuring that these lists had a reliable and consistent format. Lots of ideas spring to mind for a job like this and the temptation is to go for yet another little database application but in this case it really felt like it would be overkill.

The solution which appears to have legs is to create a rather niffty excel parser using a couple of useful PHP add ons. For those of you who don't know what a parser is the definition on wikipedia is "the process of analyzing a sequence of tokens to determine its grammatical structure", in lay mans terms think of it a s a digester of documents. You push one in one end and it reads it, digests it and magically supplies a result, or in this case a completely reformatted spreadsheet. This will allow us to keep our data in a very simple unformatted spreadsheet, but by running them through our new system, we can have a nicely formatted consistent look ready to print in a click. Its all summed up nicely by another of my random dips into Google images, this time for the word "parse" see image right.

So if anyone ever has a need of such a beast or, more likely, if in 6 months time I have forgotten what I did and need a reference, the 2 places to go are PEAR for the excel spreadsheet writer add on and Source forge for the Excel Reader add on. When these are installed and working individually there is no reason why they cannot be used in the same PHP script in a push-me pull-you sort of fashion. It only took a couple of hours to get a basic system running and you can even allow the user to specify some parameters with their spreadsheet. So for example you pass a raw sheet of data, a title, a font and a relative font size and the parser running through the writer will apply sizing's and fonts in defined ways to different columns of data, it will even specify margins and printing areas so the document is completely ready to roll.

Keep tuning in for the definitive guide to installing Nagios on Centos without 'going postal' later this week.

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