2009-09-14 23 views
13

Tôi đang sử dụng cổng java của fpdf. Tôi đang gặp phải lỗi fowwlowing.sự cố ngắt dòng với MultiCell trong FPDF

1) .Khi tôi gọi multicell 2 lần mỗi lần văn bản được in trên một dòng mới.

MultiCell(0, 1, "abcd", currentBorders, Alignment.LEFT, false); //prints on one line 
MultiCell(0, 1, "efg", currentBorders, Alignment.LEFT, false); //prints on next line 

Tôi muốn không có ngắt dòng sau khi gọi đến đa điểm. Tôi làm nó như thế nào?

2) Nếu tôi làm điều sau thì một số phần của chuỗi của tôi được in trên một dòng và một số tiếp theo.

MultiCell(getStringWidth(myString), 1, myStringcurrentBorders, Alignment.LEFT, false); 

3) Nếu tôi thực hiện điều sau thì có nhiều dòng trống sau dòng được in myString. Nó hoạt động chính xác nếu tôi sử dụng một tham số 1 giây thứ hai

MultiCell(0, myFontSize, "123456", currentBorders, Alignment.LEFT, false); 

Sự cố là gì?

+0

tại sao không có ai trả lời? Tôi cần nó xấu – user156073

Trả lời

15

Tôi sẽ nhận được vị trí Y hiện tại trước khi viết MultiCell và sau đó di chuyển "con trỏ" trở lại vị trí đó Y sau khi thế hệ MultiCell. Như thế này:

$current_y = $pdf->GetY(); 
$current_x = $pdf->GetX(); 

$cell_width = 50; 
MultiCell($cell_width, 1, "abcd", currentBorders, Alignment.LEFT, false); 

$pdf->SetXY($current_x + $cell_width, $current_y); 

$current_x = $pdf->GetX(); 
MultiCell($cell_width, 1, "abcd", currentBorders, Alignment.LEFT, false); 

Điều gì đó tương tự.

1

Tôi đã sửa đổi phương thức MultiCell, nó hoạt động như câu trả lời ở trên và bạn có thể sử dụng phương pháp theo cách tương tự như phương thức Ô.

function MultiCell($w, $h, $txt, $border=0, $ln=0, $align='J', $fill=false) 
{ 
    // Custom Tomaz Ahlin 
    if($ln == 0) { 
     $current_y = $this->GetY(); 
     $current_x = $this->GetX(); 
    } 

    // Output text with automatic or explicit line breaks 
    $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--; 
    $b = 0; 
    if($border) 
    { 
     if($border==1) 
     { 
      $border = 'LTRB'; 
      $b = 'LRT'; 
      $b2 = 'LR'; 
     } 
     else 
     { 
      $b2 = ''; 
      if(strpos($border,'L')!==false) 
       $b2 .= 'L'; 
      if(strpos($border,'R')!==false) 
       $b2 .= 'R'; 
      $b = (strpos($border,'T')!==false) ? $b2.'T' : $b2; 
     } 
    } 
    $sep = -1; 
    $i = 0; 
    $j = 0; 
    $l = 0; 
    $ns = 0; 
    $nl = 1; 
    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'); 
      } 
      $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill); 
      $i++; 
      $sep = -1; 
      $j = $i; 
      $l = 0; 
      $ns = 0; 
      $nl++; 
      if($border && $nl==2) 
       $b = $b2; 
      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'); 
       } 
       $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill); 
      } 
      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)); 
       } 
       $this->Cell($w,$h,substr($s,$j,$sep-$j),$b,2,$align,$fill); 
       $i = $sep+1; 
      } 
      $sep = -1; 
      $j = $i; 
      $l = 0; 
      $ns = 0; 
      $nl++; 
      if($border && $nl==2) 
       $b = $b2; 
     } 
     else 
      $i++; 
    } 
    // Last chunk 
    if($this->ws>0) 
    { 
     $this->ws = 0; 
     $this->_out('0 Tw'); 
    } 
    if($border && strpos($border,'B')!==false) 
     $b .= 'B'; 
    $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill); 
    $this->x = $this->lMargin; 

    // Custom Tomaz Ahlin 
    if($ln == 0) { 
     $this->SetXY($current_x + $w, $current_y); 
    } 
} 
+2

Chỉnh sửa trực tiếp phương pháp là đáng sợ. Điều gì xảy ra nếu FPDF cập nhật một cách quyết liệt làm mất hiệu lực các sửa đổi của bạn? Tôi đã từng thấy kiểu kịch bản đó trước đây. Tôi nghĩ tốt hơn là nên tạo một phương pháp mới bao quanh 'MultiCell' thay vì trực tiếp sửa đổi nó. –

3

Tôi đã tạo phương thức mới có tên là MultiAlignCell. Phải có cùng thông số như MultiCell nhưng với trường được thêm vào ln từ Cell. Bạn có thể thêm nó vào lớp học mở rộng FPDF của bạn.

/** 
* MultiCell with alignment as in Cell. 
* @param float $w 
* @param float $h 
* @param string $text 
* @param mixed $border 
* @param int $ln 
* @param string $align 
* @param boolean $fill 
*/ 
private function MultiAlignCell($w,$h,$text,$border=0,$ln=0,$align='L',$fill=false) 
{ 
    // Store reset values for (x,y) positions 
    $x = $this->GetX() + $w; 
    $y = $this->GetY(); 

    // Make a call to FPDF's MultiCell 
    $this->MultiCell($w,$h,$text,$border,$align,$fill); 

    // Reset the line position to the right, like in Cell 
    if($ln==0) 
    { 
     $this->SetXY($x,$y); 
    } 
} 
+0

Tại sao bạn khai báo phương thức là 'riêng tư'? – Andrea