fpdf page break issue

Claudiu Creanga picture Claudiu Creanga · Sep 10, 2012 · Viewed 32.9k times · Source

I have this loop that prints 6 rows (multicell) for about 30 times. The issue is that when it reaches the bottom page it prints 2 or 3 rows from the multicell and on the next page it prints the other 3 rows. I want to make it print all 6 rows on the next page if there is not enough space for all 6 rows on the present page. I use this code:

$height_of_cell = 60; mm
$page_height = 279.4; // mm (portrait letter)
$bottom_margin = 20; // mm
$space_left = $page_height - $p->GetY(); // space left on page
$space_left -= $bottom_margin; // less the bottom margin
if ( $height_of_cell >= $space_left) {
$p->Ln();                        
$p->AddPage(); // page break
$p->Cell(100,5,'','B',2); // this creates a blank row for formatting reasons
}

but it doesn't work. Any solutions? Thanks!

Answer

Mark picture Mark · Sep 10, 2012

Use GetY to get the current position, subtract it from the height of your document. If that is less than 6x (you have 6 rows) your multicell height, then force a page break by using AddPage.

I know you fixed this, but for the benefit of anyone else, this should give a broad idea.

<?php
$p = new FPDF();
$p->AddPage();
$p->SetFont('Arial','B',16);
$p->SetAutoPageBreak(false);
$height_of_cell = 60; // mm
$page_height = 286.93; // mm (portrait letter)
$bottom_margin = 0; // mm
  for($i=0;$i<=100;$i++) :
    $block=floor($i/6);
    $space_left=$page_height-($p->GetY()+$bottom_margin); // space left on page
      if ($i/6==floor($i/6) && $height_of_cell > $space_left) {
        $p->AddPage(); // page break
      }
    $p->Cell(100,10,'This is a text line - Group '.$block,'B',2);
  endfor;
$p->Output();
?>