[php] Inserting line breaks into PDF

I'm generating some PDF file on the fly using PHP. My problem is I need to insert line breaks in some part of the text that will be inserted in the PDF file. Something like:

$pdf->InsertText('Line one\n\nLine two');

So it prints:

Line one

Line two

I know \n doesn't work on PDF, but do you guys know any character or something that represents a line break on these files?

This question is related to php pdf fpdf carriage-return

The answer is


Another solutions (works with TCPDF)

Use HEREDOC for a long string. Put HERDOC for a CONST for example (define different languages)

$_prepare_const_EN = <<<EOT
this is a long string
and new line as well ...
EOT;

$define('STR_EN', $_prepare_const_EN);

$pdf->InsertText(STR_EN);

works for me wery well....


$pdf->SetY($Y_Fields_Name_position);
$pdf->SetX(#);
$pdf->MultiCell($height,$width,"Line1 \nLine2 \nLine3",1,'C',1);

In every Column, before you set the X Position indicate first the Y position, so it became like this

Column 1

$pdf->SetY($Y_Fields_Name_position);
$pdf->SetX(#);
$pdf->MultiCell($height,$width,"Line1 \nLine2 \nLine3",1,'C',1);

Column 2

$pdf->SetY($Y_Fields_Name_position);
$pdf->SetX(#);
$pdf->MultiCell($height,$width,"Line1 \nLine2 \nLine3",1,'C',1);

Maybe it´s too late but I solved this issue in a very simple way, I am using the Multicell option and the text come from a form, if I use an input field to get the text I can´t insert line breaks in any way, but if use a textarea field, the line breaks in the text area are line breaks in the multicell ... and that´s it, it works even if I use utf8_encode($text) option to preserve accents


Your code reads

$pdf->InsertText('Line one\n\nLine two');

I don't know about the PDF library you're using but normally if you want \n to be interpreted as a line break you must use double quotes in PHP, e.g.

$pdf->InsertText("Line one\n\nLine two");

Or just try this after each text passage for a new line.

$pdf->Write(0, ' ', '*', 0, 'C', TRUE, 0, false, false, 0) ;


I have simply replaced the "\n" with "<br>" tag. Worked fine. It seems TCPDF render the text as HTML

$strText = str_replace("\n","<br>",$strText);
$pdf->MultiCell($width, $height,$strText, 0, 'J', 0, 1, '', '', true, null, true);

Another option is to use TCPDF::Ln(). It adds a line to the PDF with the option to set the height.

If the newlines are within your content already then MultiCell() is probably the way to go, as others have mentioned, but I find I like using:

$pdf->Cell(0, 0, 'Line 1', 0, 0, 'C');
$pdf->Ln();
$pdf->Cell(0, 0, 'Line 2', 0, 0, 'C');

It confuses me that Cell() and MultiCell() take in different arguments so I tend to stick to using Cell() only. Also it reads like a newline character for the PDF the same as \n reads like a newline character in text or <br> in HTML.


If you are using fpdf, in order to be able to use line breaks you will need to use a multi-line text cell as described here.

If you use this, then line breaks in your text should be interpreted and converted correctly.

Just a quick example:

$pdf->Multicell(0,2,"This is a multi-line text string\nNew line\nNew line"); 

Here, 2 is the height of the multi-line text box. I don't know what units that's measured in or if you can just set it to 0 and ignore it. Perhaps try it with a large number if at first it doesn't work.


I changed '\n' for chr(10) and it worked:

$pdf->MultiCell(0,5,utf8_decode($variable1 . chr(10) . $variable2),1);

You state that

2 is the height of the multi-line text box

No it's not. 2 is the distance between lines of text.

I don't think there is a real way for calculating the height of the actual resulting text box, unless you use GetY() and then subtract the original Y value used in your SetXY() statement for placing the Multicell in the first place.


The solution I've found was:

$text = 'Line one\n\nLine two');
$text = explode('\n', $text);

foreach ($text as $txt){
    $pdf->Write($txt);
    $pdf->Ln();
}

So this way, you may have any number of \n in any position, if you're getting this text dinamically from the database, it will break lines correctrly.


MultiCell($w, $h, 'text<br />', $border=0, $align='L', $fill=1, $ln=0,
    $x='', $y='', $reseth=true, $reseth=0, $ishtml=true, $autopadding=true,
    $maxh=0);

You can configure the MultiCell to read html on a basic level.


After having so many nightmares, I found a solution.

utf8_decode(chr(10))

I tried \n, <br/> and chr(10) but nothing worked. Then I realized that it was utf-8 and just tried the above one. It works fine with MultiCell but not with Cell.


Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to pdf

ImageMagick security policy 'PDF' blocking conversion How to extract table as text from the PDF using Python? Extract a page from a pdf as a jpeg How can I read pdf in python? Generating a PDF file from React Components Extract Data from PDF and Add to Worksheet How to extract text from a PDF file? How to download PDF automatically using js? Download pdf file using jquery ajax Generate PDF from HTML using pdfMake in Angularjs

Examples related to fpdf

Setting paper size in FPDF FPDF error: Some data has already been output, can't send PDF FPDF utf-8 encoding (HOW-TO) Make text wrap in a cell with FPDF? Inserting an image with PHP and FPDF Inserting line breaks into PDF

Examples related to carriage-return

Create Carriage Return in PHP String? What is the difference between a "line feed" and a "carriage return"? How can I insert new line/carriage returns into an element.textContent? What are the differences between char literals '\n' and '\r' in Java? What's the Use of '\r' escape sequence? Carriage return and Line feed... Are both required in C#? Find and replace - Add carriage return OR Newline In C#, what's the difference between \n and \r\n? See line breaks and carriage returns in editor What are carriage return, linefeed, and form feed?