tcpdf zend framework zf3 php pdf 

Creating PDF in zend framework 3 using tcPDF

How to create PDF's in Zend 3 using TCPDF.

1) Install a packing using Composer:

php composer.phar require vladmeh/zf3-tcpdf

I used this package to install Tcpdf or you can use your own way to install tcpdf.

2) In /config/modules.config.php - add TCPDFModule in the modules list

3) If you are using a Factory add this Module to the invoke method to pass to your controllers , see below code example.

update your module.config with the Factory if you already not done.

<?php
namespase Application;

use Application\Factory\IndexControllerFactory;

return [
    'controllers' => [
        'factories' => [
            Controller\IndexController::class => IndexControllerFactory::class,
        ],
    ],
    'router' => [],
    ...
];

4) and In IndexControllerFactory add in invoke method like below

use Zend\View\Renderer\RendererInterface;

 

public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
    {
        $tcpdf = $container->get(\TCPDF::class);
        $renderer = $container->get(RendererInterface::class);
        return new IndexController(
            $tcpdf,
            $renderer
        );
    }

we need renderer to render any views.

5) In indexController in _construct method call this tcpdf and renderer see below.

protected $tcpdf;
protected $renderer;

public function __construct($tcpdf, $renderer)
    {
        $this->tcpdf = $tcpdf;
        $this->renderer = $renderer;
    }

    public function indexAction()
    {
        $view = new ViewModel();

        $renderer = $this->renderer;
        $view->setTemplate('layout/pdf');
        $html = $renderer->render($view);

        $pdf = $this->tcpdf;

        $pdf->SetFont('arialnarrow', '', 12, '', false);
        $pdf->AddPage();
        $pdf->writeHTML($html, true, false, true, false, '');

        $pdf->Output();
    }

[source: https://packagist.org/packages/vladmeh/zf3-tcpdf]