2012-01-07 78 views
1

我正在使用fpdf生成報表,但其中一個字段需要多單元,因爲內容可能只有幾行。下面是代碼,現在發生的情況是,多單元將只有幾行,但剩下的只有一個標準尺寸,多單元之後的內容將在下一行。因此,如何克服這一切問題fpdf多單元格控件

$row_height = 5; // set the default    
$column_width = 15; 

$number_of_lines = ceil($pdf->GetStringWidth($eventMessage)/($column_width - 1)); 
$cell_height = 5; 
$height_of_cell = ceil($number_of_lines * $cell_height); 
if ($cell_height > $row_height) { 
$row_height = $cell_height; 
} 

$pdf->Cell(10, $row_height,$count, 1, 0, 'L', true); 
$pdf->Cell(15, $row_height,$row['latitude'], 1, 0, 'L', true); 
$pdf->Cell(16, $row_height,$row['longitude'], 1, 0, 'L', true); 
$pdf->Cell(25, $row_height,$row['dateTimer'], 1, 0, 'L', true); 
$pdf->Cell(25, $row_height,$row['insertDateTime'], 1, 0, 'L', true); 

$pdf->MultiCell(15, $row_height,$eventMessage, 1, 'J',true); 
$pdf->Cell(15, $row_height,$eventSource, 1, 0, 'L', true); 
$pdf->Cell(15, $row_height,$eventLocation, 1, 0, 'L', true); 
$pdf->Cell(18, $row_height,$row['stat'], 1, 0, 'L', true); 
+0

大哥,我已經下降同樣的問題,但沒有開發商來解決這個問題,我認爲。所以我們受苦了。 – 2013-05-13 10:32:23

回答

0
class pdf extends FPDF { 
    function GetMultiCellHeight($w, $h, $txt, $border=null, $align='J') { 
    // Calculate MultiCell with automatic or explicit line breaks height 
    // $border is un-used, but I kept it in the parameters to keep the call 
    // to this function consistent with MultiCell() 
    $cw = &$this->CurrentFont['cw']; 
    if($w==0) 
     $w = $this->w-$this->rMargin-$this->x; 
    $wmax = ($w-2*$this->cMargin)*1000/$this->FontSize; 
    $s = str_replace("\r",'',$txt); 
    $nb = strlen($s); 
    if($nb>0 && $s[$nb-1]=="\n") 
     $nb--; 
    $sep = -1; 
    $i = 0; 
    $j = 0; 
    $l = 0; 
    $ns = 0; 
    $height = 0; 
    while($i<$nb) 
    { 
     // Get next character 
     $c = $s[$i]; 
     if($c=="\n") 
     { 
      // Explicit line break 
      if($this->ws>0) 
      { 
       $this->ws = 0; 
       $this->_out('0 Tw'); 
      } 
      //Increase Height 
      $height += $h; 
      $i++; 
      $sep = -1; 
      $j = $i; 
      $l = 0; 
      $ns = 0; 
      continue; 
     } 
     if($c==' ') 
     { 
      $sep = $i; 
      $ls = $l; 
      $ns++; 
     } 
     $l += $cw[$c]; 
     if($l>$wmax) 
     { 
      // Automatic line break 
      if($sep==-1) 
      { 
       if($i==$j) 
        $i++; 
       if($this->ws>0) 
       { 
        $this->ws = 0; 
        $this->_out('0 Tw'); 
       } 
       //Increase Height 
       $height += $h; 
      } 
      else 
      { 
       if($align=='J') 
       { 
        $this->ws = ($ns>1) ? ($wmax-$ls)/1000*$this->FontSize/($ns-1) : 0; 
        $this->_out(sprintf('%.3F Tw',$this->ws*$this->k)); 
       } 
       //Increase Height 
       $height += $h; 
       $i = $sep+1; 
      } 
      $sep = -1; 
      $j = $i; 
      $l = 0; 
      $ns = 0; 
     } 
     else 
      $i++; 
    } 
    // Last chunk 
    if($this->ws>0) 
    { 
     $this->ws = 0; 
     $this->_out('0 Tw'); 
    } 
    //Increase Height 
    $height += $h; 

    return $height; 
    } 
} 

所以

$pdf = new pdf(); 
$pdf->addPage(); 
$pdf->MultiCell(50, 4, 'Bla bla bla'); 
$pdf->ln(GetMultiCellHeight(50, 4, 'Bla bla bla')); 
$pdf->Output();