-->

Change WhiteOctober TCPDF bundle configuration val

2019-05-07 17:01发布

问题:

I've started using the WhiteOctober TCPDF bundle in my symfony project but I can't figure out how to change the default configuration values, such as page format. I've tried putting that in my 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

but they are totally ignored when calling

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

in my controller. The generated PDF is still in A4 with no header and defaults margins.

Am I missing something to make the bundle take my custom configuration?

回答1:

I have the same problem. Alter vendor code is not best practice. My workaround is below:

I created a new class "CustomWhiteOctoberTCPDFBundle" in my src folder.

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:

You need to edit the tcpdf_config.php in the vendor/tecnick.com/tcpdf/config folder.



回答3:

Add this code ...

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

to the bundle file

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

The original file https://github.com/wrep/TCPDFBundle/blob/master/WrepTCPDFBundle.php currently matches only the configuration parameters that start with ^k The configuration options for this bundle are shown here

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

I had to add a few more configuration parameters from tcpdf_config.php that were not listed in the documentation config example above. Here is my complete white_october_tcpdf: section in 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:

Well, just now I resolve the same problem:

You need to define your needed constants like mirk said,

white_october_tcpdf:
tcpdf:
  .
  .
  .

then you need to change the class: /vendor/whiteoctober/tcpdf-bundle/WhiteOctober/TCPDFBundle/WhiteOctoberTCPDFBundle.php with this code

                // 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);
                }
            }

to match both constants types.



回答5:

This has been solved in version 1.1.0 of WhiteOctoberTCPDFBundle (released 21 June 2018):

Fix issue 'custom config parameters not loaded' (PR #45)

So updating your version of the bundle should now resolve this - you can add the additional configuration without also needing to make code changes.

Previously, only configuration values beginning with k_ were read, now all are.