los / lospdf
ZF2 module to wrapper some pdf generation libraries
Installs: 7 653
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 4
Forks: 4
Open Issues: 0
Requires
- php: >=5.4
- zendframework/zendframework: >=2.3.2,<3.0.0
Requires (Dev)
- phpunit/phpunit: ~4.4
- satooshi/php-coveralls: dev-master
Suggests
- los/loslog: los/loslog for logging
- los/losui: los/losui for twitter bootstrap 3 styling, jquery, chosen and other libraries
- mpdf/mpdf: mpdf/mpdf for html to pdf convertion
README
Introduction
This ZF2 module provides a wrapper to mPdf (more coming) to generate PDF documents from html.
Requirements
Instalation
Instalation can be done with composer ou manually
Installation with composer
For composer documentation, please refer to getcomposer.org.
-
Enter your project directory
-
Create or edit your
composer.json
file with following contents:{ "require": { "los/lospdf": "1.*", "mpdf/mpdf" : ">=v5.7.4", } }
-
Run
php composer.phar install
-
Open
my/project/directory/config/application.config.php
and addLosPdf
to yourmodules
```php
<?php
return array(
'modules' => array(
'LosPdf',
'Application'
),
'module_listener_options' => array(
'config_glob_paths' => array(
'config/autoload/{,*.}{global,local}.php',
),
'module_paths' => array(
'./module',
'./vendor',
),
),
);
```
Installation without composer
- Clone this module LosPdf to your vendor directory
- Enable it in your config/application.config.php like the step 4 in the previous section.
Usage
Render to browser
The following example demonstrates a typical usage of the LosPdf module inside an Action in a Controller:
public function pdfAction() { $generated = new \DateTime('now'); $genetared = $gerado->format('d/m/Y H:i:s'); $pdf = new PdfModel(); $renderer = $this->getServiceLocator()->get('ViewPdfRenderer'); $renderer->getEngine()->setHTMLHeader('<table width="100%" style="vertical-align: bottom; font-family: serif; font-size: 8pt; color: #000000; font-weight: bold; font-style: italic; border-bottom: 1px solid #000"><tr> <td width="33%"><span style="font-weight: bold; font-style: italic;">Client</span></td> <td width="33%" align="center" style="font-weight: bold; font-style: italic;">Report Name</td> <td width="33%" style="text-align: right; ">My Company</td> </tr></table> '); $renderer->getEngine()->setHTMLFooter('Footer', '<table width="100%" style="vertical-align: bottom; font-family: serif; font-size: 8pt; color: #000000; font-weight: bold; font-style: italic; border-top: 1px solid #000"><tr> <td width="50%" style="text-align: left; font-weight: bold; font-style: italic;">Generated: '.$generated.'</td> <td width="50%" style="text-align: right; ">Page {PAGENO}</td> </tr></table> '); $pdf->setTerminal(true); $pdf->setVariables(['name'=>'Leandro']); $pdf->setOption("paperSize", "a4"); return $pdf; }
And use the view file as usual.
You can set any mPdf option through $renderer->getEngine():
$renderer = $this->getServiceLocator()->get('ViewPdfRenderer'); $renderer->getEngine()->pagenumPrefix = 'Page n ';
Render to string
You can capture the pdf output to a string:
public function pdfAction() { $generated = new \DateTime('now'); $genetared = $gerado->format('d/m/Y H:i:s'); $pdf = new PdfModel(); $renderer = $this->getServiceLocator()->get('ViewPdfRenderer'); $renderer->getEngine()->setHTMLHeader('<table width="100%" style="vertical-align: bottom; font-family: serif; font-size: 8pt; color: #000000; font-weight: bold; font-style: italic; border-bottom: 1px solid #000"><tr> <td width="33%"><span style="font-weight: bold; font-style: italic;">Client</span></td> <td width="33%" align="center" style="font-weight: bold; font-style: italic;">Report Name</td> <td width="33%" style="text-align: right; ">My Company</td> </tr></table> '); $renderer->getEngine()->setHTMLFooter('Footer', '<table width="100%" style="vertical-align: bottom; font-family: serif; font-size: 8pt; color: #000000; font-weight: bold; font-style: italic; border-top: 1px solid #000"><tr> <td width="50%" style="text-align: left; font-weight: bold; font-style: italic;">Generated: '.$generated.'</td> <td width="50%" style="text-align: right; ">Page {PAGENO}</td> </tr></table> '); $pdf->setTerminal(true); $pdf->setVariables(['name'=>'Leandro']); $pdf->setOption("paperSize", "a4"); $pdf->setTemplate('site/index/pdf'); $output = $renderer->renderToString($pdf); //Do something with output }
Render to file
You can save the pdf to a file:
public function pdfAction() { $generated = new \DateTime('now'); $genetared = $gerado->format('d/m/Y H:i:s'); $pdf = new PdfModel(); $renderer = $this->getServiceLocator()->get('ViewPdfRenderer'); $renderer->getEngine()->setHTMLHeader('<table width="100%" style="vertical-align: bottom; font-family: serif; font-size: 8pt; color: #000000; font-weight: bold; font-style: italic; border-bottom: 1px solid #000"><tr> <td width="33%"><span style="font-weight: bold; font-style: italic;">Client</span></td> <td width="33%" align="center" style="font-weight: bold; font-style: italic;">Report Name</td> <td width="33%" style="text-align: right; ">My Company</td> </tr></table> '); $renderer->getEngine()->setHTMLFooter('Footer', '<table width="100%" style="vertical-align: bottom; font-family: serif; font-size: 8pt; color: #000000; font-weight: bold; font-style: italic; border-top: 1px solid #000"><tr> <td width="50%" style="text-align: left; font-weight: bold; font-style: italic;">Generated: '.$generated.'</td> <td width="50%" style="text-align: right; ">Page {PAGENO}</td> </tr></table> '); $pdf->setTerminal(true); $pdf->setVariables(['name'=>'Leandro']); $pdf->setOption("paperSize", "a4"); $pdf->setTemplate('site/index/pdf'); $renderer->renderToFile($pdf, '/tmp/report.pdf'); }
Mixing outputs
You can use more than one render type. the following example will save the pdf to a file, render the pdf to a string and to the browser:
public function pdfAction() { $generated = new \DateTime('now'); $genetared = $gerado->format('d/m/Y H:i:s'); $pdf = new PdfModel(); $renderer = $this->getServiceLocator()->get('ViewPdfRenderer'); $renderer->getEngine()->setHTMLHeader('<table width="100%" style="vertical-align: bottom; font-family: serif; font-size: 8pt; color: #000000; font-weight: bold; font-style: italic; border-bottom: 1px solid #000"><tr> <td width="33%"><span style="font-weight: bold; font-style: italic;">Client</span></td> <td width="33%" align="center" style="font-weight: bold; font-style: italic;">Report Name</td> <td width="33%" style="text-align: right; ">My Company</td> </tr></table> '); $renderer->getEngine()->setHTMLFooter('Footer', '<table width="100%" style="vertical-align: bottom; font-family: serif; font-size: 8pt; color: #000000; font-weight: bold; font-style: italic; border-top: 1px solid #000"><tr> <td width="50%" style="text-align: left; font-weight: bold; font-style: italic;">Generated: '.$generated.'</td> <td width="50%" style="text-align: right; ">Page {PAGENO}</td> </tr></table> '); $pdf->setTerminal(true); $pdf->setVariables(['name'=>'Leandro']); $pdf->setOption("paperSize", "a4"); $pdf->setTemplate('site/index/pdf'); $output = $renderer->renderToString($pdf); $renderer->renderToFile($pdf, '/tmp/report.pdf'); return $pdf; }