I'm starting with Laravel 4 and I'd like to use TCPDF to create documents with custom header and footer, like in this example. To do it, I need to extend the original class somewhere and then use extended class in service provider. I also use https://github.com/maxxscho/laravel-tcpdf as service provider for TCPDF.
TCPDF is a dependency of my laravel-tcpdf service provider. This means it is autoloaded from Laravel. You can create your own class which extends TCPDF directly and then override the header() and footer() method. For example create the file app/pdf/Pdf.php:
<?php namespace Pdf;
class Pdf 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');
}
}
To autoload you new file add "app/pdf"
to you composer.json
autoload
classmap
and then run composer dump-autoload
from your console.
Now create your PDF:
// create new PDF document
$pdf = new \Pdf\Pdf;
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Nicola Asuni');
$pdf->SetTitle('TCPDF Example 003');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
// ....
I know, it's not the best solution, but it should work. My package was a little quick'n'dirty because I needed TCPDF in a Laravel project to and so I created this little package. I will optimize my package so you can override functions easier.
For now, I hope, this will help you!