Ladislav Maxa Ladislav Maxa - 1 year ago 85
PHP Question

Creating class that extends TCPDF with WhiteOctoberBundle

For creating PDF files in Symfony I'm using TCPDF class with WhiteOctoberTCPDFBundle. Both installed via composer.

I need to create custom header and footer by extending TCPDF class, like mentioned in TCPDF documentation.

But the only working solution for me is to extend TCPDF class directly in vendors and then set MYPDF class as default in WhiteOctoberBundle inside getConfigTreeBuilder() method in vendor/whiteoctober/tcpdf-bundle/DependencyInjection/Configuration.php.

So my Configuration.php file looks like this:

public function getConfigTreeBuilder()
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('white_october_tcpdf');



return $treeBuilder;

The problem is that extending TCPDF class directly in vendor is bad practice and it causes that MYPDF class disappears during every update of TCPDF Bundle. Where should I place MYPDF class so it would stay accessible for WhiteOctoberBundle and at the same time it won't be placed inside vendors folder?

Answer Source

Bundle author here :-) This is in the bundle documentation, but for a quick reference:

  1. Create your MYPDF class and place it somewhere within your normal Symfony2 code base - let's say in src/Acme/DemoBundle/MYPDF.php

    This class needs to extend the base TCPDF class, which is brought in via the vendors. You'll get an exception thrown if it's not.

  2. In your config.yml or similar:

        class: 'Acme\DemoBundle\MYPDF'
  3. You should be then able to use this as per the docs:

    $pdfObj = $container->get("white_october.tcpdf")->create();

    which will give you a new instance of your MYPDF class.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download