Create PDF file in ZF2?

I am starting a new project, and I'm also quite new to Zend Framework 2. What are your suggestions for creating pdf code from the html code that you want to pass as a variable ($ html)? I read online that DOMPDF is pretty good, but I'm not sure how to use it while looking at the documentation. What would be the easiest way for beginners?

+4
source share
3 answers

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

Step 1: Download DOMPDFModule

"dino/dompdf-module": "dev-master" composer.json

:
composer.json

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

, . , , include ( "dompdf.php" )

:

php composer.phar install

DOMPDFModule config/application.config.php

:
application.config.php

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

.

2: Zend Framework 2

, PDF , DOMPDFModule\View\Model\PdfModel;
; .

:
IndexController.php

<?php
namespace Application\Controller;

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

PDF .

:
, ; pdf .
example.com/generatepdf

generatepdfAction(), PDF . IndexController.php

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

Zend Framework 2; generatepdf.phtml, HTML-, PDF.

:
generatepdf.phtml

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

, , ; PDF , PDF.

generatepdfAction() setVariables() PdfModel()

:
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>

PDF. setOption() PdfModel().

:
IndexController.php

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

(.pdf) .

PaperSize paperOrientation

:
IndexController.php

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

, .

+18

: http://www.mpdf1.com/mpdf/index.php

pdf html. css html, . .

:

include('../mpdf.php');

// Create an instance of the class:
$mpdf=new mPDF();

// Write some HTML code:
$mpdf->WriteHTML('<p>Hello World</p>');

// Output a PDF file:
$mpdf->Output();
exit;

Manual

+2

If you use DOMPDF, there is a module, DOMPDFModule for Zend Framework 2, which does the creation of a piece of cake in pdf format.

<?php

namespace Application\Controller;

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

class ReportController extends AbstractActionController
{
    public function monthlyReportPdfAction()
    {
        $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"

        // To set view variables
        $pdf->setVariables(array(
          'message' => 'Hello'
        ));

        return $pdf;
    }
}

Alternatives

You can use Snappy , which uses wkhtmltopdf to generate PDF from a URL or html page. For Zend Framework 2, there is a MvlabsSnappy module .

0
source

Source: https://habr.com/ru/post/1540841/


All Articles