sampart / WhiteOctoberTCPDFBundle

A bundle to facilitate using TCPDF for PDF generation in Symfony applications
89 stars 43 forks source link

How can i use a custom class that extends TCPDF #41

Open julien-its opened 7 years ago

julien-its commented 7 years ago

I would like to change the header and footer functions

sampart commented 7 years ago

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 commented 7 years ago

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

julien-its commented 7 years ago

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

sampart commented 7 years ago

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

julien-its commented 7 years ago

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

sampart commented 7 years ago

@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?