2012-11-05 42 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); 
       } 
      } 

匹配兩個常量類型。