【问题标题】:line break problem with MultiCell in FPDFFPDF中MultiCell的换行问题
【发布时间】:2010-11-28 00:42:55
【问题描述】:

我正在使用 fpdf 的 java 端口。我遇到了以下错误。

1).当我每次在新行上打印文本时调用 multicell 2 次。

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

我希望在调用 multicell 后没有换行符。我该怎么做?

2)如果我执行以下操作,那么我的字符串的某些部分会打印在一行上,而另一些会打印在下一行。

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

3) 如果我执行以下操作,那么在打印 myString 的行之后会有许多空白行。如果我使用一个 1 ans 第二个参数,它可以正常工作

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

有什么问题?

【问题讨论】:

    标签: php pdf-generation fpdf


    【解决方案1】:

    我会在编写MultiCell 之前获得当前的Y 位置,然后在MultiCell 生成之后将“光标”移回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);
    

    类似的东西。

    【讨论】:

      【解决方案2】:

      我创建了一个名为MultiAlignCell 的新方法。它采用与MultiCell 相同的参数,但添加了来自Cell 的ln 字段。您可以将其添加到您的扩展 FPDF 类中。

      /**
      * 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);
          }
      }
      

      【讨论】:

      • 为什么将方法声明为private?
      【解决方案3】:

      我已经修改了MultiCell方法,它的工作原理和上面的答案一样,你可以像使用Cell方法一样使用该方法。

      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);
          }
      }
      

      【讨论】:

      • 直接修改一个方法是可怕的。如果 FPDF 以剧烈的方式更新导致您的修改无效怎么办?我以前见过这样的场景。我觉得做一个新方法包裹MultiCell而不是直接修改它会更好。
      【解决方案4】:

      在我的情况下,我没有创建任何方法,我只是设置了 X 和 Y,然后在我重置的行尾。它也很完美。

              $pdf->SetFont('times', 'B', 10);
              $x = $pdf->GetX();
              $y = $pdf->GetY();
              $pdf->MultiCell($etiquetas_largura, $etiquetas_altura, $campos[$linha]['B1COD'], 0, 'L', 0, 0, $x, $y, true, 0, false, true, 0);
              $y = $y + 5;
              $pdf->SetFont('times', '', 10);
              $pdf->MultiCell($etiquetas_largura, $etiquetas_altura, $campos[$linha]['B1DESC'], 0, 'L', 0, 0, $x, $y, true, 0, false, true, 0);
              // resete x y
              $pdf->SetXY($x + $etiquetas_largura, $y - 5);
      

      【讨论】:

        【解决方案5】:

        @Muhammad Abdul Rahim 和@tomazahlin 提供了很好的方法。问题他们只解决了单个单元格中的换行问题。它们与同一行中其他单元格的主题单元格的高度不匹配。如果您正在处理动态表,则使用 GetY() 会变得复杂。我找到的最简单的解决方案是识别可能有溢出文本的列并将其用作参考。

        $l=strlen($string_of_reference_cell);
        $h = ceil($l/$cell_width*1.5)*preferred_normal_height;//1.5 is a loading for allowance`depending on font size.
        
        $pdf->cell(20,$h,$string,1,0);
        $pdf->MultiAlignCell(50,5,$string_of_reference_cell,1,0);// 5 is the preferred normal height
        $pdf->Cell(23,$h,$string,1,1);
        

        生成pdf时,如果MultiAlignCell的字符串长于单元格宽度,则生成换行符。结果高度是两倍(5 x 2 = 10)。十的高度分配给 $h。因此,其他单元格的高度也为 $h,整行的高度一致。

        【讨论】:

          猜你喜欢
          • 2013-04-15
          • 2021-11-09
          • 2017-10-15
          • 2013-12-29
          • 1970-01-01
          • 2017-04-07
          • 2012-11-13
          • 1970-01-01
          • 2013-03-01
          相关资源
          最近更新 更多