简体   繁体   English

在 PDF 中插入换行符

[英]Inserting line breaks into PDF

I'm generating some PDF file on the fly using PHP.我正在使用 PHP 即时生成一些 PDF 文件。 My problem is I need to insert line breaks in some part of the text that will be inserted in the PDF file.我的问题是我需要在将插入 PDF 文件中的文本的某些部分插入换行符。 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?我知道\n在 PDF 上不起作用,但是你们知道任何字符或代表这些文件的换行符的东西吗?

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.这里,2 是多行文本框的高度。 I don't know what units that's measured in or if you can just set it to 0 and ignore it.我不知道测量的单位是什么,或者您是否可以将其设置为 0 并忽略它。 Perhaps try it with a large number if at first it doesn't work.如果一开始它不起作用,也许可以尝试大量使用。

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, eg我不知道您正在使用的 PDF 库,但通常如果您希望 \\n 被解释为换行符,则必须在 PHP 中使用双引号,例如

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

我为chr(10)更改了'\\n'并且它起作用了:

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

Another option is to use TCPDF::Ln() .另一种选择是使用TCPDF::Ln() It adds a line to the PDF with the option to set the height.它向 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. Cell()MultiCell()采用不同的参数让我感到困惑,所以我倾向于只使用Cell() 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.此外,它读起来像 PDF 的换行符,就像\\n读作文本中的换行符或 HTML 中的<br>

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 值,以便将SetXY()放在首位。

I have simply replaced the " \\n " with " <br> " tag.我只是用“ <br> ”标签替换了“ \\n ”。 Worked fine.工作得很好。 It seems TCPDF render the text as HTML似乎 TCPDF 将文本呈现为 HTML

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

After having so many nightmares, I found a solution.在做了这么多噩梦之后,我找到了解决办法。

utf8_decode(chr(10))

I tried \\n , <br/> and chr(10) but nothing worked.我尝试了\\n<br/>chr(10)但没有任何效果。 Then I realized that it was utf-8 and just tried the above one.然后我意识到它是utf-8并尝试了上面的一个。 It works fine with MultiCell but not with Cell .它适用于MultiCell但不适用于Cell

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.您可以将MultiCell配置为在基本级别上读取 html。

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,如果您从数据库中动态获取此文本,它将正确地断行。

$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);

也许为时已晚,但我以一种非常简单的方式解决了这个问题,我使用的是多单元格选项,文本来自表单,如果我使用输入字段来获取文本,我无法在任何地方插入换行符方式,但如果使用 textarea 字段,文本区域中的换行符是多单元格中的换行符......就是这样,即使我使用utf8_encode($text)选项来保留重音,它也能工作

或者只是在每个文本段落后尝试这个新行。

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

Another solutions (works with TCPDF)另一个解决方案(适用于 TCPDF)

Use HEREDOC for a long string.对长字符串使用 HEREDOC。 Put HERDOC for a CONST for example (define different languages)例如,将 HERDOC 用于 CONST(定义不同的语言)

$_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....非常适合我......

pars_text.append(content[0])
pdf.multi_cell(w=190, txt = content[0])
pdf.ln();  # this

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM