php 在 PDF 中插入换行符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/861809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Inserting line breaks into PDF
提问by thomasrutter
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:
我正在使用 PHP 即时生成一些 PDF 文件。我的问题是我需要在将插入 PDF 文件的文本的某些部分插入换行符。就像是:
$pdf->InsertText('Line one\n\nLine two');
So it prints:
所以它打印:
Line one
Line two
一号线
二号线
I know \ndoesn't work on PDF, but do you guys know any character or something that represents a line break on these files?
我知道\n在 PDF 上不起作用,但是你们知道这些文件中代表换行符的任何字符或东西吗?
回答by thomasrutter
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.
如果您使用 fpdf,为了能够使用换行符,您将需要使用多行文本单元格,如此处所述。
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.
这里,2 是多行文本框的高度。我不知道测量的单位是什么,或者您是否可以将其设置为 0 并忽略它。如果一开始它不起作用,也许可以尝试大量使用。
回答by David Snabel-Caunt
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 库,但通常如果您希望 \n 被解释为换行符,则必须在 PHP 中使用双引号,例如
$pdf->InsertText("Line one\n\nLine two");
回答by MCR
I changed '\n'for chr(10)and it worked:
我更改'\n'为chr(10)并且它有效:
$pdf->MultiCell(0,5,utf8_decode($variable1 . chr(10) . $variable2),1);
回答by Prasad Rajapaksha
I have simply replaced the "\n" with "<br>" tag. Worked fine. It seems TCPDF render the text as HTML
我只是将“ \n”替换为“ <br>”标签。工作得很好。似乎 TCPDF 将文本呈现为 HTML
$strText = str_replace("\n","<br>",$strText);
$pdf->MultiCell($width, $height,$strText, 0, 'J', 0, 1, '', '', true, null, true);
回答by Nicolas
You state that
你说
2 is the height of the multi-line text box
2是多行文本框的高度
No it's not. 2 is the distance between lines of text.
不,这不对。2 是文本行之间的距离。
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.
我不认为有一种真正的方法来计算实际结果文本框的高度,除非您使用GetY()然后减去您的SetXY()语句中使用的原始 Y 值以将 Multicell 放在首位。
回答by None
Another option is to use TCPDF::Ln(). It adds a line to the PDF with the option to set the height.
另一种选择是使用TCPDF::Ln(). 它向 PDF 添加了一条线,并带有设置高度的选项。
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:
如果换行符已经在您的内容中,那么MultiCell()可能是要走的路,正如其他人所提到的,但我发现我喜欢使用:
$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 \nreads like a newline character in text or <br>in HTML.
这让我感到困惑Cell()并MultiCell()接受了不同的论点,所以我倾向于坚持Cell()只使用。此外,它读起来像 PDF 的\n换行符,就像文本或<br>HTML 中的换行符一样。
回答by Lingasamy Sakthivel
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-8and just tried the above one. It works fine with MultiCellbut not with Cell.
我想\n,<br/>和chr(10),但毫无效果。然后我意识到它是utf-8并尝试了上面的一个。它适用于MultiCell但不适用于Cell.
回答by dummy
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 MultiCellto read html on a basic level.
您可以将 配置为MultiCell在基本级别上读取 html。
回答by David Alves
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.
因此,这样,您可以在任何位置拥有任意数量的 \n,如果您从数据库中动态获取此文本,它将正确地断行。
回答by zeus2026
$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
在每一列中,在你设置 X 位置之前先指示 Y 位置,所以它变成了这样
Column 1
第 1 栏
$pdf->SetY($Y_Fields_Name_position);
$pdf->SetX(#);
$pdf->MultiCell($height,$width,"Line1 \nLine2 \nLine3",1,'C',1);
Column 2
第 2 栏
$pdf->SetY($Y_Fields_Name_position);
$pdf->SetX(#);
$pdf->MultiCell($height,$width,"Line1 \nLine2 \nLine3",1,'C',1);

