Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display image in PDF using fpdf

Tags:

php

image

pdf

fpdf

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)
like image 445
anonymous123 Avatar asked Sep 08 '10 03:09

anonymous123


2 Answers

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);
}
like image 162
Joshua Pinter Avatar answered Sep 22 '22 10:09

Joshua Pinter


If one page contains many images then may be your images are placed on each others. You should change position for each image on one page. Try something like this.

for( $i=10; $i<=200; $i=$i+10 ) {
  $fpdf->Image($row_products['prod_imagelarge'],30, $i, 40, 40);
}
like image 32
Naveed Avatar answered Sep 20 '22 10:09

Naveed