takeoo / zend-pdf
Create pdf from view in zend framework 2
Installs: 3 787
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- zendframework/zend-servicemanager: ^3.0.3
- zendframework/zend-view: ^2.6.3
This package is auto-updated.
Last update: 2024-11-29 05:26:36 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);