Generate PDF using PHP with Zend Framework
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:
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:
Comments