我正在使用PHP/TCPDF中的Multicell()函数创建一个pdf表。我面临的两个问题是
- 表(3x3)的每一行都有不同数量的数据线,因此如何为每一行绘制水平线,考虑到每一行的最大行数
-
如何计算分页符?例如,如果第2行的数据流入下一页。如何在新页面中重写完整的表格,或在新页面的第2行以表格的副标题开始?对这两种情况的任何投入都是值得赞赏的。我尝试了以下文章的输入http://www.onemoretake.com/2009/03/27/revisited-tcpdf-variable-height-table-rows-with-multicell/
public function xyz($arr_1){ $f1 = $arr_1[f1]; $f2 = $arr_1[f2]; $f3 = $arr_1[f3]; $numL1 = substr_count($f1, "'n" ); $numL2 = substr_count($f2, "'n" ); $numL3 = substr_count($f3, "'n" ); $Maxrowlines = max($numL1, $numL2, $numL3)*6; if($this->GetY() + $Maxrowlines > $this->PageBreakTrigger){ $this->AddPage(); } $startx = $this->GetX(); $starty = $this->GetY(); $rowmaxy = $starty + $Maxrowlines; $this->MultiCell(40,10,$txt = $arr_1[f1],'0','L',0, $ln =0); $this->MultiCell(40,10,$txt = $arr_2[f2],'0','L',0, $ln =0); $this->MultiCell(40,10,$txt = $arr_3[f3],'0','L',0, $ln =0); $this ->SetXY($startx, $starty); $tempy = $this->GetY(); $this->SetXY($startx, $tempy); if($temp < $rowmaxy){ $diffy = $rowmaxy - $tempy; $this->MultiCell(40,$diffy, '' , '0', 'C', 0); } else { $this->MultiCell(40,0,'','0','C',0); } $addx = $this->GetX(); $startx+= $addx; } $arr_1 = array(f1=> $apple , f2=> $oranges, f3=> $mangoes); $pdf->xyz($arr_1);
修复很容易。虽然花了一些时间,但这里是解决办法。
# define methods and classes-----------------
# constructors....class...
public function xyz($arr_1){
$f1 = $arr_1[f1];
$f2 = $arr_1[f2];
$f3 = $arr_1[f3];
$numL1 = substr_count($f1, "'n" );
$numL2 = substr_count($f2, "'n" );
$numL3 = substr_count($f3, "'n" );
$Maxrowlines = max($numL1, $numL2, $numL3)*6;
$startx = $this->GetX();
$starty = $this->GetY();
$rowmaxy = $starty + $Maxrowlines;
$this ->SetXY($startx, $starty);
$this->MultiCell(40,10,$txt = $arr_1[f1],'0','L',0, $ln =0);
$this->MultiCell(40,10,$txt = $arr_2[f2],'0','L',0, $ln =0);
$this->MultiCell(40,10,$txt = $arr_3[f3],'0','L',0, $ln =0);
# Now, if the lines in a multicell are overflowing into next row
# get current Y position store it in a variable
# SetXY to the current Y and if the tempy is less than the rowmaxy we..
# calculate extend cell height with that difference
$tempy = $this->GetY();
$this->SetXY($startx, $tempy);
if($tempy < $rowmaxy){
$diffy = $rowmaxy - $tempy;
$this->MultiCell(40,$diffy, '' , '0', 'C', 0);
}
}
#--------------Call the functions.........
#..... invoke the main class and methods...
# main code as follows..
$arr_1 = array(f1=> $apple , f2=> $oranges, f3=> $mangoes);
$rowcount = max($pdf->getNumLines($arr_1 ['f1'],40),
$pdf->getNumLines($arr_1['f2'],40),
$pdf->getNumLines($arr_1 ['f3'],40));
$height_of_cell = $pdf->GetY()+($rowcount*6); # 6 is adjustment of the
# height to font you use. My case, `helvetica`
$dimensions = $pdf->getPageDimensions();
$page_height = $dimensions['hk'];# standard value 286;
$bottom_margin = $dimensions['bm'];# standard value of 20
if($height_of_cell > $page_height - $bottom_margin) {
$pdf->AddPage();
$pdf->your_TableHeader();# call table header if there is one
$pdf->Ln(10); # No. Line you want to start printing data below header
}
$pdf->xyz($arr_1);
更新,看起来这个答案没有解决所有行的溢出问题——它特定于1行。接受任何建议或改进。另一方面,分页符的工作非常完美。