luifer99 luifer99 - 3 months ago 74
PHP Question

Generate PDF in ZF2?

Good day to all. I'm starting a new project, and I am also fairly new to Zend Framework 2.
What are your suggestions to generate a pdf from an html code that you want to pass as a variable ($html)?
I was reading the web that DOMPDF is pretty good, but I'm not sure how to use it, looking at documentation.
What would be the easiest way for a beginner?

Thank you very much for reading!

Answer

Your selection of DOMPDF is right. It's the most easiest to use in Zend Framework 2. Just add a simple module(DOMPDFModule) and you are ready to go. As you are a newbie to Zend Framework 2; here is how to use it.

Step 1: Downloading DOMPDFModule

Add "dino/dompdf-module": "dev-master" to the require section in composer.json file

Example:
composer.json

"require": {
    "php": ">=5.3.3",
    "zendframework/zendframework": "2.2.*",
    "dino/dompdf-module": "dev-master"
}

Now install or update the dependencies using composer. Composer will automatically include the dependencies which we would otherwise include using include("dompdf.php")

Example:

php composer.phar install

Next we have to add DOMPDFModule to the modules section in config/application.config.php

Example:
application.config.php

'modules' => array(
    'Application',
    'DOMPDFModule',
),

And we are ready to use it in our project.

Step 2: Using it in Zend Framework 2 Project

Open up the controller in which we are going to generate the PDF and add
use DOMPDFModule\View\Model\PdfModel;
to the top; after the namespace.

Example:
IndexController.php

<?php
namespace Application\Controller;

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

Next we have to place the necessary PDF generation code to the required action.

Example:
I'm assuming; to download pdf we are using the following route.
example.com/generatepdf

For that create a generatepdfAction() method in which we will place the PDF generation logic as follows.

IndexController.php

public function generatepdfAction()
{
    $pdf = new PdfModel();
    return $pdf;
}

As most Action in Zend Framework 2; this action will also have a corresponding view file generatepdf.phtml where we actually place the HTML which is to be converted to PDF.

Example:
generatepdf.phtml

<html>
  <body>
      <h1>My Name is Blah</h1>
  </body>
</html>

Now if we go to the route we created; a PDF will be shown in the same window if the browser supports viewing PDF.

Passing variables to view file

It can be done in generatepdfAction() by using the setVariables() method of PdfModel()

Example:
IndexController.php

public function generatepdfAction()
{
    $pdf = new PdfModel();
    $pdf->setVariables(array(
      'name' => 'John Doe',
    ));
    return $pdf;
}

generatepdf.phtml

<html>
  <body>
      <h1>My Name is <?php echo $this->name; ?></h1>
  </body>
</html>

Setting filename and forcing download

Setting filename will trigger auto download of the PDF file. A filename can be set using the setOption() method of PdfModel().

Example:
IndexController.php

public function generatepdfAction()
{
    $pdf = new PdfModel();
    $pdf->setOption("filename", "my-file");
    return $pdf;
}

File extension(.pdf) will be added automatically.

Setting paper size and paper orientation

Paper size and paper orientation can be set by the paperSize and paperOrientation options

Example:
IndexController.php

public function generatepdfAction()
{
    $pdf = new PdfModel();
    $pdf->setOption("paperSize", "a4"); //Defaults to 8x11
    $pdf->setOption("paperOrientation", "landscape"); //Defaults to portrait
    return $pdf;
}

Hope this helps.