2017-08-31 3 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のときにページを追加し、残りの$データで同じ関数をもう一度呼び出します –

関連する問題