This ZF2 module provides a wrapper to mPdf (more coming) to generate PDF documents from html.
Instalation can be done with composer ou manually
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',
),
),
);
```
- Clone this module LosPdf to your vendor directory
- Enable it in your config/application.config.php like the step 4 in the previous section.
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 ';
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
}
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');
}
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;
}