【问题标题】:fpdf page break issuefpdf分页问题
【发布时间】:2012-09-03 05:02:06
【问题描述】:

我有这个循环打印 6 行(多单元格)大约 30 次。问题是,当它到达底部页面时,它会从多单元格中打印 2 或 3 行,而在下一页上它会打印其他 3 行。如果当前页面上的所有 6 行没有足够的空间,我想让它在下一页上打印所有 6 行。 我使用此代码:

$height_of_cell = 60; mm
$page_height = 279.4; // mm (portrait letter)
$bottom_margin = 20; // mm
$space_left = $page_height - $p->GetY(); // space left on page
$space_left -= $bottom_margin; // less the bottom margin
if ( $height_of_cell >= $space_left) {
$p->Ln();                        
$p->AddPage(); // page break
$p->Cell(100,5,'','B',2); // this creates a blank row for formatting reasons
}

但它不起作用。有什么解决办法吗?谢谢!

【问题讨论】:

    标签: php pdf pdf-generation fpdf


    【解决方案1】:

    使用GetY 获取当前位置,从文档的高度中减去它。如果这小于多单元格高度的 6 倍(您有 6 行),则使用 AddPage 强制分页。

    我知道你解决了这个问题,但为了其他人的利益,这应该给出一个广泛的想法。

    <?php
    $p = new FPDF();
    $p->AddPage();
    $p->SetFont('Arial','B',16);
    $p->SetAutoPageBreak(false);
    $height_of_cell = 60; // mm
    $page_height = 286.93; // mm (portrait letter)
    $bottom_margin = 0; // mm
      for($i=0;$i<=100;$i++) :
        $block=floor($i/6);
        $space_left=$page_height-($p->GetY()+$bottom_margin); // space left on page
          if ($i/6==floor($i/6) && $height_of_cell > $space_left) {
            $p->AddPage(); // page break
          }
        $p->Cell(100,10,'This is a text line - Group '.$block,'B',2);
      endfor;
    $p->Output();
    ?>
    

    【讨论】:

    • 嗨,我修改了这样的代码,但它什么也没做。您在某处看到错误吗?
    • 因为它对我有用,请注意对于 A4 $page_height = 297;
    • SetAutoPageBreak(boolean auto , float margin) fx 帮了我。第二个参数是定义触发限制的页面底部的距离。默认开启模式,边距为 2 厘米。
    • 是的,绝对是@Ravs,这将是典型的应用程序。这里的 OP 想要确保所有 6 行都适合,如果 1-5 行适合页面,只需使用 autopagebreak 就会破坏他的表格,这就是为什么他需要更精细的解决方案。
    猜你喜欢
    • 2011-01-26
    • 2012-01-10
    • 2016-06-18
    • 2014-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-04
    • 2013-12-29
    相关资源
    最近更新 更多