heimrichhannot/contao-pdf-creator-bundle

通用的PDF创建包

0.4.9 2024-07-23 08:00 UTC

This package is auto-updated.

Last update: 2024-09-23 08:24:12 UTC


README

此包添加了一种通用的配置PDF文件创建的方法,重用这些配置,并基于它们创建PDF。它基于PDF Creator库

功能

  • 从后端模块或yaml添加PDF配置
  • 可以轻松地用于您的包
  • 包含以下支持

注意:此包不包含PDF库,您需要自己添加您想使用的库!有关更多信息,请参阅使用部分。

截图配置

使用方法

设置

  1. 安装您想使用的PDF库(目前支持dompdf、mpdf和tcpdf,有关更多信息,请参阅PDF Creator库

  2. 使用composer或contao manager安装包

     composer require heimrichhannot/contao-pdf-creator-bundle
    
  3. 更新数据库

  4. 在contao后端系统中创建PDF配置 -> PDF配置或通过yaml(请参阅配置参考)

Yaml pdf配置

要重用配置或在不同环境中自定义它们,您可以在配置文件中设置PDF创建器配置。您可以在配置参考中找到所有可能选项。

示例

# /config/config.yml
huh_pdf_creator:
  configurations:
    news_export:
      type: dompdf
      name: "Default News export configuration"
      filename: '%%title%%-my_brand_corporate.pdf'
      output_mode: inline
    brand_brochure:
      type: dompdf
      name: "Brand brochure"
      filename: 'my_brand_corporate.pdf'
      output_mode: download
      format: A5
      base_template: 'files/media/brand_cd/brand_brochure_template.pdf'

导出文章为PDF

  1. huh_pdf_creator.enable_contao_article_pdf_syndication设置为true

    # config/config.yml or app/config/config.yml (Contao 4.4)
    huh_pdf_creator:
        enable_contao_article_pdf_syndication: true
  2. 清除缓存并更新数据库

  3. 在文章配置中选择PDF作为聚合选项,并选择一个PDF配置

聚合包

选择PDF聚合,并选择您想用于导出的PDF创建器设置。

高级主题

日志记录

在创建PDF时获取增强的调试信息,您可以将contao置于开发模式。在开发模式下,PDF创建器包将PDF库创建的所有日志(如果库支持PSR-3日志)保存到日志文件夹中的huh_pdf_creator-[DATE].log文件。对于dompdf,此包支持自定义日志实现,并将日志存储到日志文件夹中的huh_pdf_creator-dompdf.log文件。

服务器路由问题/身份验证

为了避免服务器路由(例如,使用docker等的特殊URL处理)的问题,并在受访问限制的网站上(例如,测试设置)使用库,您可以设置一个base_url,它将覆盖由请求和凭据(仅支持HTTP基本身份验证)确定的URL。这些选项仅在yaml配置中可用,因为它们通常是针对特定服务器的。

huh_pdf_creator:
  configurations:
    custom_pdf_config:
      type: dompdf
      name: "PDF behind authentication"
      base_url: 'https://customer.example.org'
      credentials: 'user:password'

DomPdf chroot设置

Pdf Creator Bundle包含针对dompdf chroot选项的默认设置,这些选项用于存储在PDF中使用的典型文件夹,您可以在包配置中调整这些设置。

这些是默认设置

huh_pdf_creator:
    allowed_paths:
        - web
        - public
        - files
        - assets

开发者

将PDF创建器添加到您的包

  1. 使用PdfGenerator::generate()使用您的内容生成PDF。它期望一个PDF创建器配置的ID、HTML内容和PdfContext实例,并返回一个PdfCreatorResult实例。

    use Heimrichhannot\PdfCreatorBundle\Generator\PdfGenerator;
    use Heimrichhannot\PdfCreatorBundle\Generator\PdfGeneratorContext;
    
    class ExportCustomEntity {
        /**@var PdfGenerator */
        protected $pdfGenerator;
        
        public function __invoke(string $content, array $row): void {
            $context = new PdfGeneratorContext($row['title']);
            $result = $this->pdfGenerator->generate($content, $row['pdfConfiguration'], $context);
            
        }
    }
  2. 使用DcaGenerator将PDF创建器配置字段添加到您的dca。

    use Contao\CoreBundle\DataContainer\PaletteManipulator;
    use Heimrichhannot\PdfCreatorBundle\Generator\DcaGenerator;
    
    class LoadDataContainerListener {
        /** @var DcaGenerator */
        protected $dcaGenerator;
        
        public function __invoke(string $table): void
        {
            if ('tl_custom_dca' === $table) {
                PaletteManipulator::create()->addField('pdfConfiguration', 'someField')->applyToPalette('default', 'tl_custom_entity');
                $GLOBALS['TL_DCA']['tl_custom_entity']['fields']['pdfConfiguration'] = $this->dcaGenerator->getPdfCreatorConfigSelectFieldConfig();
            }
        }
    }

事件

更多信息

配置参考