littlerocket/pdf-bundle

此捆绑包将Symfony与PHPPdf库集成。分支以修复与PHP7兼容的捆绑包

安装数: 2,026

依赖项: 0

建议者: 0

安全: 0

星标: 1

关注者: 0

分支: 45

类型:symfony-bundle

1.1.1 2023-04-12 08:57 UTC

This package is auto-updated.

Last update: 2024-09-12 12:00:43 UTC


README

此捆绑包将Symfony2与PHPPdf库集成。感谢此捆绑包,您可以轻松生成PDF或图像(png、jpg)文件。

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

安装

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

    composer require littlerocket/pdf-bundle

  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 - 缓存引擎的特定选项(例如File引擎的"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输出应缓存?True或False,默认:False。模板和样式表内容的哈希(md5)是缓存键,仅缓存PHPPdf调用,控制器始终被调用。