I have a fairly simple piece of code that generates rows of images in FPDF, 2 images per row, 5 rows per page.
For some reason FPDF is applying a page break after the second to last image is placed. I have tried setting margins to 0, forcing a page break when the images hit 10... but nothing is working.
//-- PDF build
// Set the frameborder to 1 to help while we build the layout
$layout_frameborder = 0;
// Define some standard column sizes, divide width of paper by 24
$col = 8.125;
class PDF extends FPDF {
function Header() {
global $layout_frameborder, $col, $timeframe_string, $confidential, $listing_rsn;
//-- Listing data
$sql = "SELECT property.name,property.city_or_area,listing.local_price,listing.currency,listing.fk_property_rsn
FROM listing_rwx AS listing
JOIN property
ON listing.fk_property_rsn = property.rsn
WHERE listing.rsn = '$listing_rsn'";
$query = mysql_query($sql) or die(mysql_error() . $sql);
$result = mysql_fetch_array($query);
$price = $result['currency'] . ' ' . number_format($result['local_price']);
$listing_name = $result['name'];
$city_or_area = $result['city_or_area'];
$block = getPropertyProperty($result['fk_property_rsn'], 'Block');
$parcel = getPropertyProperty($result['fk_property_rsn'], 'Parcel');
$today = date("F j, Y");
$this->Image($_SESSION['affiliate']['logo_png'], 10, 10, 30, 20);
$this->SetFont('Helvetica', 'B', 12);
$this->Cell($col * 8);
$this->Cell($col * 8,5, $listing_name, $layout_frameborder, 0, 'C');
$this->Ln();
$this->SetFont('Helvetica', 'B', 9);
$this->Cell($col * 8);
$this->Cell($col * 8, 4, "Section: " . $city_or_area, $layout_frameborder, 0, 'C');
$this->Ln();
$this->Cell($col * 8);
$this->Cell($col * 8, 4, "Block / Parcel: " . $block . '/' . $parcel, $layout_frameborder, 0, 'C');
$this->Ln();
$this->Cell($col * 8);
$this->Cell($col * 8, 4, "Price: " . $price, $layout_frameborder, 0, 'C');
$this->Ln(12);
}
function Footer() {
global $col;
//include("includes/pdf_footer.php");
}
}
//Build PDF
$pdf = new PDF('P', 'mm', 'Legal');
$pdf->SetAutoPageBreak(true, 40);
$pdf->AddPage();
$sql = "SELECT file FROM listing_file WHERE fk_listing_rsn = '$listing_rsn' AND status_flag IN('OK','NEW','UPDATED')";
$query = mysql_query($sql) or die(mysql_error() . $sql);
$x = 0;
$y = 1;
while ($result = mysql_fetch_array($query)) {
$image = str_replace("/var/www/html/images", "http://images.cbislands.com", $result['file']);
if ($x == 0) {
$pdf->Cell($col * 10, 60, $pdf->Image($image, $pdf->GetX(), $pdf->GetY(), $col * 10, 56), 0, 0, 'C', false);
$pdf->Cell($col * 4);
}
if ($x == 1) {
$pdf->Cell($col * 10, 60, $pdf->Image($image, $pdf->GetX(), $pdf->GetY(), $col * 10, 56), 0, 0, 'C', false);
$pdf->Ln();
}
$x++;
$y++;
if ($x == 2) {
$x = 0;
}
if($y == 10) {
$y = 1;
}
}
$pdf->Output();
Here is a screenshot of what it is outputting (I have blurred identifying imagery):
UPDATE: working code with auto incrementing Y position posted below:
//Build PDF
$pdf = new PDF('P','mm','Legal');
$pdf->SetAutoPageBreak(false,20);
$pdf->AddPage();
$sql = "SELECT file FROM listing_file WHERE fk_listing_rsn = '$listing_rsn' AND status_flag IN('OK','NEW','UPDATED') AND type = 'IMAGE'";
$query = mysql_query($sql) or die(mysql_error().$sql);
$x = 0; // Images on x axis
$y = 1; // Total images
$row = 1; // Rows
$yPos = 40; // Initialize static y position
while ($result = mysql_fetch_array($query)) {
$image = str_replace("/var/www/html/images", "http://images.cbislands.com", $result['file']);
$pdf->SetY($yPos);
if ($x == 0) {
$pdf->SetX(10);
$pdf->Cell($col * 10, 10, $pdf->Image($image, $pdf->GetX(), $pdf->GetY(), $col * 10, 56), 0, 0, 'C', false );
$pdf->Cell($col * 4);
}
if ($x == 1) {
$pdf->SetX(120);
$pdf->Cell($col * 10, 10, $pdf->Image($image, $pdf->GetX(), $pdf->GetY(), $col * 10, 56), 0, 0, 'C', false );
$pdf->Ln();
}
if($y % 2 == 0) {
if($row < 5) {
$yPos = $yPos + 60;
$row++;
}
else {
// Reset the values
$yPos = 40;
$row = 1;
$pdf->AddPage();
}
}
$x++;
$y++;
if ($x == 2) {
$x = 0;
}
if($y == 10) {
$y = 1;
}
}
$pdf->Output();
I'm not in a position to build and run your sample code, but the symptoms and sample pages strongly suggest that the vertical movement for the image positioning is occurring before the last image on the page instead of after it, placing it on the top right of the next page instead of the lower right of the current one.
What appears to be happening is that although the horizontal positioning (your X movement) is defined to occur before the image, and your vertical (Y) movement should occur after it, both are being associated with the image location. Thus both movements are evaluated and executed before the image is placed, and the vertical drop triggers pagination and its placement on the next page. You need to separate the image placement from the vertical positioning.
Your strategy of forcing a pagebreak after the 10th image is on the right track, but I believe that's occurring after the pagination decision has already been made.