New approach to generate PDF using PHP with Zend Framework

Recommend this page to a friend!
  PHP Classes blog     New approach to gener...   Post a comment   See comments (22)   Trackbacks (0)  


Viewers: 1,064

Last month viewers: 4

Categories: PHP Tutorials

LiveDocx is a Zend Framework package that implements a new approach to generate PDF documents in PHP.

It combines structured data passed by a PHP script with a template created in a word processor. It is the same concept as mail merge.

The resulting document can be saved as a PDF, as well in other formats such as DOCX, DOC or RTF file.


* Generating PDF using PHP

* A new approach to PDF generation

* Availability of this solution

* Learn more

* About the author

* Generating PDF using PHP

Generating ready to print and well formatted PDF documents with PHP is not an easy task.

There are two usual approaches to generate PDF using PHP. Both get the job done given enough time and patience, but still leave a lot to be desired.

1. HTML to PDF

This approach is widely used in mainstream applications. An HTML document is programmatically created and converted into a PDF using one of the many Open Source libraries, such as: domPDF, HTML 2 (F)PDF, HTML_ToPDF, mPDF, etc..

However, it is impossible to perform a one to one mapping between HTML and PDF, as HTML is not a page oriented format, unlike PDF.

Typical word processing document features, such as header and footers, orphans and widows, or even page numbers simply cannot be represented in HTML.

2. Programmatic

This approach offers total control of the resulting PDF. However, it requires that you compute the coordinates of every line of text and every other document object in the pages.

Not only is this an extremely time consuming solution, but is also very brittle. Every time a graphical designer changes the layout of a document, a programmer must rework his code.

There are several libraries that use this approach, such as: Zend_Pdf, PDFlib, FPDF, Cpdf, etc.

* A new approach to PDF generation

LiveDocx is a solution offers an entirely new approach. It relies on templates being created in a WYSIWYG environment, such as Microsoft Word or Open Office. Then the documents are populated with data in PHP.

The resulting document can be saved not only to PDF, but also DOCX, DOC and RTF.

The following PHP code illustrates how to generate PDF by merging the values of the fields "software", "licensee" and "company" in the template with data from the PHP script.

$phpLiveDocx = new Zend_Service_LiveDocx_MailMerge(
'password'=> yourPassword'


$phpLiveDocx->assign('software', 'Magic Graphical Compression Suite v1.9');
$phpLiveDocx->assign('licensee', 'Henry Döner-Meyer');
$phpLiveDocx->assign('company', 'Megasoft Co-operation');


$document = $phpLiveDocx->retrieveDocument('pdf');

file_put_contents('document.pdf', $document);

The resulting document is stored in a file.

Using a template in DOCX format: ...

It generates a PDF document like this: ...

* Availability of this solution

The Zend_Service_LiveDocx package will be shipped with the Zend Framework 1.10 when it becomes available. Although at the time of writing, there is no official release date, Zend Framework 1.10 is expected to be released in fourth quarter of 2009.

In the meantime, you can check the components out of the Standard Incubator SVN repository.

* Learn more

This article just briefly scratched the surface of what is possible with LiveDocx in PHP.

To learn more about remote and local templates, documents containing repeating sections, such as rows of a table, take a look at the following article: ...

If you want to request free technical support about this package, please go to this page:

* About the author

Jonathan Maron has been working in the Web application conceptualization and development space since 1996.

His current position is with an international company, specialized in the production of word processing components.

Being an advocate of the FOSS movement, Jonathan promotes the use of Open Source software and rejects the notion of reinventing the wheel, preferring to develop using established frameworks.

You may contact him by going to this page:

Feel to post a message here if you have questions or want to make any other kind of comments.

You need to be a registered user or login to post a comment

Login Immediately with your account on:



8. it's not safe.... - lc (2013-09-23 03:49)
this solution is not safe for futur.... - 2 replies
Read the whole comment and replies

10. Pass image files? - PHP-4-Business (2013-09-04 02:35)
Can image files be passed - either as data or a URI?... - 0 replies
Read the whole comment and replies

9. support hindi unicode - venga (2009-10-12 05:40)
support hindi unicode?... - 1 reply
Read the whole comment and replies

2. Free or paid? - Peter Krajcar (2009-10-09 14:39)
Impressive... - 2 replies
Read the whole comment and replies

7. Excellent Tutorial - Which ZF Version? - Keith Hall (2009-10-09 04:20)
Very good tutorial on a great new approach, ? about ZF version... - 1 reply
Read the whole comment and replies

6. Amazing - Marcos Bezerra (2009-10-09 04:19)
Zend Framework always help us... - 1 reply
Read the whole comment and replies

5. It's just a service wrapper - Igor Blanco (2009-10-09 03:59)
It's just a service wrapper... - 1 reply
Read the whole comment and replies

4. Nice but ... - eddy maddix (2009-10-09 03:58)
docx file crashes Open Office 3.0.0 Build:9358... - 1 reply
Read the whole comment and replies

3. Awesome! - Hugo Ferreira (2009-10-09 03:58)
Great library...... - 1 reply
Read the whole comment and replies

1. LiveDocX - Bill Dawes (2009-10-09 03:57)
Very very cool!... - 2 replies
Read the whole comment and replies

  PHP Classes blog     New approach to gener...   Post a comment   See comments (22)   Trackbacks (0)  

For more information send a message to info at phpclasses dot org.