WhiteOctoberTCPDFBundle icon indicating copy to clipboard operation
WhiteOctoberTCPDFBundle copied to clipboard

How can i use a custom class that extends TCPDF

Open julien-its opened this issue 7 years ago • 6 comments

I would like to change the header and footer functions

julien-its avatar Sep 01 '17 14:09 julien-its

Hi @julien-its,

Could you take a look at the instructions at https://github.com/whiteoctober/WhiteOctoberTCPDFBundle#using-a-custom-class and see if they provide the information that you need?

If you're still stuck, add further comments to this issue and we'll help you further and also think whether the documentation needs updating.

Thanks Sam

sampart avatar Sep 04 '17 13:09 sampart

Oh, and if you don't need further help, let us know that too so that we can close this issue! Thanks

sampart avatar Sep 04 '17 13:09 sampart

Hello, Thank you for your answer. I actuly found the solution but not like explained in the Readme

Doing this :

white_october_tcpdf:
    class: 'Acme\MyBundle\MyTCPDFClass'

The class is never called

So i created a new class that extends TCPDF with Header function

class InvoiceTcpdf extends \TCPDF
{
public function Header(){ ... }
}

And instead of using

$this->pdf = $this->tcpdfService->create('vertical', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

I instantialize my custom class

$this->pdf = new \AppBundle\Services\Export\Pdf\InvoiceTcpdf(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

It works but maybe there is a beter solution

Regards Julien

julien-its avatar Sep 04 '17 14:09 julien-its

Hi Julien,

I've done a test of the custom-class functionality here and all seems to be working as expected. Could you update your config to look like this?

white_october_tcpdf:
    class: 'Namespace\InvoiceTcpdf'

Replace Namespace with the appropriate namespace.

And then use

$this->pdf = $this->tcpdfService->create('vertical', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

and let me know if that works?

If it doesn't, can you show me where you're getting $this->tcpdfService from so I can check that? Thanks

sampart avatar Sep 07 '17 15:09 sampart

Sorry it doesn't work

My service.yml :

white_october_tcpdf:
        class: AppBundle\Services\Exports\Pdf\InvoiceTcpdf

    AppBundle\Services\Exports\Pdf\InvoiceService:
        public: true
        class: AppBundle\Services\Exports\Pdf\InvoiceService
        arguments:
            $em: "@doctrine.orm.entity_manager"
            $tcpdf: '@white_october.tcpdf'

In InvoiceService :

$this->pdf = $this->tcpdfService->create('vertical', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

var_dump($this->pdf) return object(TCPDF)

When doing in InvoiceService :

$this->pdf = new \AppBundle\Services\Exports\Pdf\InvoiceTcpdf(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

var_dump($this->pdf) return object(AppBundle\Services\Exports\Pdf\InvoiceTcpdf)

$this->tcpdfService is a parameter given in the service.yml

` AppBundle\Services\Exports\Pdf\InvoiceService: public: true class: AppBundle\Services\Exports\Pdf\InvoiceService arguments: $em: "@doctrine.orm.entity_manager" $tcpdf: '@white_october.tcpdf'``

it's the $tcpdf param

julien-its avatar Sep 23 '17 22:09 julien-its

@julien-its I think the problem is these lines:

white_october_tcpdf:
        class: AppBundle\Services\Exports\Pdf\InvoiceTcpdf

These should go in config.yml, not services.yml as you don't need to define a new service, you need to configure an existing one.

Could you move those lines to config.yml and try again please?

sampart avatar Oct 19 '17 10:10 sampart