A Zend Framework 3 module for incorporating TCPDF support.
- Laminas framework
- PHP 7.4 || 8.0
-
Installation of TCPDFModule uses PHP Composer. For more information about PHP Composer, please visit the official PHP Composer site.
php composer.phar require valerialevenets/laminas-tcpdf
-
Open my/project/directory/config/modules.config.php and add the following key to your modules:
return [ ... 'TCPDFModule', ];
// module config: module\Application\config\module.config.php
<?php
namespase Application;
use Application\Factory\IndexControllerFactory;
return [
'controllers' => [
'factories' => [
Controller\IndexController::class => IndexControllerFactory::class,
],
],
'router' => [],
...
];
// module\Application\src\Factory\IndexControllerFactory.php
<?php
namespace Application\Factory;
use Application\Controller\IndexController;
use Interop\Container\ContainerInterface;
use Laminas\ServiceManager\Factory\FactoryInterface;
use Laminas\View\Renderer\RendererInterface;
class IndexControllerFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$tcpdf = $container->get(\TCPDF::class);
$renderer = $container->get(RendererInterface::class);
return new IndexController(
$tcpdf,
$renderer
);
}
}
// module\Application\src\Controller\IndexController.php
<?php
namespace Application\Controller;
use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\View\Model\ViewModel;
use Laminas\View\Renderer\RendererInterface;
class IndexController extends AbstractActionController
{
/**
* @var \TCPDF
*/
protected $tcpdf;
/**
* @var RendererInterface
*/
protected $renderer;
public function __construct($tspdf, $renderer)
{
$this->tcpdf = $tspdf;
$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();
}
}