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...

Update:
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.

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

Posted by Julien on May 12, 2004. Permalink
Comments
comments powered by Disqus

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 at 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. at June 23, 2004 12:49 AM

PDML ROCKS!

Posted by: Jose Quervo at 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 at 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 at 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 at January 2, 2005 05:36 PM
Trackbacks
PDML
Excerpt: PDML, or Portable Document Markup Language is a PHP library used to generate PDF documents using a markup language resembling HTML. So far, PDML features paging, cells, column layout, geometric shapes, headers, footers, and more. Documents are rendered by
Weblog:
Tracked: May 12, 2004 10:27 PM
PDML->PHP->FPDF->PDF ! SVG->PHP->MING->FLASH ?
Excerpt: ”PDML is an informal markup language written in 100% PHP, allowing to create complex PDF documents very easily for people already familiar with HTML. People familiar with PHP can also use it as a way to define templates for dynamic PDF documents....
Weblog: Znarf Infos - le carnet web
Tracked: May 13, 2004 03:32 AM
Writing the simplest code (Part2)
Excerpt:
Weblog: Cyrus' Blather
Tracked: May 16, 2004 06:57 PM