Home > Zend Framework > How to let DomPDF and Zend Framework play along

How to let DomPDF and Zend Framework play along

August 27th, 2009

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.

Share and Enjoy:
  • DZone
  • del.icio.us
  • StumbleUpon
  • Digg
  • Ma.gnolia
  • Technorati
  • TwitThis

Tom Zend Framework , ,

  1. September 28th, 2009 at 19:09 | #1

    Thank you for sharing these services

  2. October 20th, 2009 at 16:15 | #2

    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.

  3. October 23rd, 2009 at 15:32 | #3

    Glad to be of service :)

  4. Romanitch
    November 13th, 2009 at 10:49 | #4

    Thank you !!!

  5. Peter
    January 5th, 2010 at 21:25 | #5

    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?

  6. January 6th, 2010 at 00:56 | #6

    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’);

  7. Michael D
    January 18th, 2010 at 07:51 | #7

    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.

  8. January 21st, 2010 at 00:52 | #8

    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.

  9. Michael D
    January 23rd, 2010 at 03:31 | #9

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

  10. March 9th, 2010 at 16:22 | #10

    Great!

    Works fine.

  11. nomad
    April 5th, 2010 at 09:52 | #11

    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");
  1. No trackbacks yet.