动态fpdf表创建仅适用于第一页

时间:2017-08-31 18:50:38

标签: pdf fpdf

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

我的代码是这样的:

$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 个答案:

答案 0 :(得分:1)

我认为这是因为使用了CellMultiCell。有时你会有一个高度超过页面的单元格,而AutoPageBreak只会将这些数据抛到下一页。

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