orkestra / OrkestraPdfBundle

Provides a uniform API for generating PDFs using wkhtmltopdf, tcpdf and zend pdf
MIT License
2 stars 2 forks source link

Header and footer with TCPDF #3

Closed davidromani closed 9 years ago

davidromani commented 11 years ago

Hello again,

How can I make my custom header and footer like this example http://www.tcpdf.org/examples/example_003.phps with TCPDF library? I don't know how to override Header() and Footer() methods.

Kind regards,

tyler-sommer commented 11 years ago

Ah, this is great! I've never used TCPDF in this way. I wasn't aware of implementations which would extend the TCPDF object itself, so this will be a great feature to make easily available. However, it is achievable with the current implementation-- its just a bit more complicated.

In the bundle's current state, you would need to implement a custom TcPdfFactory subclass:

class MyCustomTcPdfFactory extends \Orkestra\Bundle\PdfBundle\Factory\TcPdfFactory
{
    public function __construct()
    {
        parent::__construct();

        $this->reflClass = new ReflectionClass('My\Bundle\ApplicationBundle\Pdf\CustomTcPdf');
    }
}

This new factory knows how to instantiate the right subclass TCPDF. Take a look at Orkestra\Bundle\PdfBundle\Factory\TcPdfFactory for more details on how you can customize the process.

Then, you would override the included bundle factory by specifying in your parameters.yml:

// parameters.yml
parameters:
  orkestra.pdf.tcpdf_factory.class: My\Bundle\ApplicationBundle\Factory\MyCustomTcPdfFactory

All that said, I will (or, if you want to, you're welcome to!) implement a better way to handle subclasses of TCPDF so that we can better support your use case.

tyler-sommer commented 11 years ago

Hi David,

I've added better support for your use case. Check here: https://github.com/orkestra/OrkestraPdfBundle/compare/41ccd0b%5E...develop

Let me know what you think! Thanks again

davidromani commented 11 years ago

Don't works :(

I made this:

// src/Flux/CustomerBundle/PdfGenerator/MyCustomTcpdf.php <?php namespace Flux\CustomerBundle\PdfGenerator; class MyCustomTcpdf extends \TCPDF { //Page header public function Header() { // Set font $this->SetFont('helvetica', 'B', 20); // Title $this->Cell(0, 15, '<< TCPDF Example 003 >>', 0, false, 'C', 0, '', 0, false, 'M', 'M'); } // Page footer public function Footer() { // Position at 15 mm from bottom $this->SetY(-15); // Set font $this->SetFont('helvetica', 'I', 8); // Page number $this->Cell(0, 10, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M'); } }

// src/Flux/CustomerBundle/PdfGenerator/InvoiceGenerator.php <?php

namespace Flux\CustomerBundle\PdfGenerator;

use Orkestra\Bundle\PdfBundle\Generator\AbstractPdfGenerator; use Symfony\Component\OptionsResolver\OptionsResolverInterface; use Flux\CustomerBundle\Entity\Customer; use Flux\CustomerBundle\Entity\Invoice; use Flux\CustomerBundle\Entity\InvoiceConcept;

class InvoiceGenerator extends AbstractPdfGenerator { /* * Performs the PDF generation * @param array $parameters An array of parameters to be used to render the PDF * @param array $options An array of options to be passed to the underlying PdfFactory * @return \Orkestra\Bundle\PdfBundle\Pdf\PdfInterface / protected function doGenerate(array $parameters, array $options) { $hasBorder = false; /* @var Invoice $invoice / $invoice = $parameters['invoice']; /* @var Customer $customer / $customer = $invoice->getCustomer();

    // Use the createPdf method to create the desired type of PDF
    $options['className'] = 'Flux\CustomerBundle\PdfGenerator\MyCustomTcpdf';
    $pdf = $this->createPdf('tcpdf', $options);

    // Call any native methods on the underlying library object
    /** @var \TCPDF $builder */
    $builder = $pdf->getNativeObject();

    $builder->SetAuthor('FLUX, S.L.');
    $builder->SetTitle('Factura ' . $invoice->getRef());
    $builder->SetSubject('Factura en PDF');
    $builder->SetKeywords('PDF, factura');
    $builder->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH);
    $builder->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
    $builder->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
    $builder->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
    $builder->SetMargins(35, 30, 30); // (left, top, right)
    $builder->SetHeaderMargin(PDF_MARGIN_HEADER);
    $builder->SetFooterMargin(PDF_MARGIN_FOOTER);
    $builder->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
    //$builder->SetAutoPageBreak(FALSE, PDF_MARGIN_BOTTOM);
    $builder->setImageScale(PDF_IMAGE_SCALE_RATIO);
    $builder->setFontSubsetting(true);
    $builder->addPage();

(...)

But the result doesn't print any header or footer.

davidromani commented 9 years ago

Solved.

Inside MyCustomTcpdf class there are footer & header methods. So it's easy, call $builder->header();