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.
Subscribe to:
Post Comments (Atom)
A view from the rack is the personal blog of an IT manager who works for a pub company - hence beer
13 comments:
Hi,
Just thought I would leave a note saying 'hi'. I am a bloke who does stuff with nice domain names and was looking at pub.co.uk / pubs.co.uk and found B + P own them!
I am from Wrexham (currently in australia) and am often in aldford / harkers / the pant so good to see them owned by someone decent!
cheers,
rob.
The domain name was purchased by a visionary well before my time, however I would like to accept your comment on their behalf and bask in the reflected glory :o)
K
Brilliant job! This FPDF text rotation code worked perfectly. You should submit it to the FPDF folks. Many thanks!
thank you very much :) it works very well, thanks again
Many thanks indeed, your code just made my life a whole lot easier!
Just wanted to say a quick thankyou for posting this up - it was just what I was looking for and was very easy to implement!
Thanks again!
Lee
Thank you very much! That's just what I needed, and it works flawlessly. Thanks again!
THX!!! You saved couple of hours of my workday!
Hmmm... most exquisite code!
May the force be with you... always.
You are my hero. I had a momentary panic attack when I found no hint of rotation in the FPDF docs... thanks for sharing this!
Thx a lot - simple code but perfect result. Just saved a working day figuring this out myself!
Thanks so much for publishing your effort. I will toast your existence with a nice cold beer.
Hey – nice blog, simply trying round some blogs, appears a pretty nice platform You Are using. I’m presently utilizing Drupal for a couple of of my sites however seeking to change one in every of them over to a platform very a lot the identical to yours as a trial run. Something specifically you'd advocate about it?
Post a Comment