How to let DomPDF and Zend Framework play along
For a recent project, I had to generate a PDF for a catalog. Since I’m using Zend Framework for my development, first thing that sprang in mind was Zend_Pdf. After some investigation, I found it too “difficult” to use. Flexible though that component is, I didn’t feel much for using coordinates to draw each and every line and text of that PDF. So I went to find another solution: DomPDF. DomPDF can generates PDF output, from HTML input. Exactly what I wanted and needed. It was however quite tricky to get it working. If you also have problems with this, read on!
DomPDF isn’t part of the Zend Framework library, and as such doesn’t really follow the ZF conventions. That’s what caused me trouble: I couldn’t load the needed classes for it to work.
So how do you set everything up properly?
First step: set include path
When you’re setting your include paths, just add one for DomPDF.
1 | set_include_path(APPLICATION_PATH . "/../../library/dompdf-0.5.1" . PATH_SEPARATOR . get_include_path()); |
Second step: make ZF & DomPDF play along
DomPDF has an autoloader which should take care of autoloading the classes for you. All you need to do, is make Zend Framework aware of that autoloader. The solution I found the most was this:
1 2 | require_once 'dompdf_config.inc.php'; spl_autoload_register('DOMPDF_autoload'); |
However this may not work if you use Zend Framework 1.8 or newer. The real solution I found on a forum, where Matthew Weier O’Phinney enlightened another soul with the same problem:
1 2 3 | require_once 'dompdf_config.inc.php'; $autoloader = Zend_Loader_Autoloader::getInstance(); // assuming we're in a controller $autoloader->pushAutoloader('DOMPDF_autoload'); |
Final step: generate the PDF
That’s the simplest one, as I found no problems with that:
1 2 3 4 5 6 | $dompdf = new DOMPDF(); $dompdf->set_paper("a4","portrait"); $dompdf->load_html($html); $dompdf->set_base_path($_SERVER['DOCUMENT_ROOT']); $dompdf->render(); $dompdf->stream("document.pdf"); |
There you go, this should set you up to get DomPDF up and running.




Thank you for sharing these services
This was a great help. My colleague and I were looking into this problem for over an hour, until we stumbled upon your Blog. Thank you.
Glad to be of service
Thank you !!!
This was a fantastic help, thank you!
I’m wondering if you know how to pass the output of a rendered ZF view script to DOMPDF?
I have an invoice template that renders XHTML output. I can view the output in my application by going here:
/invoices/view/id/234
Is there an easy way to grab that output and pass it to $dompdf->load_html($html) to convert it to a PDF?
Hello Peter,
That’s exactly what I’ve needed DomPDF for. Below is how I did it. The code should be in your invoices controller, view action. Via a URL parameter, you could switch to PDF code.
// We need a layout to render the view in (possibly a separate PDF layout)
$layout = new Zend_Layout();
$layout->setLayoutPath(APPLICATION_PATH . “/path/to/layouts”);
// Assign stuff to your view like this:
$this->view->foo = “bar”;
// Render the current view script:
$this->render();
// capture the response:
$output = $this->getResponse()->getBody();
// set that view script content as content of the layout:
$layout->content = $output;
$layout->setLayout(‘pdf’); // my custom layout is called ‘pdf.phtml’
$html = $layout->render();
That’s it, you can now use $html as input for DomPDF.
As an extra, I’ll also give you the code to just render any view script. You’ll see that it’s much shorter, and maybe you want to use that. It’s something I use in cron scripts to render e-mail templates, but it can be used for DomPDF as well:
// create a new view:
$view = new Zend_View();
$view->setScriptPath(APPLICATION_PATH . “/path/to/view/scripts”);
// assign stuff to the view:
$view->foo = “bar”;
// render the view:
$html = $view->render(‘pdf.phtml’);
Ive been trying your approach but I keep getting corrupted files with the html content coming before the pdf. I can disable the layout with $this->helper->layout->diableLayout(); to just get the view scipt html code but I cant figure out how to get the view in the pdfs layout content area.
Have you tried my second method? That’s a more compact approach. Of course, you lose the benefit of a layout, but I could live with that.
Could you post what you have, then maybe I can help you with fixing it.
@Tom
Here is the html that is added to the file before the pdf header.
———-
Warning: file_put_contents(/var/www/vmnc/library/dompdf/lib/fonts/php_Times-Roman.afm) [function.file-put-contents]: failed to open stream: Permission denied in /var/www/vmnc/library/dompdf/lib/class.pdf.php on line 2354
————-
Im not sure what that means but is enough to corrupt the file so it wont be rendered by some pdf viewers.
Thanks I appreciate your help.
Great!
Works fine.
I’ve been trying your approach that you have suggested to Peter but i can’t seems to get it working.
function viewAction() {
layout = new Zend_Layout();
$layout->setLayoutPath(APPLICATION_PATH . “/views/scripts/invoice”);
$this->view->foo = “test data”;
$this->render(); /* i can render my view.phtml */
$layout->content = $output;
$layout->setLayout(‘pdf’);
echo “
"; print_r($layout); $html = $layout->render(); } When i render the layout it show following error: Message: Requested HTML document contains no data. following is my pdf.phtml code $dompdf = new DOMPDF(); $dompdf->set_paper("a4","portrait"); $dompdf->load_html($html); $dompdf->set_base_path($_SERVER['DOCUMENT_ROOT']); $dompdf->render(); $dompdf->stream("document.pdf");