我正在使用PHP / TCPDF中的Multicell()函数创建一个pdf表。我面临的两个问题是
如何解决分页符问题?例如,如果第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);
答案 0 :(得分:0)
修复很容易。花了一些时间,这是修复。
# 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行的所有行的溢出问题。任何建议或改进都被接受。另一方面,分页符合完美。