You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用FPDF在PDF文件中添加多张图片?代码仅显一张图求排查

Hey there! Let's figure out why only one image is showing up in your FPDF-generated PDF. This is a common issue, and usually boils down to a few key missteps with how you're adding or positioning the images. Let's break down the most likely causes and fix them together.

Common Reasons & Fixes

1. Missing Position Adjustments or Page Breaks

FPDF draws images starting from the current cursor position. If you don't update this position after each image, subsequent images will either overlap the first one or get pushed off the visible page area entirely.

  • Fix: After adding an image, adjust the Y-coordinate (using $pdf->SetY()) or use $pdf->Ln() to create space. If an image's height would exceed the remaining page space, manually add a new page with $pdf->AddPage().

2. Incorrect or Unchanging Image Paths

If you're looping through images but reusing the same path variable (or the path is invalid for some files), FPDF will only render the first valid image and skip the rest without throwing an obvious error.

  • Fix: Double-check that each image path is correct and that your loop is actually updating the path variable. You can add a quick check with file_exists($path) to catch missing files early.

3. Oversized Image Dimensions

If you set an image's width/height to be too large, it might take up the entire page, leaving no space for other images. Even if you omit dimensions, FPDF uses the image's original size, which could exceed the page bounds.

  • Fix: Scale images to fit the page. For example, set the width to match the page minus margins, and let FPDF auto-calculate the height to maintain aspect ratio.
Working Example Code

Here's a robust snippet that handles multiple images with positioning, pagination, and error checking:

require('fpdf.php');

$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial', 'B', 16);
$pdf->Cell(40, 10, 'Multiple Images Demo');
$pdf->Ln(20); // Add space after header

// List of your image paths (update these to your actual files)
$imagePaths = [
    'sunset.jpg',
    'mountain.png',
    'flower.jpeg'
];

$currentY = 30; // Starting Y position after header
$pageWidth = $pdf->GetPageWidth();
$imageWidth = $pageWidth - 20; // Leave 10px margins on left/right

foreach ($imagePaths as $path) {
    if (file_exists($path)) {
        // Get original image dimensions to calculate scaled height
        list($origWidth, $origHeight) = getimagesize($path);
        $imageHeight = ($origHeight / $origWidth) * $imageWidth;

        // Check if adding this image would go past the page bottom (20px margin)
        if ($currentY + $imageHeight > 297 - 20) {
            $pdf->AddPage();
            $currentY = 30; // Reset position for new page
        }

        // Add the image at calculated position
        $pdf->Image($path, 10, $currentY, $imageWidth, $imageHeight);
        $currentY += $imageHeight + 10; // Add 10px gap between images
    } else {
        // Handle missing images gracefully
        $pdf->Ln(10);
        $pdf->Cell(0, 10, "⚠️ Image not found: $path", 0, 1);
        $currentY += 20;
    }
}

$pdf->Output();

Compare your code to this example—odds are you're missing a position update, page break, or have a path issue. This snippet should resolve the single-image problem by ensuring each image is placed correctly and fits within the page bounds.

内容的提问来源于stack exchange,提问作者amit munde

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:20:56