【问题标题】:Change WhiteOctober TCPDF bundle configuration values更改 WhiteOctober TCPDF 捆绑配置值
【发布时间】:2012-11-05 16:02:46
【问题描述】:

我已经开始在我的 symfony 项目中使用 WhiteOctober TCPDF 包,但我不知道如何更改默认配置值,例如页面格式。我试过把它放在我的 config.yml 中:

white_october_tcpdf:
    tcpdf:
        k_tcpdf_external_config: true
        pdf_page_format: 'LETTER'
        pdf_author: 'Company Name'
        pdf_header_title: 'Order Confirmation'
        pdf_header_string: 'Company Name'
        pdf_header_logo: '%kernel.root_dir%/../web/images/logo.png'
        pdf_header_logo_width: '35'
        pdf_margin_header: 15
        pdf_margin_footer: 15
        pdf_margin_top: 25
        pdf_margin_bottom: 25
        pdf_margin_left: 25
        pdf_margin_right: 25

但在调用时它们会被完全忽略

$pdf = $this->get('white_october.tcpdf')->create();

在我的控制器中。生成的 PDF 仍然是 A4 格式,没有页眉和默认边距。

我是否缺少使捆绑包采用我的自定义配置的东西?

【问题讨论】:

  • 你发现问题了吗?我对 symfony 2.3 有同样的问题
  • 那种。我从 (wk2pdf) 开始切换到另一个解决方案,那是很久以前的事了,所以我不记得确切的代码,但这里是一般的想法:我创建了一个自定义服务来生成带有 white_october.tcpdf 的“基础”PDF 和设置适当的配置,然后在我的控制器中使用此服务而不是white_october.tcpdf。这更像是一种解决方法,而不是真正的解决方案,但效果很好。
  • 项目现在有一个关于这个的问题:github.com/whiteoctober/WhiteOctoberTCPDFBundle/issues/25

标签: symfony tcpdf symfony-2.1


【解决方案1】:

我也有同样的问题。更改供应商代码不是最佳做法。我的解决方法如下:

我在我的 src 文件夹中创建了一个新类“CustomWhiteOctoberTCPDFBundle”。

namespace TCPDFBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;

class CustomWhiteOctoberTCPDFBundle extends Bundle
{
 public function getParent()
    {
        return 'WhiteOctoberTCPDFBundle';
    }  
    public function boot() {
         //Put modified method as mentioned in the previous answer

    }
}

On AppKernel.php put:
    public function registerBundles()
    {
        $bundles = array(...
            new WhiteOctober\TCPDFBundle\WhiteOctoberTCPDFBundle(),
            new TCPDFBundle\CustomWhiteOctoberTCPDFBundle(),

【讨论】:

    【解决方案2】:

    您需要编辑 vendor/tecnick.com/tcpdf/config 文件夹中的 tcpdf_config.php。

    【讨论】:

      【解决方案3】:

      添加此代码...

                  if (preg_match("/^pdf_/i", $k)) {
                      if (!defined($constKey)) {
                          define($constKey, $v);
                      }
                  }
      

      到捆绑文件

       /vendor/whiteoctober/tcpdf-bundle/WhiteOctober/TCPDFBundle/WhiteOctoberTCPDFBundle.php
      

      原文件https://github.com/wrep/TCPDFBundle/blob/master/WrepTCPDFBundle.php 目前只匹配以^k开头的配置参数 此捆绑包的配置选项显示在此处

      https://github.com/wrep/TCPDFBundle/blob/master/WrepTCPDFBundle.php

      我不得不从 tcpdf_config.php 添加一些配置参数,这些参数没有在上面的文档配置示例中列出。 这是我完整的 white_october_tcpdf:app/config/config.yml 中的部分

      ...ober_tcpdf:
      file:                 %kernel.root_dir%/../vendor/tecnick.com/tcpdf/tcpdf.php
      class:                TCPDF
      tcpdf:
          k_path_url:           %kernel.root_dir%/../vendor/tecnick.com/tcpdf/
          k_path_main:          %kernel.root_dir%/../vendor/tecnick.com/tcpdf/
          k_path_fonts:         %kernel.root_dir%/../vendor/tecnick.com/tcpdf/fonts/
          k_path_cache:         %kernel.cache_dir%/tcpdf
          k_path_url_cache:     %kernel.cache_dir%/tcpdf
          k_path_images:        %kernel.root_dir%/../vendor/tecnick.com/tcpdf/examples/images/
          k_blank_image:        %kernel.root_dir%/../vendor/tecnick.com/tcpdf/examples/images/_blank.png
          k_cell_height_ratio:  1.25
          k_title_magnification:  1.3
          k_small_ratio:        0.66666666666667
          k_thai_topchars:      true
          k_tcpdf_calls_in_html:  true
          k_tcpdf_external_config:  true
          head_magnification:   10
          pdf_page_format:      LETTER
          pdf_page_orientation:  P
          pdf_creator:          TCPDF
          pdf_author:           TCPDF
          pdf_header_title:  Example Title
          pdf_header_string: "this is motto - My Company\nwww.mycompany.org" 
          pdf_header_logo: ../../../../../web/bundles/home/images/peas.jpg    
          pdf_header_logo_width: 30
          pdf_unit:             mm
          pdf_margin_header:    5
          pdf_margin_footer:    10
          pdf_margin_top:       27
          pdf_margin_bottom:    25
          pdf_margin_left:      15
          pdf_margin_right:     15
          pdf_font_name_main:   helvetica
          pdf_font_size_main:   10
          pdf_font_name_data:   helvetica
          pdf_font_size_data:   8
          pdf_font_monospaced:  courier
          pdf_image_scale_ratio:  1.25
      

      【讨论】:

        【解决方案4】:

        嗯,刚才我解决了同样的问题:

        你需要像 mirk 说的那样定义你需要的常量,

        white_october_tcpdf:
        tcpdf:
          .
          .
          .
        

        然后您需要使用此代码更改类:/vendor/whiteoctober/tcpdf-bundle/WhiteOctober/TCPDFBundle/WhiteOctoberTCPDFBundle.php

                        // All K_ and _pdf constants are required
                    if (preg_match("/^k_/i", $k) OR preg_match("/^pdf_/i", $k)) {
                        if (!defined($constKey)) {
                            $value = $this->container->getParameterBag()->resolveValue($v);
                            if (($k === 'k_path_cache' || $k === 'k_path_url_cache') && !is_dir($value)) {
                                $this->createDir($value);
                            }
        
                            define($constKey, $value);
                        }
                    }
        

        匹配两种常量类型。

        【讨论】:

          【解决方案5】:

          此问题已在 WhiteOctoberTCPDBundle(2018 年 6 月 21 日发布)的 version 1.1.0 中解决:

          修复问题“未加载自定义配置参数”(PR #45

          因此,现在更新您的捆绑包版本应该可以解决这个问题 - 您可以添加额外的配置而无需更改代码。

          以前只读取以k_ 开头的配置值,现在全部读取。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2013-02-17
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-03-16
            • 1970-01-01
            • 2013-04-15
            相关资源
            最近更新 更多