PHP code example of valerialevenets / laminas-tcpdf
1. Go to this page and download the library: Download valerialevenets/laminas-tcpdf library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
valerialevenets / laminas-tcpdf example snippets
// module config: module\Application\config\module.config.php
namespase Application;
use Application\Factory\IndexControllerFactory;
return [
'controllers' => [
'factories' => [
Controller\IndexController::class => IndexControllerFactory::class,
],
],
'router' => [],
...
];
// module\Application\src\Factory\IndexControllerFactory.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
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();
}
}
php composer.phar