1

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...

bene-we
  • 761
  • 11
  • 23

1 Answers1

3

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

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

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.

Call BreakCel(...) with the same parameters as 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;
}
Michel
  • 4,076
  • 4
  • 34
  • 52