Multicell table 如何根据每行行数绘制边框,防止数据流入新页面

In Multicell table how to draw border according to number of lines in each row and prevent data flowing into new page

我正在使用 PHP/TCPDF 中的 Multicell() 函数创建一个 pdf table。我面临的两个问题是

  1. table (3x3) 的每一行都有不同数量的数据行,因此如何根据各行的最大行数为每一行绘制水平线。
  2. 如何计算分页符?例如,如果第 2 行数据流入下一页。如何在新页面中 re-write 完整的 table,或者在新页面中以 table 的 sub-headings 开始第 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); 

Update,看起来这个答案并没有解决它特定于 1 行的所有行的溢出问题。任何建议或改进都是 accepted.On 另一个注意事项,分页符完美无缺。