If you’re using Zend Framework 2 (beta 4 beta 5) and need to easily generate PDFs, and you’d like to generate those PDFs from an HTML template, then boy do I have some good news for you!

For those of you who have dealt with generating dynamic PDFs, you know how much of a pain in the ass it can be (e.g. dealing with x, y coordinates, word-wrap (or lack of), etc.). There are a few options out there, some paid, some free, but none of them are as nice as DOMPDF (simple API, powerful, and free).

The true power behind DOMPDF comes from rendering standard HTML/CSS files instead of having to write ugly and unmanageable code.

Excerpt from their usage docs:

That’s it. That will take your HTML and stream a PDF file to the end-user. Awesome, right?

Ok, let’s bring this back around to ZF2. About a month or so ago I started using ZF2 beta4 for a new project that I’m working on and wouldn’t you know it, I have to generate PDF reports, receipts, etc. I had a choice; I could just drop in DOMPDF, ignore the conventions set by ZF2, and move forward with my work, or I could spend a little extra time and create a clean and elegant solution that my project,  future projects, and the ZF2 community could benefit from.

What I came up with is DOMPDFModule. A drop-in 3rd-party module that wraps DOMPDF and takes full advantage of ZF2′s implementation of the ViewModel pattern.


That’s it. The proper headers and template rendering happens behind the scenes. If you’d like the browser to prompt the user where to save the PDF file, call:

But wait there’s more! The installation of DOMPDFModule couldn’t be any easier, utilizing the increasingly popular PHP Composer dependency manager via Packagist repository. See for installation instructions.

17 thoughts on “DOMPDF in ZF2

  1. Wilmer Arteaga Izquierdo

    Hi. I'm trying to use this module but I have a error :
    Fatal error: Class 'DOMPDF' not found in C:xampphtdocsantimicrobianovendordinoDOMPDFModulesrcDOMPDFModuleViewRendererPdfRenderer.php on line 77.
    I installed dompdf via composer but dompdfmodule manually because composer couldn't install.

    1. Vinicius Oliveira Garcia

      Hi Raymond Kolbe! Thank for your response.
      I followed the I found here:
      I create a empty folder called 'DOMPDFModule'…
      after that, I created a composer.phar file like this:
      "require": {
      "dino/DOMPDFModule": "dev-master"
      Then I execute 'C:wampwwwDOMPDFModule>php composer.phar install' and got the following error:
      >Installing dependencies
      >Your requirements could not be resolved to an installable set of packages.
      > Problem 1
      > – The requested package dino/dompdfmodule dev-master could not be found.
      I couldn't find this 'dino/dompdfmodule', but I found 'dino/dompdf-module'…
      Did I do something wrong?

    2. Vinicius Oliveira Garcia

      ** When I said 'composer.phar' I mean 'composer.json'…

      Now, I changed the composer.json file to:
      "require": {
      "dino/dompdf-module": "dev-master"
      but I got a GIT error:
      >Installing dependencies
      > – Installing dino/dompdf (v0.1.1)
      > Downloading: 100%
      > – Installing dino/dompdf-module (dev-master f10d3c7)
      > Cloning f10d3c7f411120918b6cb72439cd2402bbd58f79
      > [RuntimeException]
      > Failed to clone, git was not found, check that it is installed and in your PATH env.
      > 'git' não é reconhecido como um comando interno ou externo, um programa operável ou um arquivo em lotes.

    3. Raymond Kolbe

      Ah yes. I do need to update the readme. However, the error you are getting in your second post doesn't appear to be an issue with my packages. Try issuing composer.phar self-update and try again. I'm curious to know if the git error is a composer error, or a configuration error on your system.

  2. Jonathan Schmid

    Hi Raymond, thank you so much for your great work! I just wondered why you don't put the module class directly into Module.php instead of including a second one?

Leave a Reply