2017-08-31 57 views
0

我第一次使用fpdf,我设法创建一个函数,使pdf中的表动态,并根据单元格中的文本调整表行高度。它在第一页上很有魅力,但在所有其他页面上它看起来很奇怪,杂散的单元格和文本在四周浮动(我如何将文件附加到此?)。动态fpdf表创建只适用于第一页

我的代码是这样:

$pdf=new PDF(); 
$pdf->AddPage('P', '', 'A4'); 
$pdf->SetLineWidth(0,2); 
$pdf->SetFont('Arial','B',14); 
$pdf->Cell(75,25,$pdf->Image($imgurl, $pdf->GetX(100), $pdf->GetY(), 40),0,0); 
$pdf->Cell(250,25,$kw[555],0,1); 
//this is the function that makes the table 
$pdf->CreateDynamicTable($array,$finalData); 
$pdf->Output(); 


class PDF extends FPDF{ 
public $padding = 10; 
function CreateDynamicTable($array,$data){ 
    $this->SetFillColor(191, 191, 191); 
    $this->SetFont('Arial', 'B', 9); 
    foreach($array AS $name => $confs){ 
     $this->Cell($confs['width'],10,$confs['header'],1,0,'C', true); 
    } 
    $this->Ln(); 
    $x0=$x = $this->GetX(); 
    $y = $this->GetY(); 
    foreach($data as $rows=>$key){ 
     $yH = $this->getTableRowHeight($key,$array); 
     foreach($array AS $name => $confs){ 
      if(isset($key[$name])){ 
       $this->SetXY($x, $y); 
       $this->Cell($confs['width'], $yH, "", 'LRB',0,'',false); 
       $this->SetXY($x, $y); 
       $this->MultiCell($confs['width'],6,$key[$name],0,'C'); 
       $x =$x+$confs['width']; 
      } 
     } 
     $y=$y+$yH; //move to next row 
     $x=$x0; //start from first column 
    } 
} 
public function getTableRowHeight($key,$array){ 
    $yH=5; //height of the row 
    $temp = array(); 
    foreach($array AS $name => $confs){ 
     if(isset($key[$name])){ 
      $str_w = $this->GetStringWidth($key[$name]); 
      $temp[] = (int) $str_w/$confs['width']; 
     } 
    } 
    $m_str_w = max($temp); 
    if($m_str_w > 1){ 
     $yH *= $m_str_w; 

    } 
    $yH += $this->padding; 
    return $yH; 
} 
} 

回答

1

我想,这是因为使用的CellMultiCell的。有时你会有一个单元格,其高度将超过页面,并且AutoPageBreak只会将该数据扔到下一页。

尝试$pdf -> SetAutoPageBreak(false);并在知道您位于页面底部时使用AddPage()。要获得适当的高度(如果单元格),您需要先获取行中所有单元格的最大高度,然后确定是要在当前页面还是下一个页面上输出。

+0

这就是我最终做的,它完美的作品。 基本上它现在只是在$ y> 270时添加一个页面,并再次与剩余的$ data一起调用相同的函数 –

相关问题