php - 检测何时仅剩一行空间以及何时使用 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() 是创建我需要添加的行的函数。
解决方案
这就是我检测表格行是否会溢出到下一页的方式:
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
推荐阅读
- javascript - JavaScript 的 typeof 的性能开销是多少(编辑:在 V8 中)?
- oracle - How to inline a recordtype variable during a procedure call?
- html - HTML 网站不会随移动设备上的视口代码缩放
- android - Eclipse Maven 项目中的 ThreeTenABP
- function - How to refer to a dynamically named variable with a variable?
- c# - 一种序列化方法会产生内存不足错误,另一种则不会
- activejdbc - 如何将 LazyList 的 toJson 方法与 json 数组一起使用?
- php - behat.yml 抛出错误:保留的指示符“%”不能开始一个普通的标量;
- javascript - JSON 值未定义?
- pandas - 计算滚动(平均差)^2 熊猫