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;
}
}
這就是我最終做的,它完美的作品。 基本上它現在只是在$ y> 270時添加一個頁面,並再次與剩餘的$ data一起調用相同的函數 –