2010-05-14 61 views
1

我正在使用fPDF生成PDF。fPDF:如何在多单元中删除/删除合理的文本?

我需要删除MultiCell中的长文本。该文本是正确的左和右,这可能是问题的根源。

这里是我的代码:

//get the starting x and y of the cell to strikeout 
$strikeout_y_start = $pdf->GetY(); 
$strikeout_x = $pdf->getX(); 
$strikeText = "Some text with no New Lines (\n), which is wrapped automaticly, cause it is very very very very very very very very very very long long long long long long long long long long long long long long long long long long" 
//draw the text 
$pdf->MultiCell(180, 4, $strikeText); 
//get the y end of cell 
$strikeout_y_end = $pdf->GetY(); 
$strikeout_y = $strikeout_y_start+2; 
$strikeCount = 0; 
for ($strikeout_y; $strikeout_y < $strikeout_y_end - 4; $strikeout_y+=4) { 
    $strikeCount++; 
    //strike out the full width of all lines but last one - works OK 
    $pdf->Line($strikeout_x, $strikeout_y, $strikeout_x + 180, $strikeout_y); 
} 

//this works, but gives incorrect results 
$width = $pdf->GetStringWidth($strikeText); 
$width = $width - $strikeCount*180; 
//the line below will strike out some text, but not all the letters of last line 
$pdf->line($strikeout_x, $strikeout_y, $strikeout_x+$width, $strikeout_y); 

的问题是,在多小区文本是合理的(和必须的),在以前的线路spacec更宽比GetStringWidth假设,所以GetStringWidth低估本文的全部宽度。

因此,最后一行被划出,比如说70%,并且结尾的一些字母没有被绘出。

任何想法如何计算多单元最后一行的宽度?

回答

3

我自己找到了解决方案。 对不起,提出不必要的问题。

这里是我做了什么:

class VeraPDF extends FPDF { 

    /** 
    * Returns width of the last line in a multicell 
    * useful for strike out/strike through 
    * 
    * 
    * @param string $s - the string measured 
    * @param int $lineWidth - with of the cell/line 
    * @return int 
    */ 
    function GetStringWidth_JustifiedLastLineWidth($s, $lineWidth) 
    { 
     //Get width of a string in the current font 
     $s=(string)$s; 
     $words = split(' ',$s); 
     $cw=&$this->CurrentFont['cw']; 
     $w=0; 
     $spaceWidth = $this->GetStringWidth(' '); 

     for($i=0, $wordsCount = count($words); $i<$wordsCount; $i++){ 
      // sum up all the words width, and add space withs between the words 
      $w += $this->GetStringWidth($words[$i]) + $spaceWidth; 
      if ($w > $lineWidth) { 
       //if current width is more than the line width, then the current word 
       //will be moved to next line, we need to count it again 
       $i--; 
      } 
      if ($w >= $lineWidth) { 
       //if the current width is equal or grater than the line width, 
       //we need to reset current width, and count the width of remaining text 
       $w = 0; 
      } 
     } 
     //at last, we have only the width of the text that remain on the last line! 
     return $w; 
    }  
} 

希望这会帮助别人:)

+0

抱歉回答自己。我有一个想法如何解决这个问题后,我只是在这里发布一个问题 – SWilk 2010-05-14 09:22:14

0

在以前的线路spacec比GetStringWidth宽 假设,所以 GetStringWidth低估了全 本文的宽度。

您是否试图计算空格并自己添加缺少的宽度。假设每个空间的宽度应该是5px,但是fpdf会将其描述为4px,也许您可​​以在每个空间中添加1px到最终的总宽度。

+0

感谢您的答案。实际上fpdf按当前字体中的定义计算空间宽度。但是,当文本被包装和合理化时,空间“增长”,以便在线条被填充到末尾时移动到下一行的单词所占据的空间。我刚刚发布了一个解决方案,它是基于你需要计算两次包装词的规则,并完全忽略完全填充的行。 – SWilk 2010-05-14 09:20:52