los/lospdf

ZF2 module to wrapper some pdf generation libraries

1.0.1 2016-04-23 17:02 UTC

This package is auto-updated.

Last update: 2024-10-26 03:32:52 UTC


README

Latest Stable Version Total Downloads Scrutinizer Code Quality SensioLabs Insight Dependency Status

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.

  1. Enter your project directory

  2. Create or edit your composer.json file with following contents:

    {
        "require": {
            "los/lospdf": "1.*",
            "mpdf/mpdf" : ">=v5.7.4",
        }
    }
  3. Run php composer.phar install

  4. Open my/project/directory/config/application.config.php and add LosPdf to your modules

```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

  1. Clone this module LosPdf to your vendor directory
  2. 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;
    }