2010-09-08 167 views
5

我想在創建的PDF文件中插入圖像。但是,它不會很好地定位。使用fpdf在PDF中顯示圖像

如果我這樣做:

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

然而,圖像會出現,他們是太大了。

如果我這樣做:

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

並非所有的圖像將出現。每頁只有1張圖片會出現,但是 的大小合適。

其實,我在while循環中插入圖像。 我希望在PDF文件中顯示的是:(按順序)

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

回答

5

如果一個頁面中包含許多圖像,然後可能是你的圖像放置在每個人。您應該在一個頁面上更改每個圖像的位置。嘗試這樣的事情。

for($i=10; $i<=200; $i=$i+10) { 
    $fpdf->Image($row_products['prod_imagelarge'],30, $i, 40, 40); 
} 
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); 
}