Login   Register  
PHP Classes
elePHPant
Icontem

New approach to generate PDF using PHP with Zend Framework - PHP Classes blog

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog New approach to gener...   Post a comment Post a comment   See comments See comments (22)   Trackbacks (0)  
<< Previous: Microsoft promotes th...>> Next: Submit your site rede...

Author: Jonathan Maron

Posted on:

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.




Contents

* 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(
array(
'username'=>'yourUsername',
'password'=> yourPassword'
)
);

$phpLiveDocx->setLocalTemplate('template.docx');

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

$phpLiveDocx->createDocument();

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

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


The resulting document is stored in a file.

Using a template in DOCX format:

http://www.phplivedocx.org/wp-content/uploads/2009/01/licens ...

It generates a PDF document like this:

http://www.phplivedocx.org/wp-content/uploads/2009/01/licens ...


* 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:

http://www.phplivedocx.org/articles/pdf-generation-with-zend ...

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

http://www.phplivedocx.org/support/


* 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:

http://www.phplivedocx.org/contact/


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

1,282,072 PHP developers registered to the PHP Classes site.
Be One of Us!

Login Immediately with your account on:

Facebook ConnectGmail or other Google Account
Hotmail or Microsoft Windows LiveStackOverflow
GitHubYahoo


Comments:

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


<< Previous: Microsoft promotes th...>> Next: Submit your site rede...

  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog New approach to gener...   Post a comment Post a comment   See comments See comments (22)   Trackbacks (0)