使用fpdf在PDF中显示图像

6

我想在我创建的PDF文件中插入一张图片。但是,它的位置不太好。

如果我这样做:

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

图像会出现,但它们太大了。
如果我这样做:
$fpdf->Image($row_products['prod_imagelarge'],30, 40, 40, 40);

并非所有图片都会出现。每页只会出现一张图片,但大小将正确显示。

实际上,我正在while循环中插入一张图片。 我想在pdf文件中显示的内容是:(按顺序)

-product name (works fine)  
-product image (the problem is here!)  
-product description (works fine)
2个回答

7

与Naveed类似,但更完整地涉及您的行数据。诀窍是在放置图像之前捕获X和Y位置,然后手动将横坐标(“位置”)设置为给定新图像的正确位置。

$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);
}

5
如果一个页面包含许多图像,则可能会出现图像彼此重叠的情况。您应该为页面上的每个图像更改位置。尝试类似以下的方法。
for( $i=10; $i<=200; $i=$i+10 ) {
  $fpdf->Image($row_products['prod_imagelarge'],30, $i, 40, 40);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接