我已经阅读了大量有关如何将文本换行到FPDF中下一行的问题,但是当文本大于单元格时是否可以剪切文本?就像CSS中的text-overflow: ellipsis
一样......
答案 0 :(得分:1)
我有同样的问题。怀疑如果你仍然需要答案,但对于寻找答案的人来说:
您必须添加自己的功能,因为FPDF
没有提供解决方案。
我已复制MultiCell
功能并将其重命名为BreakCell
。它在创建第一个Cell
后停止。单元格中的字符串缩短3个字符,并添加3个点。在下面的代码//***
中,我已经解释了我的所作所为。
使用与BreakCel(...)
MultiCel(...)
function BreakCell($w, $h, $txt, $border=0, $align='J', $fill=false)
{
// Output text with automatic or explicit line breaks
if(!isset($this->CurrentFont))
$this->Error('No font has been set');
$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--;
//*** Since we only create one Cell, there is no need to remove bottom border
//*** code removed
$b = 0;
if($border)$b=$border;
$sep = -1;
$i = 0;
$j = 0;
$l = 0;
$ns = 0;
$nl = 1;
// *** stop as one cell is set
// *** prevents last line if there allready is a cell
$stop=false;
while($i<$nb && $stop===false)
{
// 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);
// *** cell is set, so we stop
// *** rest of code removed
$stop=true;
break;
}
// *** do not auto-break after space
// *** code removed
$l += $cw[$c];
if($l>$wmax)
{
// *** sep always is -1 if there is no "\n"
// *** if/else removed
// Automatic line break
if($sep==-1)
{
if($i==$j)
$i++;
if($this->ws>0)
{
$this->ws = 0;
$this->_out('0 Tw');
}
//*** changed lenght $i-$j in $i-$j-3 to make space for 3 dots
//*** added 3 dots
$this->Cell($w,$h,substr($s,$j,$i-$j-3).'...',$b,2,$align,$fill);
// *** cell is set, so we stop
// *** rest of code removed
$stop=true;
break;
// *** cell is set, so we stop
}
}
else
$i++;
}
// Last chunk
// *** only if no cell is set
if($stop===false){
if($this->ws>0)
{
$this->ws = 0;
$this->_out('0 Tw');
}
//*** bottom border allready set
//*** code removed
$this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
}
$this->x = $this->lMargin;
}