psliwa / pdf-bundle
此包将 Symfony2 与 PHPPdf 库集成。
1.0.3
2015-12-06 14:17 UTC
Requires
- php: >=5.3.0
- psliwa/php-pdf: ^1.1.5
- sensio/framework-extra-bundle: >=2.0 <4.0.0
- symfony/symfony: ~2.3|~3.0
Requires (Dev)
- phpunit/phpunit: >=4,<6.0.0
This package is auto-updated.
Last update: 2024-09-11 04:15:22 UTC
README
此包将 Symfony2 与 PHPPdf 库集成。感谢此包,您可以轻松生成 PDF 或图像(png、jpg)文件。
您可以在 GitHub 上找到 PHPPdf 的文档(README 文件)。
安装
-
使用 composer。PsPdfBundle 需要 "minimum-stability" 等于 dev。运行此命令
composer require psliwa/pdf-bundle
-
在 AppKernel 中注册包
//app/AppKernel.php public function registerBundles() { return array( // .. new Ps\PdfBundle\PsPdfBundle(), // .. ); }
配置
所有选项都是可选的。
# app/config/config.yml
ps_pdf:
nodes_file: ~
fonts_file: ~
complex_attributes_file: ~
colors_file: ~
use_cache_in_stylesheet: ~
cache:
type: ~
options: ~
markdown_stylesheet_filepath: ~
markdown_document_template_filepath: ~
document_parser_type: ~
- nodes_file - 节点/标签定义文件路径,默认使用 PHPPdf 库内部的 nodes.xml 文件
- fonts_file - 字体定义文件路径,默认使用 PHPPdf 库内部的 fonts.xml 文件
- complex_attributes_file - 复杂属性定义文件路径,默认使用 PHPPdf 库内部的 complex-attributes.xml 文件
- colors_file - 默认调色板颜色文件路径,默认使用 PHPPdf 库内部的 colors.xml 文件
- cache.type - 缓存类型,支持 Zend_Cache 组件的所有后端缓存(例如 File、Apc、Memcached、Sqlite 等)。默认使用文件引擎。
- cache.options - 缓存引擎的特定选项(例如 "cache_dir" 用于文件引擎)。默认的 cache_dir 与 kernel.cache_dir 相同。
- use_cache_in_stylesheet - 如果设置此选项,则将样式表匹配规则缓存。在复杂的样式表中,缓存可以显著提高性能。默认为 true,但在开发环境中缓存应关闭。
- markdown_stylesheet_filepath - markdown 解析器样式表文件路径
- markdown_document_template_filepath - markdown 解析器输出 xml 文档模板文件路径
- document_parser_type - 默认解析器类型:xml 或 markdown
源文档中的图像
如果您想显示图像,您必须通过图像标签的 "src" 属性提供图像文件的绝对路径。Asset Twig 函数不起作用,因为它会将图像路径转换为相对于 web 目录的相对路径。为了更容易使用图像,此包提供了将图像逻辑名称转换为真实、绝对路径的 Twig 函数。
示例
<pdf>
<dynamic-page>
<!-- pdf_image('BundleName:image-name.extension') -->
<img src="{{ pdf_image('SymfonyWebConfiguratorBundle:blue-arrow.png') }}" />
</dynamic-page>
</pdf>
字体和文档 xml 文件中的包基础路径
如果您想使用自定义字体,应创建自己的 fonts.xml 配置文件(默认字体文件路径为 PHPPdf\Resources\config\fonts.xml)。为了更容易定义字体路径,此包支持基于包的路径。示例
<!-- some fonts.xml file -->
<italic src="%SomeBundle:file.ttf%" />
"%SomeBundle:file.ttf%" 将被替换为 "path/to/SomeBundle/Resources/file.ttf"
示例
// In controller
//...
use Ps\PdfBundle\Annotation\Pdf;
//...
/**
* @Pdf()
*/
public function helloAction($name)
{
$format = $this->get('request')->get('_format');
return $this->render(sprintf('SomeBundle:SomeController:helloAction.%s.twig', $format), array(
'name' => $name,
));
}
// in helloAction.html.twig
Hello {{ name }}!
// in helloAction.pdf.twig
<pdf>
<dynamic-page>
Hello {{ name }}!
</dynamic-page>
</pdf>
包自动检测 pdf 格式(通过 _format 请求)并从响应中创建 pdf 文档。
Pdf 注释有四个可选属性
- headers - 特定头部的关联数组
- stylesheet - pdf 样式表模板文件,使用标准的 Symfony2 语法("Bundle:Controller:file.format.engine")
- documentParserType - 解析器类型:xml 或 markdown
- enableCache - pdf 输出应该缓存吗?布尔值,默认:false。模板和样式表内容的哈希(md5)是缓存键,仅缓存 PHPPdf 调用,控制器始终调用。