Search code examples
phppdffpdf

FPDF after page break using SetAutoPageBreak multicell values are not inserted in the defined Y position


I am trying to display data using multiCell. And when the data in the page arrives to y=228 I want it to go to the next Page And be displayed in the position y=112.

As first Step I tried to only add 2 simple conditions :

when the data arrives to position y=228 create a new page when the data goes to the next Page display the result at position =112

It worked. But if the current multiCell content is large it dosn't go to the next page untill it finishs writting all the multicell content , so I added the function SetAutoPageBreak So it inserts a page Break when y=228 . Here where the problems start The code doesnt insert my data in the new page in the position I defined (y=112) it insert it in the start .I don't know how to fix this problem I hope I could find some help I will appreciate it.

Here is My code :

<?php
require ('../fpdf/fpdf.php');

$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial', 'B', 12);
$y = $pdf->GetY();
$x = $pdf->GetX();
$width_cell = array(
    10,
    30,
    50
);
$pdf->SetFillColor(193, 229, 252);
$pdf->SetY(112);

$pdf->Cell($width_cell[0], 10, 'ID', 1, 0, C, true);
$pdf->Cell($width_cell[1], 10, 'NAME', 1, 0, C, true);
$pdf->Cell($width_cell[2], 10, 'CLASS', 1, 1, C, true);

$pdf->SetFont('Arial', '', 10);

for ($i = 0;$i < 30; $i++) {
    $pdf->Cell($width_cell[0], 10, $i, 1, 0, C, false);
    $pdf->Cell($width_cell[1], 10, 'John Deo', 1, 0, C, false);
    $pdf->Cell($width_cell[2], 10, 'Four', 1, 1, C, false);
    $y = $pdf->GetY();
    $pdf->Cell($width_cell[0], 10, $i, 1, 0, C, false);
    $pdf->Cell($width_cell[1], 10, 'Y:' . $y, 1, 0, C, false);

    // $pdf->Cell($width_cell[2],10,'Four',1, 1, C, false);
    $pdf->MultiCell($width_cell[2], 10, 'four four four four four four four four four four four four four four four four four four four four four four ', 1, C, false);

    // Uncomment this line to see what Happends when the Page Break is inserted
    // $pdf->SetAutoPageBreak(auto,69);
    $y = $pdf->GetY();
    if ($y > 228 && $i != 29) {
        $pdf->AddPage();
        $pdf->SetY(112);
    }

    /*
    if ($pdf->PageNo() != 1 && $y < 20){


       $pdf->SetY(112);
    }
    */
}
$pdf->Output();

?>

Solution

  • Extend fPDF and add a header function which positions Y where you want it every time a new page is started.

    require ('fpdf.php');
    class PDF extends FPDF {
    
        function Header() {
    
            $this->SetY(112);
        }
    
    }  // end of the PDF class
    
    $pdf = new pdf();
    $pdf->AddPage();
    $pdf->SetFont('Arial','B',12);
    $y=$pdf->GetY();
    $x=$pdf->GetX();
    $width_cell=array(10,30,50);
    $pdf->SetFillColor(193,229,252);
    $pdf->SetY(112);
    

    The rest of your code goes here and you DO want to uncomment the AutoPageBreak line. You'll also want to change the AutoPageBreak line to read

    $pdf->SetAutoPageBreak(1,69);
    

    since the first argument is a Boolean to indicate whether or not it should be enabled.