takeoo/zend-pdf

There is no license information available for the latest version (v0.2.1) of this package.

Create pdf from view in zend framework 2

v0.2.1 2017-05-04 11:26 UTC

This package is auto-updated.

Last update: 2024-04-29 03:50:33 UTC


README

Create pdf from view in ZendFramework 3

Works on zend framework v3.*

IF you want to use it with older versions, use one of previous tags

Requirements

  • wkhtmltopdf installed on your server

    • Debian, Ubuntu:
    sudo apt-get install wkhtmltopdf
    • *Debian servers have known errors with wkhtmltopdf lib, so if you get error like:
    wkhtmltopdf: cannot connect to X server

    install xvfb:

    sudo apt-get install xvfb
  • zend-view 2.3* is installed as dependency

  • zend-service-manager is already installed with 3.* version of zf3.

Install

  • Composer install

     composer require takeoo/zend-pdf
  • Add "Takeoo\Pdf" to 'modules' array in application.config.php

Usage

  • Takeoo\Zend-pdf will create service throughout service manager, so it is available in serviceLocator as 'PdfCreator';

  • Instantiate:

$pdfCreator = $serviceManager->get('PdfCreator');

NOTE: PdfCreator is not shared service, so every call to serviceManager will create new instance.

PdfCreator uses ViewResolver which is auto-magically injected to PhpRenderer if have MVC application, so you can use any view or layout you have in your app!

  • Set layout

     $pdfCreator->setLayoutTemplate('layout/pdf-template');
    • Add view as you would in your response, just pass name and variables to createHtml() function
    $pdfCreator->createHtml('path/to/view', ['variableName' => $variableValue]);

    OR you can directly pass already created view model

    $pdfCreator->createHtmlFromViewModel($viewModel);
  • Output

    • All modern browser support pdf output directly to browser:

      $pdfCreator->output();
    • get file handle

      $pdfCreator->writePdf();

    NOTE: Both above functions will save file to your disk

  • Get file path

    $pdfCreator->getFilePath();
  • By default all files are generated to your project root into "{Ymd_His}.pdf"

  • Change file destination

    $pdfCreator->setPdfFileName('path/to/desired/folder/nameofyourfile.pdf');

NOTE: All functions with public scope (with exception of output() and writePdf()) are fluent, so you can chain all functions:

$pdfCreator->setLayoutTemplate('layout/pdf-layout')
  ->createHtml('view', ['variable1' => $variable1Value])
  ->setPdfFileName('./../file.pdf')
  ->setHasXvfb(false)
  ->output();

NOTE: By default all wkhtmltopdf conversions are created with setHasXvfb(true), so you have to have installed Xvfb on your server! If you want to turn it of just do:

$pdfCreator->setHasXvfb(false);