首页 > 解决方案 > 检测何时仅剩一行空间以及何时使用 FPDF 添加新页面

问题描述

在这个项目中,我使用 FPDF 生成带有表格的 PDF 文件。我设法设置了表结构,但我想在需要时添加表头和表尾。

代码:

<?php
function tableTitle($pdf){

    $pdf -> SetFillColor(214, 214, 214);
    $pdf -> Cell(28, 5, utf8_decode('MATRÍCULA'), 1, 0, 'C', 1);
    $pdf -> Cell(106, 5, utf8_decode('NOME'), 1, 0, 'C', 1);
    $pdf -> Cell(28, 5, utf8_decode('CPF'), 1, 0, 'C', 1);
    $pdf -> Cell(28, 5, utf8_decode('MÉDIA'), 1, 0, 'C', 1);
    $pdf -> Ln();
}

function tableBody($pdf, $matricula, $nome, $cpf, $media) {
    
    for ($i=0; $i 100< ; $i++) { 
    //For testing purpose, data input will come later
    
    $pdf -> Cell(28, 5, utf8_decode($matricula), 1, 0, 'C');
    $pdf -> Cell(106, 5, utf8_decode($nome), 1, 0, 'C');
    $pdf -> Cell(28, 5, utf8_decode($cpf), 1, 0, 'C');
    $pdf -> Cell(28, 5, utf8_decode($media), 1, 0, 'C');
    $pdf -> Ln();

    }

}

tableTitle($pdf);
tableBody($pdf, "55555/20", "LOREM IPSUM DOLOR SIT AMET", "xxx.xxx.xxx-xx", 10.00);


$pdf -> Output();
?>

tableTitle() 是创建我需要添加的行的函数。

标签: phpfpdf

解决方案


这就是我检测表格行是否会溢出到下一页的方式:

if (($ourpdf->GetY() + ($linecount * 5)) > $ourpdf->pagebreak) {  // data won't fit on this page

    $ourpdf->AddPage();   // page break
    pcs_Header($ourpdf);  // add the table header
    $fill = true;         // force next row of table to be filled

}  // end of not enough room for the data on this page

这是在pcs_Header需要时调用以输出新表头的函数。

function pcs_Header($ourpdf) {

    $ourpdf->SetWidths([43,28,25,20,30,25,13,23]);
    $ourpdf->SetAligns(['C','C','C','C','C','C','C','C']);
    $fill = false;
    $ourpdf->SetFont('','',10);
    $ourpdf->Row(['PCS Name'        ,
                  'PCS ID'          ,
                  'PCS Type'        ,
                  'PCS Risk Rating' ,
                  'Street Address'  ,
                  'City'            ,
                  'Zip'             ,
                  'County'],0,true);

}  // end of the pcs_Header function

推荐阅读