vladmeh/zf2-tcpdf

A Zend Framework 2 module for incorporating TCPDF support.

dev-master 2016-12-27 09:21 UTC

This package is auto-updated.

Last update: 2024-04-13 23:18:23 UTC


README

A Zend Framework 2 module for incorporating TCPDF support.

for Zend Framework 3

Build Status

Requirements

  • Zend Faramework 2 (version 2.5 and later)

Installation

Installation of TCPDFModule uses PHP Composer. For more information about PHP Composer, please visit the official PHP Composer site.

Installation steps

php composer.phar require vladmeh/zf2-tcpdf:dev-master

or

  1. cd my/project/directory

  2. create a composer.json file with following contents:

     {
         "require": {
             "vladmeh/zf2-tcpdf": "dev-master"
         }
     }
    
  3. install PHP Composer via curl -s http://getcomposer.org/installer | php (on windows, download (http://getcomposer.org/installer) and execute it with PHP)

  4. run php composer.phar install

  5. open my/project/directory/config/application.config.php and add the following key to your modules:

    'TCPDFModule',
    

Example usage

Side note: use of getServiceLocator() in the controller is deprecated since in ZF3. Make sure you create your controller via a factory and inject the TCPDF object in the constructor. Migration Guide (ZF3 to use the new version)

// module config: module\Application\config\module.config.php

<?php
namespase Application;

return array(
    'controllers' => array(
        'factories' => array(
            'Application\Controller\Index' => 'Application\Factory\IndexControllerFactory',
        )
    ),
    'router' => array(...),
    ...
)
// module\Application\src\Application\Factory\IndexControllerFactory.php

<?php

namespace Application\Factory;

use Application\Controller\IndexController;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class IndexControllerFactory implements FactoryInterface
{
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        $realServiceLocator = $serviceLocator->getServiceLocator();
        $renderer           = $realServiceLocator->get('Zend\View\Renderer\RendererInterface');
        $tcpdf              = $realServiceLocator->get('TCPDF');

        return new IndexController(
            $tcpdf,
            $renderer
        );
    }
}
// module\Application\src\Application\Controller\IndexController.php
<?php

namespace Application\Controller;

use TCPDF;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\View\Renderer\RendererInterface;

class IndexController extends AbstractActionController
{

    /**
     * @var TCPDF
     */
    protected $tcpdf;

    /**
     * @var RendererInterface
     */
    protected $renderer;

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

    public function indexAction()
    {
        $view = new ViewModel();
        
        $renderer = $this->renderer;
        $view->setTemplate('pdf');
        $html = $renderer->render($view);

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

        $pdf->Output();
    }

}