41

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?

k0pernikus
  • 60,309
  • 67
  • 216
  • 347
  • 1
    "some third party code" : it would have been easier for everybody if you said which lib exactly (luckily it looks like thomasrutter did recognize it) – siukurnin May 15 '09 at 07:42
  • InsertText() doesn't appear in the fpdf manual so I doubt that the third party code is fpdf. I think it might be best to remove the fpdf tag – danio May 19 '09 at 15:06
  • Returning to this question almost 3 years later I'm embarrassed by how vague it is. Sorry. I'm pretty sure I was referring to fpdf, or a variant of it I found somewhere. I abandoned my plan of writing to PDF anyway (or at least, I used it for a while, and it was good, then abandoned it). – thomasrutter Feb 16 '12 at 03:15

14 Answers14

50

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.

thomasrutter
  • 114,488
  • 30
  • 148
  • 167
  • 2
    sir, its not interpreting the \n as newline , its just printing it on the pdf as \n – sqlchild Jan 16 '12 at 11:53
  • 15
    @sqlchild - You must use the double quotes as mentioned below using the \n as mentioned here. It does work. Just tried myself. – JM4 Feb 14 '12 at 20:56
  • @thomasrutter, I'm using FPDF as well. Is there any way to read `\n` in `Cell()` function? I have problems with positioning adjacent `MultiCell()` when it prints on the 2nd page. I really need to read `\n` in `Cell()`. Please help. Thanks. – xjshiya Apr 18 '13 at 01:38
  • I don't think you can in Cell() – thomasrutter Apr 20 '13 at 13:57
20

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");
David Snabel-Caunt
  • 57,804
  • 13
  • 114
  • 132
15

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

$pdf->MultiCell(0,5,utf8_decode($variable1 . chr(10) . $variable2),1);
Dave
  • 3,073
  • 7
  • 20
  • 33
MCR
  • 151
  • 1
  • 2
  • 2
    This is because PHP takes text in single quotes (') literally, while text contained in double quotes (") is 'interpreted', so that means '\n' is a 2 character string, with a slash and an n character, while "\n" is a 1 character string, containing a single new-line character. ;) – enhzflep Jun 25 '16 at 17:34
7

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.

None
  • 5,491
  • 1
  • 40
  • 51
7

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.

cppanda
  • 1,235
  • 1
  • 15
  • 29
Nicolas
  • 71
  • 1
  • 1
  • 3
    Oh my gosh. I could not work out why the damn thing wasnt working correctly - All I wanted to do was print new lines! Then suddenly I read this. How annoying. It could be more specific in the documentation. For *w* it merely states `Height of cells.` That plural there is crucial to understand what It meant. I assumed it was just the height of the box that I wanted to print in. D'oh! – Chud37 Jun 20 '13 at 09:01
6

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);
Prasad Rajapaksha
  • 6,118
  • 10
  • 36
  • 52
  • Can you update your code with what parameters you are passing ? for instance $w =205, $h = 10. It may be easy to understand for expert users, I am not. Thanks for your help – user5249203 Mar 18 '16 at 15:54
6

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.

Linga
  • 10,379
  • 10
  • 52
  • 104
3

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.

David Dutra
  • 391
  • 7
  • 21
2
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.

kero
  • 10,647
  • 5
  • 41
  • 51
dummy
  • 21
  • 1
1
$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);
halfer
  • 19,824
  • 17
  • 99
  • 186
zeus2026
  • 131
  • 13
0

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

Felipe
  • 11
0

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

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

raf_qpsk
  • 101
  • 1
  • 3
0

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

Ja Loc
  • 43
  • 4
-2
pars_text.append(content[0])
pdf.multi_cell(w=190, txt = content[0])
pdf.ln();  # this
Just Me
  • 864
  • 2
  • 18
  • 28
  • 1
    This answer was reviewed in the [Low Quality Queue](https://stackoverflow.com/help/review-low-quality). Here are some guidelines for [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). Code only answers are **not considered good answers**, and are likely to be downvoted and/or deleted because they are **less useful** to a community of learners. It's only obvious to you. Explain what it does, and how it's different / **better** than existing answers. [From Review](https://stackoverflow.com/review/low-quality-posts/32529500) – Trenton McKinney Aug 23 '22 at 20:05