Php – Add sub-headers in all pages with FPDF

fpdfPHP

I have a simple script that generates a PDF file. I'm getting the info I need from the database and using a foreach to create the pages of the PDF file, and the last page I have some charts and other info that are not retrived from DB. All the pages, except this last one, must have a subheader.

My current code is:

foreach ($sql as $key => $value) 
{
    $pdf->Cell(20, $line_height, $value['sale_id'], '', '', 'L');
    $pdf->Cell(90, $line_height, $value['product'], '', '', 'L');
    $pdf->Cell(90, $line_height, $value['ammount'], '', '', 'L');
    $pdf->Cell(90, $line_height, $value['value'], '', '', 'L');
}

Basically, I need to have the subheader describing each row. But If I do this, all the lines will have the subheader:

foreach ($sql as $key => $value) 
{
    $pdf->SetFont('Arial', 'B', $font_size);
    $pdf->Ln(2 * $line_height);

    $pdf->Cell(20, $line_height, 'Number', '', '', 'L');
    $pdf->Cell(120, $line_height, 'Product', '', '', 'L');
    $pdf->Cell(40, $line_height, 'Ammount', '', '', 'L');
    $pdf->Cell(40, $line_height, 'Value ($)', '', '', 'L');

    // 

    $pdf->SetFont('Arial', 'B', $font_size);
    $pdf->Ln(2 * $line_height);

    $pdf->Cell(20, $line_height, $value['sale_id'], '', '', 'L');
    $pdf->Cell(120, $line_height, $value['product'], '', '', 'L');
    $pdf->Cell(40, $line_height, $value['ammount'], '', '', 'L');
    $pdf->Cell(40, $line_height, $value['value'], '', '', 'L');
}

I need this subheader in each page, except the last one. I've tried to do some crazy code with pageNo() function, but it didn't work.

Thank's!

Best Answer

Ok guys, I solved my problem. For anyone who's looking for something similar, here is what I did: The GetY() function return the Y position of that line, so when it's the first line of the page, this value will be 0 (or a constant, depends of your layout). I just did:

foreach ($sql as $key => $value) 
{

    if ($pdf->GetY() == 0)
    {
        $pdf->SetFont('Arial', 'B', $font_size);
        $pdf->Ln(2 * $line_height);

        $pdf->Cell(20, $line_height, 'Number', '', '', 'L');
        $pdf->Cell(120, $line_height, 'Product', '', '', 'L');
        $pdf->Cell(40, $line_height, 'Ammount', '', '', 'L');
        $pdf->Cell(40, $line_height, 'Value ($)', '', '', 'L');
    }

    $pdf->SetFont('Arial', 'B', $font_size);
    $pdf->Ln(2 * $line_height);

    $pdf->Cell(20, $line_height, $value['sale_id'], '', '', 'L');
    $pdf->Cell(120, $line_height, $value['product'], '', '', 'L');
    $pdf->Cell(40, $line_height, $value['ammount'], '', '', 'L');
    $pdf->Cell(40, $line_height, $value['value'], '', '', 'L');
}

Thank you all!

Related Topic