Search code examples
phpfpdf

How to add table headers on every FPDF page


Note the picture below. i want put table header on page 2 like table header on page 1. enter image description here

I used the below code and when use the header function it gave me the below error.

$pdf' (T_VARIABLE), expecting function (T_FUNCTION) in D:\xampp\htdocs\karyawan\upah\cetak_upah_h.php on line 75

This is my code :

    require_once ("../fpdf/fpdf.php");
        class h4PDF extends FPDF{
        function Header(){
            $this->Cell(0.5, 1, "No", 1, '0', "C", false);
            $this->Cell(3, 1, "Nama", 1, '0', "C", false);
            $this->Cell(2.9, 0.5, "Masuk Kerja", 1, '0', "C", false);
            $this->Cell(1.5, 0.5, "Lembur", 1, '0', "C", false);
            $this->Cell(0.5, 0.5, "Pre", "T", '0', "C", false);
            $this->Cell(5, 0.5, "Upah", 1, '0', "C", false);
            $this->Cell(3.75, 0.5, "Lembur", 1, '0', "C", false);
            $this->Cell(1.25, 1, "Premi", 1, '0', "C", false);
            $this->Cell(1.5, 1, "Total", 1, '0', "C", false);
            $this->Ln(0.5);
            $this->Cell(3.5, 0.5, "", 0, '0', "C", false);
            $this->Cell(0.6, 0.5, "Hari", 1, '0', "C", false);
            $this->Cell(1, 0.5, "1/2Hari", 1, '0', "C", false);
            $this->Cell(0.5, 0.5, "UT", 1, '0', "C", false);
            $this->Cell(0.8, 0.5, "Jam", 1, '0', "C", false);
            $this->Cell(0.5, 0.5, "1", 1, '0', "C", false);
            $this->Cell(0.5, 0.5, "2", 1, '0', "C", false);
            $this->Cell(0.5, 0.5, "Lbr", 1, '0', "C", false);
            $this->Cell(0.5, 0.5, "mi", 1, 'B', "C", false);
            $this->Cell(1.25, 0.5, "Kerja", 1, '0', "C", false);
            $this->Cell(1.25, 0.5, "1/2 Hari", 1, '0', "C", false);
            $this->Cell(1.25, 0.5, "UT", 1, '0', "C", false);
            $this->Cell(1.25, 0.5, "Sejam", 1, '0', "C", false);
            $this->Cell(1.25, 0.5, "Lb 1", 1, '0', "C", false);
            $this->Cell(1.25, 0.5, "Lb 2", 1, '0', "C", false);
            $this->Cell(1.25, 0.5, "Lb Libur", 1, '0', "C", false);
            $this->Ln(0.5);
        }
    }

$pdf = new FPDF('P','cm',array(21.5,33));

$pdf->AddFont('verdana','','verdana.php');

$pdf->AddFont('verdanaB','','verdanaB.php');

$pdf->setTopMargin(1);

$pdf->setLeftMargin(0.6);

$pdf->setRightMargin(0.6);

$pdf->SetAutoPageBreak(false);

$pdf->SetFont('verdanaB','','12');

$pdf->SetFillColor(255, 255, 255);

$pdf->SetDrawColor( 0, 0, 0, 255);

$pdf = new h4PDF();

$pdf->AddPage();

Note: page can be more than two


Solution

  • You have to create a header function and extend the FPDF. Below is an example code you can experiment with it.

    Header:

    class h4PDF extends FPDF{
        function Header(){
            $this->Image('../../images/logo.png', 10,6,30);
            $this->SetFont('Arial', 'B', 12);
            $this->Cell(80);
            $this->Cell(50, 10, 'Report Name', 0, 0,'C');
            $this->Ln(10);    
            $this->SetFont('verdana','','8');
            $this->Cell(0.5, 1, "No", 1, '0', "C", false);
            $this->Cell(3, 1, "Nama", 1, '0', "C", false);
            $this->Cell(2.9, 0.5, "Masuk Kerja", 1, '0', "C", false);
            $this->Cell(1.5, 0.5, "Lembur", 1, '0', "C", false);
            $this->Cell(0.5, 0.5, "Pre", "T", '0', "C", false);
            $this->Cell(5, 0.5, "Upah", 1, '0', "C", false);
            $this->Cell(3.75, 0.5, "Lembur", 1, '0', "C", false);
            $this->Cell(1.25, 1, "Premi", 1, '0', "C", false);
            $this->Cell(1.5, 1, "Total", 1, '0', "C", false);
            $this->Ln(0.5);
            $this->Cell(3.5, 0.5, "", 0, '0', "C", false);
            $this->Cell(0.6, 0.5, "Hari", 1, '0', "C", false);
            $this->Cell(1, 0.5, "1/2Hari", 1, '0', "C", false);
            $this->Cell(0.5, 0.5, "UT", 1, '0', "C", false);
            $this->Cell(0.8, 0.5, "Jam", 1, '0', "C", false);
            $this->Cell(0.5, 0.5, "1", 1, '0', "C", false);
            $this->Cell(0.5, 0.5, "2", 1, '0', "C", false);
            $this->Cell(0.5, 0.5, "Lbr", 1, '0', "C", false);
            $this->Cell(0.5, 0.5, "mi", 1, 'B', "C", false);
            $this->Cell(1.25, 0.5, "Kerja", 1, '0', "C", false);
            $this->Cell(1.25, 0.5, "1/2 Hari", 1, '0', "C", false);
            $this->Cell(1.25, 0.5, "UT", 1, '0', "C", false);
            $this->Cell(1.25, 0.5, "Sejam", 1, '0', "C", false);
            $this->Cell(1.25, 0.5, "Lb 1", 1, '0', "C", false);
            $this->Cell(1.25, 0.5, "Lb 2", 1, '0', "C", false);
            $this->Cell(1.25, 0.5, "Lb Libur", 1, '0', "C", false);
            $this->Ln(0.5);
        }
    }
    

    When you start the creation of your PDF call the above function,

    $pdf = new h4PDF();
    $pdf->AddPage();
    
    while(odbc_fetch_row($pjk)){
    //Skip.. (This code is too long to display)
    if($no==61){$pdf->AddPage();}
    
    //Skip.. (This code is too long to display)
    
    }