定制 los/lospdf 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

los/lospdf

最新稳定版本:1.0.1

Composer 安装命令:

composer require los/lospdf

包简介

ZF2 module to wrapper some pdf generation libraries

README 文档

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;
    }

统计信息

  • 总下载量: 7.7k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 2
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2015-02-19

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固