1

I have a 6 column quote form which I build using fpdf using the MultiCell feature. My issue is that in the 3rd column I have the description which has multiple lines of text that need a line break. You can see that replacing the html entity with \n only prints the character \n. I have tried it in quotes both single and double and tried CHR(10), it doesn't matter it treats the description as a string and outputs the string.

enter image description here

enter image description here The data for the column "description" is in html string:

$description = "205 - Base Cap 0.717 X 3.11<br>Cypress #2<br>Mill finish<br>Exact<br>Sand<br>Kerf<br>End dado 2.625" below dado.<br>Prepare for flush bolt HW-24EFB10B<br>10/10&emsp;80/3";

To create the row I use the following code:

$description = str_replace('<br>','\n',$description);
$pdf->Row(array($itemNumber,$stk_code,$description,$quantity,$unit_price,$lineNet));

Which calls the Row function to create a MultiCell row:

var $widths;
var $aligns;

function SetWidths($w)
{
    //Set the array of column widths
    $this->widths=$w;
}

function SetAligns($a)
{
    //Set the array of column alignments
    $this->aligns=$a;
}

function Row($data)
{
    //Calculate the height of the row
    $nb=0;
    for($i=0;$i<count($data);$i++)
        $nb=max($nb,$this->NbLines($this->widths[$i],$data[$i]));
    $h=15*$nb;
    //Issue a page break first if needed
    $this->CheckPageBreak($h);
    //Draw the cells of the row
    for($i=0;$i<count($data);$i++)
    {
        $w=$this->widths[$i];
        $a=isset($this->aligns[$i]) ? $this->aligns[$i] : 'L';
        //Save the current position
        $x=$this->GetX();
        $y=$this->GetY();
        //Draw the border
        $this->Rect($x,$y,$w,$h);
        //Print the text
        $this->MultiCell($w,15,$data[$i],0,$a);
        //Put the position to the right of the cell
        $this->SetXY($x+$w,$y);
    }
    //Go to the next line
    $this->Ln($h);
}

I have also tried WriteHTML but it returns the text less any html entities visible on screen.

user1946891
  • 955
  • 1
  • 11
  • 18
  • Put double quotes (") around your \n so it is `"\n"` instead of `'\n'` See [this question](http://stackoverflow.com/questions/2531969/print-newline-in-php-in-single-quotes) – Jo. May 16 '17 at 18:48
  • When I first looked at your answer I thought this is dumb as I tried double quotes, escaping the quotes and what I thought was every variation BUT I missed this simple one. I changed the line to read $description = str_replace('
    ',"\n",$description); And it works a big thank you to you.
    – user1946891 May 16 '17 at 19:21

1 Answers1

2

You should use double quotes " around your \n instead of single quotes '.

From the docs:

The simplest way to specify a string is to enclose it in single quotes (the character ').

To specify a literal single quote, escape it with a backslash (). To specify a literal backslash, double it (\). All other instances of backslash will be treated as a literal backslash: this means that the other escape sequences you might be used to, such as \r or \n, will be output literally as specified rather than having any special meaning.

If the string is enclosed in double-quotes ("), PHP will interpret [escape sequences such as \n]

Jo.
  • 778
  • 1
  • 12
  • 17