php 使用 fpdf 以 PDF 格式显示图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3664525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:37:00  来源:igfitidea点击:

Display image in PDF using fpdf

phpimagepdffpdf

提问by anonymous123

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

我想在我创建的 PDF 文件中插入图像。但是,它根本不会定位得很好。

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.

并非所有图像都会出现。每页仅显示 1 张图像,但尺寸正确。

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

实际上,我在 while 循环中插入了一个图像。我想在 pdf 文件中显示的是:(按顺序)

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

回答by Joshua Pinter

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.

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

回答by Naveed

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