du1108 2015-12-03 15:34
浏览 77

FPDF - Cell / Image被移动到下一页,原因并非明显

I have a fairly simple piece of code that generates rows of images in FPDF, 2 images per row, 5 rows per page.

For some reason FPDF is applying a page break after the second to last image is placed. I have tried setting margins to 0, forcing a page break when the images hit 10... but nothing is working.

//-- 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();

Here is a screenshot of what it is outputting (I have blurred identifying imagery):

enter image description here

UPDATE: working code with auto incrementing Y position posted below:

//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();
  • 写回答

1条回答 默认 最新

  • douao3636 2015-12-03 21:20
    关注

    I'm not in a position to build and run your sample code, but the symptoms and sample pages strongly suggest that the vertical movement for the image positioning is occurring before the last image on the page instead of after it, placing it on the top right of the next page instead of the lower right of the current one.

    What appears to be happening is that although the horizontal positioning (your X movement) is defined to occur before the image, and your vertical (Y) movement should occur after it, both are being associated with the image location. Thus both movements are evaluated and executed before the image is placed, and the vertical drop triggers pagination and its placement on the next page. You need to separate the image placement from the vertical positioning.

    Your strategy of forcing a pagebreak after the 10th image is on the right track, but I believe that's occurring after the pagination decision has already been made.

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP