是否可以在FPDF库中的单元格上剪切文本? [英] Is it possible to cut text on cell in in FPDF library?

查看:81
本文介绍了是否可以在FPDF库中的单元格上剪切文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了很多有关如何将文本包装到FPDF中的下一行的常见问题解答,但是当文本比单元格大时,是否可以剪切文本?就像CSS中的 text-overflow:省略号 ...

i've read plenty of q&a's on how to wrap text to next line in FPDF, but is it possible to cut text when the text is larger then the cell? Like text-overflow: ellipsis in CSS somehow...

推荐答案

我有同样的问题。怀疑是否仍然需要答案,但是对于任何寻求答案的人:

I had the same question. Doubt if you still need an answer, but for anyone looking for an answer:

您必须添加自己的函数,因为 FPDF 没有提供解决方案。

You have to add your own function because FPDF doesn't provide a solution.

我已复制 MultiCell 函数并将其重命名为 BreakCell 。创建第一个 Cell 后,它就会停止。单元格中的字符串缩短了3个字符,并添加了3个点。在下面的代码中,使用 // *** 解释了我所做的事情。

I've copied the MultiCell function and renamed it as BreakCell. It just stops after the first Cell is created. The string in the Cell is shortened by 3 characters and 3 dots are added. In the code below with //*** I've explained what I did.

调用 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;
}

这篇关于是否可以在FPDF库中的单元格上剪切文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆