2017 © Pedro Peláez
 

library zend-pdf

Create pdf from view in zend framework 2

image

takeoo/zend-pdf

Create pdf from view in zend framework 2

  • Tuesday, July 4, 2017
  • by TakeoO
  • Repository
  • 1 Watchers
  • 0 Stars
  • 1,434 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 19 % Grown

The README.md

HTML to PDF for zend framework

Create pdf from view in ZendFramework 3, (*1)

Works on zend framework v3.*, (*2)

IF you want to use it with older versions, use one of previous tags, (*3)

Requirements

  • wkhtmltopdf installed on your server, (*4)

    • Debian, Ubuntu:, (*5)

      sudo apt-get install wkhtmltopdf
      
    • *Debian servers have known errors with wkhtmltopdf lib, so if you get error like:, (*6)

      wkhtmltopdf: cannot connect to X server
      

      install xvfb:, (*7)

      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 bash composer require takeoo/zend-pdf, (*8)

  • Add "Takeoo\Pdf" to 'modules' array in application.config.php, (*9)

Usage

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

  • Instantiate:, (*11)

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

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

    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!, (*13)

    • Set layout, (*14)

      $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, (*15)

      $pdfCreator->createHtmlFromViewModel($viewModel);
      
    • Output, (*16)

      • All modern browser support pdf output directly to browser:, (*17)

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

        $pdfCreator->writePdf();
        

      NOTE: Both above functions will save file to your disk, (*19)

    • Get file path, (*20)

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

    • Change file destination, (*22)

      $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:, (*23)

    $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:, (*24)

$pdfCreator->setHasXvfb(false);

The Versions

04/07 2017

dev-master

9999999-dev

Create pdf from view in zend framework 2

  Sources   Download

The Requires

 

by Jan Mrhar

04/05 2017

v0.2.1

0.2.1.0

Create pdf from view in zend framework 2

  Sources   Download

The Requires

 

by Jan Mrhar

03/05 2017

v0.2.0

0.2.0.0

Create pdf from view in zend framework 2

  Sources   Download

The Requires

 

by Jan Mrhar

18/03 2017

v0.1.1

0.1.1.0

Create pdf from view in zend framework 2

  Sources   Download

The Requires

 

by Jan Mrhar

18/03 2017

v0.1.0

0.1.0.0

Create pdf from view in zend framework 2

  Sources   Download

The Requires

 

by Jan Mrhar