Display image in PDF using fpdf

anonymous123 picture anonymous123 · Sep 8, 2010 · Viewed 29k times · Source

I want to insert an image in my created PDF file. However, it won't position well at all.

If I do this:

$fpdf->Image($row_products['prod_imagelarge'], 10); 

The images will appear however, they're too big.

If I do this:

$fpdf->Image($row_products['prod_imagelarge'],30, 40, 40, 40);

Not all images will appear. Only 1 image per page will appear but with the right size.

Actually, I am inserting an image inside a while loop. What I would want to display in the pdf file is: (in order)

-product name (works fine)  
-product image (the problem is here!)  
-product description (works fine)

Answer

Joshua Pinter picture Joshua Pinter · Sep 29, 2010

Similar to Naveed, but a little more complete with the rest of your row data. The trick is to capture the X and Y position before placing the image and then manually set the abscissa ("position") to the proper place, given the new image.

$image_height = 40;
$image_width = 40;
while ($row_products = mysql_fetch_array($products)) { 
   $fpdf->Cell(0, 0, $row_products['prod_name'], 0, 2);
   $fpdf->Cell(0, 0, $row_products['prod_description'], 0, 2);

   // get current X and Y
   $start_x = $fpdf->GetX();
   $start_y = $fpdf->GetY();

   // place image and move cursor to proper place. "+ 5" added for buffer
   $fpdf->Image($row_products['prod_imagelarge'], $fpdf->GetX(), $fpdf->GetY() + 5, 
                $image_height, $image_width) 
   $fpdf->SetXY($start_x, $start_y + $image_height + 5);
}