Latest posts

Delivering pages as PDF using PHP

HTML is great. It's the lingua franca of the web, and a fantastic format for exchange of hyperlinked information. However, it has its drawbacks - It typically relies on multiple external files, different browsers interpret it in different ways, and printing it is a bit of a minefield, even with the limited print CSS currently available.

So, sometimes it makes sense to present documents as a PDF as well. I've done so on this very site, with my CV, after finding that most recruitment sites won't except an HTML document, and recruiters just get confused when you attach one to an email (or send them a hyperlink).

The component I use is called dompdf. At its heart it is an HTML->PDF converter written completely in PHP, and is pretty simple to use. The code to convert some HTML to a PDF looks something like this:

<?php

// include in the dompdf library
require_once('dompdf_config.inc.php');
spl_autoload_register('DOMPDF_autoload');

// instance dompdf
$dompdf = new DomPDF();
$dompdf->set_paper('a4');

// tell the user-agent to expect a PDF
header('Content-type: application/pdf');

// load the HTML, convert it to PDF and output
$src file_get_contents('document.html');
$dompdf->load_html($src);
$dompdf->render();
echo 
$dompdf->output();

?>

Optimising a site for iPhone

Screengrab of my site on iPhone

The unmodified site

The first thing I should probably mention is that yes, despite saying I wouldn't, I got an iPhone 3G relatively soon after release. I'll skip the reasons why for now, that's for a future posting. Naturally the first thing I did upon getting my iPhone was to plug in my own site's URL into the browser and see how it did - you can see the result in the screengrab.

You know what - it's not bad. The site looks roughly as it should, and with a bit of zooming and panning around all the content is accessible. However, on first load the text isn't really legible and it's not making the best usage of the limited screen space on the iPhone. Also, the large header graphic makes loading a bit strenuous over an EDGE connection when I'm not in 3G coverage.

Simon has been blogging recently (here and here), about 'mobilising' websites (and has written a good article on the subject for php|architect) so it's something I've been thinking about lately and I decided to see what it would take to improve the way the page was rendered on iPhone.

Cracking a monkey puzzle

The Guardian Crossword

Today saw my debut over at Fifteensquared as one of their daily crossword blogging team. About once a week I'll be blogging a solution for the Guardian crossword, hopefully reasonably early on the day of publication.

It's been an interesting experience. On a normal day I'll buy the Guardian in the morning and do a couple of clues on the way in to work, then have it sitting on my desk for an occasional glance through the morning. When I have a free lunch I'll finish it off, usually with a bit of help from Paul if he's online. I hadn't realised how much pressure I'd be adding to myself by committing to solve it on a certain day.

I woke up this morning in a vague panic about whether I'd manage to fit it in. When I got the paper at my local corner shop, I turned straight to the crossword page and my heart sank - today's setter was Araucaria, possibly the hardest setter the Guardian use and certainly the most inventive. I normally consider it a bit of a triumph to finish his puzzles by the end of the day, let alone in the morning.

Some thoughts on Zend PHP5 Certification

ZCE logo

I qualified as a ZCE yesterday. I guess the only tangible benefits of this is I get to write the letters on my CV / business cards and use this nifty logo, but I found the overall process of the certification pretty worthwhile.

The thing that's put me off certification in the past is the imagined cost, in terms of both time and money. I thought there'd be a dreary course covering things I already knew, and I'd be paying a premium for the privilege. In fact, I was completely wrong!

Once I looked into it, I found that although Zend offer a grueling 18-hour course for about £800 it's not a requirement for the certification itself, you can buy an exam voucher for about £100 and just turn up and take the test, so I did.

A new comments system

I've made a little comments system for this blog, and it gave me a chance to look a bit more into Doctrine.

I keep meaning to blog about how I put this site together, but for now I'll just say that it uses Zend Framework for MVC, with Doctrine for the ORM. To implement the comments form I used a great bit of code from CodeUtopia, written by Jani Hartikainen (a.k.a. 'zomg' on IRC) that ties together Doctrine objects and Zend_Form in quite a nice way.

I'm hoping to do a quite simple tutorial about how to implement a small blog, but that's for another time when I'm not busy revising for my ZCE!