【问题标题】:FPDF - Cell / Image is moved to next page for no apparently reasonFPDF - 单元格/图像无故移至下一页
【发布时间】:2015-12-03 15:34:20
【问题描述】:

我有一段相当简单的代码,可以在 FPDF 中生成多行图像,每行 2 个图像,每页 5 行。

由于某种原因,FPDF 在放置倒数第二张图像后应用分页符。我尝试将边距设置为 0,当图像达到 10 时强制分页...但没有任何效果。

//-- PDF build

// Set the frameborder to 1 to help while we build the layout
$layout_frameborder = 0;

// Define some standard column sizes, divide width of paper by 24
$col = 8.125;

class PDF extends FPDF {
    function Header() {
        global $layout_frameborder, $col, $timeframe_string, $confidential, $listing_rsn;
        //-- Listing data
        $sql    =   "SELECT property.name,property.city_or_area,listing.local_price,listing.currency,listing.fk_property_rsn
                    FROM listing_rwx AS listing
                    JOIN property 
                        ON listing.fk_property_rsn = property.rsn
                    WHERE listing.rsn = '$listing_rsn'";
        $query  = mysql_query($sql) or die(mysql_error() . $sql);
        $result = mysql_fetch_array($query);

        $price          = $result['currency'] . ' ' . number_format($result['local_price']);
        $listing_name   = $result['name'];
        $city_or_area   = $result['city_or_area'];
        $block          = getPropertyProperty($result['fk_property_rsn'], 'Block');
        $parcel         = getPropertyProperty($result['fk_property_rsn'], 'Parcel');

        $today = date("F j, Y"); 
        $this->Image($_SESSION['affiliate']['logo_png'], 10, 10, 30, 20);
        $this->SetFont('Helvetica', 'B', 12);
        $this->Cell($col * 8);
        $this->Cell($col * 8,5, $listing_name, $layout_frameborder, 0, 'C');
        $this->Ln();
        $this->SetFont('Helvetica', 'B', 9);
        $this->Cell($col * 8);
        $this->Cell($col * 8, 4, "Section: " . $city_or_area, $layout_frameborder, 0, 'C');
        $this->Ln();
        $this->Cell($col * 8);
        $this->Cell($col * 8, 4, "Block / Parcel: " . $block . '/' . $parcel, $layout_frameborder, 0, 'C');
        $this->Ln();
        $this->Cell($col * 8);
        $this->Cell($col * 8, 4, "Price: " . $price, $layout_frameborder, 0, 'C');
        $this->Ln(12);      
    }

    function Footer() {
        global $col;
        //include("includes/pdf_footer.php");
    }
}

//Build PDF
$pdf = new PDF('P', 'mm', 'Legal');
$pdf->SetAutoPageBreak(true, 40);
$pdf->AddPage();

$sql    = "SELECT file FROM listing_file WHERE fk_listing_rsn = '$listing_rsn' AND status_flag IN('OK','NEW','UPDATED')";
$query  = mysql_query($sql) or die(mysql_error() . $sql);
$x = 0;
$y = 1;
while ($result = mysql_fetch_array($query)) {
    $image = str_replace("/var/www/html/images", "http://images.cbislands.com", $result['file']); 
    if ($x == 0) {
        $pdf->Cell($col * 10, 60, $pdf->Image($image, $pdf->GetX(), $pdf->GetY(), $col * 10, 56), 0, 0, 'C', false);
        $pdf->Cell($col * 4);
    }
    if ($x == 1) {
        $pdf->Cell($col * 10, 60, $pdf->Image($image, $pdf->GetX(), $pdf->GetY(), $col * 10, 56), 0, 0, 'C', false);
        $pdf->Ln();
    }
    $x++;
    $y++;
    if ($x == 2) {
        $x = 0;
    }
    if($y == 10) {
        $y = 1;
    }
}

$pdf->Output();

这是它输出的屏幕截图(我已经模糊了识别图像):

更新:下面发布的具有自动递增 Y 位置的工作代码:

//Build PDF
$pdf = new PDF('P','mm','Legal');
$pdf->SetAutoPageBreak(false,20);
$pdf->AddPage();

$sql    = "SELECT file FROM listing_file WHERE fk_listing_rsn = '$listing_rsn' AND status_flag IN('OK','NEW','UPDATED') AND type = 'IMAGE'";
$query  = mysql_query($sql) or die(mysql_error().$sql);
$x      = 0; // Images on x axis
$y      = 1; // Total images
$row    = 1; // Rows
$yPos   = 40; // Initialize static y position
while ($result = mysql_fetch_array($query)) {
    $image = str_replace("/var/www/html/images", "http://images.cbislands.com", $result['file']); 
    $pdf->SetY($yPos);
    if ($x == 0) {  
        $pdf->SetX(10);
        $pdf->Cell($col * 10, 10, $pdf->Image($image, $pdf->GetX(), $pdf->GetY(), $col * 10, 56), 0, 0, 'C', false );
        $pdf->Cell($col * 4);
    }
    if ($x == 1) {
        $pdf->SetX(120);
        $pdf->Cell($col * 10, 10, $pdf->Image($image, $pdf->GetX(), $pdf->GetY(), $col * 10, 56), 0, 0, 'C', false );
        $pdf->Ln();
    }
    if($y % 2 == 0) {
        if($row < 5) {
            $yPos = $yPos + 60;
            $row++;
        }
        else {
            // Reset the values
            $yPos   = 40;
            $row    = 1;
            $pdf->AddPage();
        }
    }
    $x++;
    $y++;
    if ($x == 2) {
        $x = 0;
    }
    if($y == 10) {
        $y = 1;
    }
}

$pdf->Output();

【问题讨论】:

    标签: php pdf fpdf


    【解决方案1】:

    我无法构建和运行您的示例代码,但症状和示例页面强烈表明图像定位的垂直移动发生在页面上的最后一个图像之前而不是在它之后,放置它在下一页的右上角,而不是当前页面的右下角。

    似乎正在发生的事情是,虽然水平定位(您的 X 移动)被定义为发生在图像之前,而您的垂直 (Y) 移动应该发生在它之后,但两者都与图像位置相关联。因此,在放置图像之前评估和执行这两个动作,并且垂直下降触发分页并将其放置在下一页上。您需要将图像放置与垂直定位分开。

    您在第 10 张图片之后强制分页的策略是正确的,但我相信这是在做出分页决定之后发生的。

    【讨论】:

    • 嘿,感谢您的评论,您的帖子帮助我专注于 Y 轴问题。我编辑了代码并为 Y 轴设置了一个增量,我还发现单元格被移动到下一页,而这些单元格内的图像实际上被渲染在不同的位置。我不完全确定为什么我添加了工作代码:
    猜你喜欢
    • 2018-12-08
    • 2015-11-13
    • 2022-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-27
    相关资源
    最近更新 更多