Search code examples
phpcodeigniterfpdf

FPDF Header Footer Codeigniter 4 Not Showing


I'm trying to add header and footer to my pdf file using fpdf My controller code is:

<?php
namespace App\Controllers;
use App\Libraries\FPDF;

class Test extends BaseController
{
    function Header()
    {
        $this->Image('logo.png',10,6,30);
        $this->SetFont('Arial','B',15);
        $this->Cell(80);
        $this->Cell(30,10,'Test',1,0,'C');
        $this->Ln(20);
    }

    function Footer()
    {
        $this->SetY(-15);
        $this->SetFont('Arial','I',8);
        $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
    }

    public function index()
    {
        $this->pdf = new fpdf();
        $this->pdf->AliasNbPages();
        $this->pdf->AddPage();
        $this->pdf->SetFont('Times','',12);
        for($i=1;$i<=40;$i++)
            $this->pdf->Cell(0,10,'Printing line number '.$i,0,1);
        $this->response->setHeader("Content-Type", "application/pdf");
        $this->pdf->Output();
    }   
}

The pdf output is line numbers only without header footer. Any clue?


Solution

  • Now it's working by changing the code slightly following FPDF tuto2.

    <?php
    namespace App\Controllers;
    
    use App\Libraries\FPDF;
    
    class PDF extends FPDF
    {
        function Header()
        {
            $this->Image('logo.png',10,6,30);
            $this->SetFont('Arial','B',15);
            $this->Cell(80);
            $this->Cell(30,10,'Title',1,0,'C');
            $this->Ln(20);
        }
    
        function Footer()
        {
            $this->SetY(-15);
            $this->SetFont('Arial','I',8);
            $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
        }
    }
    
    $pdf = new PDF();
    $pdf->AliasNbPages();
    $pdf->AddPage();
    $pdf->SetFont('Times','',12);
    for($i=1;$i<=40;$i++)
        $pdf->Cell(0,10,'Printing line number '.$i,0,1);
    $pdf->Output();
    

    Thanks to Olivier PLATHEY for your great and simple FPDF class