php fpdf分页问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12349827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:24:00  来源:igfitidea点击:

fpdf page break issue

phppdfpdf-generationfpdf

提问by Claudiu Creanga

I have this loop that prints 6 rows (multicell) for about 30 times. The issue is that when it reaches the bottom page it prints 2 or 3 rows from the multicell and on the next page it prints the other 3 rows. I want to make it print all 6 rows on the next page if there is not enough space for all 6 rows on the present page. I use this code:

我有这个循环打印 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
}

but it doesn't work. Any solutions? Thanks!

但它不起作用。任何解决方案?谢谢!

回答by Mark

Use GetYto get the current position, subtract it from the height of your document. If that is less than 6x (you have 6 rows) your multicell height, then force a page break by using AddPage.

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

I know you fixed this, but for the benefit of anyone else, this should give a broad idea.

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

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