los/lospdf
Composer 安装命令:
composer require los/lospdf
包简介
ZF2 module to wrapper some pdf generation libraries
README 文档
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.jsonfile with following contents:{ "require": { "los/lospdf": "1.*", "mpdf/mpdf" : ">=v5.7.4", } } -
Run
php composer.phar install -
Open
my/project/directory/config/application.config.phpand addLosPdfto 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; }
los/lospdf 适用场景与选型建议
los/lospdf 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.7k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2015 年 02 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「zf2」 「pdf」 「mpdf」 「los」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 los/lospdf 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 los/lospdf 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 los/lospdf 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
LosLog provides some log utility
EdpModuleLayouts is very simple Laminas module for making module-specific layouts insanely easy.
Zend Framework module to leverage the symfony dependency injection container
RCM User HTML views/pages
Laravel MPdf : Easily generate PDF files with arabic support
Builds a Bridge between Zend Expressive and Plugin Managers of Zend\MVC
统计信息
- 总下载量: 7.7k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 15
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2015-02-19