FPDF Add-on Write-Html-Tables 可能在表格单元格中出现换行错误。我用\n 和<br> 检查了它——同样的错误。
推荐解决方案
如果您使用 HTML 代码为使用 FPDF 库生成的 PDF 文档构建表格,那么我推荐FPDF easyTable project (FPDF Add-on)。
您必须从该项目下载easyTable.php、formatedstring.php、exfpdf.php,并将此文件保存在 FPDF 文件夹中。不要忘记删除 PHP 文件末尾 ?> 之后的所有符号。
示例:
<?php
include 'fpdf.php';
include 'exfpdf.php';
include 'easyTable.php';
$pdf = new exFPDF();
$pdf->AddPage();
$pdf->SetFont('helvetica','',10);
$table = new easyTable($pdf, '%{70,30}', 'border:1');
$table->easyCell('If the content of a cell is too long, I can make a line break inside the cell and the text does not overlap the next cell.', 'colspan:2; bgcolor:#b3ccff');
$table->printRow();
$table->easyCell('If the content of a cell is too long, I can make a line break inside the cell and the text does not overlap the next cell.');
$table->easyCell('Some values 1');
$table->printRow();
//the next line is double quoted because new line(\n) should be interpreted
$table->easyCell("If the content of a cell is too long, I can make a line break inside the cell and\n\nthe text does not overlap the next cell.");
$table->easyCell('Some values 2');
$table->printRow();
$table->endTable(5);
$pdf->Output();
?>
输出示例作为屏幕截图的一部分:
如果您想手动换行,那么您应该在 \n 周围使用双引号 " 而不是单引号 '。
来自docs:
指定字符串的最简单方法是将其括在单引号中
(字符')。
要指定文字单引号,请使用反斜杠 (\) 对其进行转义。
要指定文字反斜杠,请将其加倍 (\\)。所有其他实例
of 反斜杠将被视为文字反斜杠:这意味着
您可能习惯的其他转义序列,例如\r 或\n,
将按指定的字面输出,而不是有任何特殊的
意思。
如果字符串用双引号 (") 括起来,PHP 将解释 [转义序列,例如 \n]。