fbeen/pdf-bundle

此扩展包与 psliwa/pdf-bundle 相同,但与 Symfony 3.3 兼容。

维护者

详细信息

github.com/Fbeen/PdfBundle

源代码

安装: 4,551

依赖项: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 45

类型:symfony-bundle

1.0.3 2015-12-06 14:17 UTC

This package is not auto-updated.

Last update: 2024-09-24 04:45:33 UTC


README

Build Status

此扩展包将 Symfony2 与 PHPPdf 库集成。得益于此扩展包,您可以轻松生成 PDF 或图像(png、jpg)文件。

您可以在 github(README 文件)上找到 PHPPdf 的文档。

安装

  1. 使用 composer。PsPdfBundle 需要 "minimum-stability" 等于 dev。运行以下命令:

    composer require fbeen/pdf-bundle dev-master

  2. 在 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 等)。默认使用 File 引擎。
  • cache.options - 缓存引擎的特定选项(例如 "cache_dir" 对于 File 引擎)。默认的 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 - 标准 Symfony2 表示法中的 pdf 样式表模板文件 ("Bundle:Controller:file.format.engine")
  • documentParserType - 解析器类型:xml 或 markdown
  • enableCache - pdf 输出应该被缓存吗?是或否,默认:否。模板和样式表内容的哈希(md5)是缓存键,仅缓存 PHPPdf 调用,控制器始终被调用。