Curiosity is bliss    Archive    Feed    About    Search

Julien Couvreur's programming blog and more

PDML PHP tricks


Portable Document Markup Language (PDML) is a library to output PDF using PHP and an HTML-like markup language.
The "getting started" page describes how to use it: install the library, create a PHP file starting with <?php include "pdml.php" ?> and start outputting PDML.
The PDML will be automatically converted to a PDF file using the FPDF class.

I was curious how this works, as it seemed pretty different from previous similar libraries (PDFLib, FPDF), that offered a programmatic API to generate PDF DOMs instead of a markup language.

Looking at the very end of pdml.php you can notice a call to ob_start("ob_pdml"), with ob_pdml being defined above as function ob_pdml($buffer).
That turns out to be the key. Check out the documentation for the ob_start PHP function.

ob_start will turn output buffering on and will give the callback function a chance to process it. That's the trick for this seamless custom markup conversion using PHP ;-)

I wonder if Flash could be similarly outputted from PHP using a markup language (that would wrap the Ming library).
There could also be an Excel markup language, based on PHP Spreadsheet_WriteExcel, or an image markup language to generate PNG and JPEG image using the GD graphics library.

Update: Simon and some commenters on his site came up with other uses of this custom markup language technique: running XSLT or Html Tidy on this second layer of processing.
Harry Fuecks mentioned a very cool technique to have Apache automatically prepend the file with the extra PHP tag (using php_value auto_prepend_file myPhpInclude.php in the .htaccess).

I wonder if something like ob_start is available in ASP.Net...

ASP.Net does support a similar "second pass processing" technique. The "Intercept and modify the output created from a ASP.Net page" article describes how to do that. This relies on setting an output Filter on the HTTPResponse class.
Using inline aspx, it should be possible to have something very similar to PDML. I guess it would even be possible to write server-side controls to output blocks.

Gzip your CSS using the ob_start PHP trick (and the auto_prepend_file configuration in Apache).


I think using this library with Smarty templates would also lead to some nice reusable components. I've posted a few ideas on my PHP blog.

Posted by: John Herren (May 12, 2004 10:36 PM) ______________________________________

Great ideas, somthing like 'PDML PHP tricks' was on my list of things to look for today. Thanks!

Posted by: Mike P. (June 23, 2004 12:49 AM) ______________________________________


Posted by: Jose Quervo (July 21, 2004 09:09 PM) ______________________________________

I was at the MVP Summit welcome reception tonight. It was awesome to finally meet some of the people I've been talking to on-line for a long time. It was also cool to talk to all the new people I didn't know beforehand. At least three people read my name tag and say “Are you Gretchen's husband? I love her blog.” I'll be at some of the developer track talks tomorrow and the dinner in the evening.

Posted by: chian blog (July 30, 2004 07:26 AM) ______________________________________

Thanks for the interesting articles and the reading. Unfortunately I'm still incredibly lost on PDML and stuff. Would anyone mind if I e-mailed them a couple questions?

Posted by: Sean (August 30, 2004 12:47 AM) ______________________________________

Is this blog still active? I am interested in more about PDML and I would like to send email you a few questions(or maybe just point me in the right direction). Thanks!

Posted by: Dan (January 2, 2005 05:36 PM)
comments powered by Disqus