2012-11-05 28 views
4

我已经开始在我的symfony项目中使用WhiteOctober TCPDF软件包,但我无法弄清楚如何更改默认配置值,例如页面格式。我试图把在我config.yml:更改WhiteOctober TCPDF软件包配置值

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,没有标题和默认边距。

我是否错过了一些可以让我的自定义配置进行绑定的东西?

+0

你发现问题了吗?我遇到与symfony 2.3 – mirk

+0

类似的问题。自从(wk2pdf)开始,我转向另一种解决方案,并且很久以前,所以我不记得确切的代码,但这里是一般想法:我创建了一个自定义服务来生成“base”PDF,其中包含'white_october.tcpdf '并设置适当的配置,然后在我的控制器中使用此服务而不是'white_october.tcpdf'。这不是一个真正的解决方案,而是更好的解决方法,但它运行良好。 – Johann

回答

0

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

+0

如何编辑它? –

0

添加该代码...

  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 
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(), 
0

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

你需要一个像MIRK说来定义你所需要的常量,

white_october_tcpdf: 
tcpdf: 
    . 
    . 
    . 

,那么你需要更改类:/供应商/ whiteoctober/TCPDF束/ 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); 
       } 
      } 

匹配两个常量类型。