Search code examples
phpfpdf

tFPDF override Footer and Header methods?


I am using tFPDF, how can I override Footer and Header methods? I am initiating my pdf with this code:

$pdf = new tFPDF('P', 'mm', 'A4');

So this code it is not working for me

class PDF extends tFPDF
{
// Page header
function Header()
{
    // Logo
    $this->Image('logo.png',10,6,30);
    // Arial bold 15
    $this->SetFont('Arial','B',15);
    // Move to the right
    $this->Cell(80);
    // Title
    $this->Cell(30,10,'Title',1,0,'C');
    // Line break
    $this->Ln(20);
}

// Page footer
function Footer()
{
    // Position at 1.5 cm from bottom
    $this->SetY(-15);
    // Arial italic 8
    $this->SetFont('Arial','I',8);
    // Page number
    $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
}

Solution

  • The example code is here: https://tcpdf.org/examples/example_003

    From this you can easily see what goes wrong. You create a new PDF with:

    $pdf = new tFPDF('P', 'mm', 'A4');
    

    calling the original tFPDF class, not the class you created with your custom header and footer. You should use:

    $pdf = new PDF('P', 'mm', 'A4');
    

    Since PDF is the class with your header and footer in it.