Skip to main content
Image for post: Generate PDF documents using ZF2 and DOMPDF

Generate PDF documents using ZF2 and DOMPDF

If you are searching a library to generate PDF documents using Zend framework 2 in PHP, I suggest DOMPDF. You can integrate it with the framework and easily convert HTML documents to PDF. This is very useful if you have some data into your MySQL database and you want to create reports and documents. Just create a simple web page and the PdfModel class will render the page as a PDF file!

Install with composer

  "require" : {
    "dompdf/dompdf" : "0.6.*"

 Activate the module

Add 'DOMPDFModule' on your application.config.php file to activate the module or Zend wil not render your pages as PDF files but only as a simple HTML page.

Use the library

Create a controller like this:
namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use DOMPDFModule\View\Model\PdfModel;

class PdfController extends SetupAbstractController
    public function generatepdfAction()
        $pdf = new PdfModel();
        $pdf->setOption('filename', 'monthly-report'); // Triggers PDF download, automatically appends ".pdf"
        $pdf->setOption('paperSize', 'a4'); // Defaults to "8x11"
        $pdf->setOption('paperOrientation', 'landscape'); // Defaults to "portrait"

          'message' => 'Hello'

        return $pdf;
Activate the controller and create a directory with the view. Zend Studio is an IDE that can create all for you. If you have the generatepdf.phtml file, you will be able to render the page as a PDF document.