0

I am trying to create id cards from records in my database. where the photo of a person would be on the left side and items of information like would be printed by the side and beneath as in the screenshot below. Screenshot of pdf output showing pagination issue

Using fpdf cells I am able to create pdfs with pagination. But for the said purpose I thought using the Rect() would be the best for the task, however, It does not respond to autopage break. I also realize there is a logical error in my code but this is the only way I could get the 3 column output. I shall be glad to receive assistance correcting the errors in the code below. Thank you!

class PDF extends FPDF {
    function Header(){
        $this->SetFont('Arial', '', 10);
        $this->Cell(18, 10, '', 0);
        $this->SetFont('Arial', '', 9);
        $this->Cell(35, 10, 'Date: '.date('d-m-Y').'', 0);
        $this->Ln(12);
        $this->SetFont('Arial', 'B', 11);
        $this->Cell(70, 8, '', 0);
        $this->Cell(100, 8, 'LIST', 0);
        $this->Ln(13);
    }

    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();


$staff = mysql_query("SELECT * FROM staff");
$item = 0;
$width = 60;
$height = 40;
$x = 15;
$y = 35;
$margin = 2;
while($a_staff = mysql_fetch_array($staff)){
    $item = $item+1;
    for($i = 0; $i < 3; $i++){
        $pdf->Rect($x, $y, $width, $height, 'D');
        $pdf->Image('images.gif' , $x+$margin ,$y+$margin, 25 , 25);
        $x = ($x + $width + $margin);
    }
    $y = $y + $height + $margin;
    $x = 15;
}

$pdf->Output( "report.pdf", "I" );
?>
BrightGuy
  • 1
  • 2
  • You should avoid learning or writing new code using PHP's `mysql_*` functions. They have been removed in the latest version and your code won't work in the future. Please read [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) for information on why and what to replace them with. – Matt Raines Jul 15 '16 at 08:35

1 Answers1

0

Since you know the starting Y coordinate and the height of the content you are adding, you can do the pagination manually.

After $y = $y + $height + $margin;, add:

// replace 20 with whatever margin you need at the bottom of the page
if ($y + $height > $pdf->h - 20) {
    $pdf->AddPage();
    $y = 35;
}
Matt Raines
  • 4,149
  • 8
  • 31
  • 34