FPDF 附加组件 "Write-Html-Tables" 支持多单元格/单元格中的换行符

FPDF add-on "Write-Html-Tables" with multicell support / line breaks in cell

我使用 FPDF 插件 "write-html-tables"。该插件非常适合 HTML 表。

http://fpdf.de/downloads/add-ons/write-html-tables.html

但是我遇到了换行问题。如果一个单元格的内容太长,我无法在单元格内换行,文本会与下一个单元格重叠。

我试图从 "write-html-tables" 插件扩展代码,但它不起作用。有没有人告诉我如何扩展附加组件?

FPDF Add-on Write-Html-Tables 可能在 table 单元格中出现新行错误。我用 \n<br> 检查了它——同样的错误。

推荐方案

如果您使用 HTML 代码为使用 FPDF 库生成的 PDF 文档构建 tables,那么我想推荐 FPDF easyTable project (FPDF Add-on).

您必须从此项目下载 easyTable.phpformatedstring.phpexfpdf.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

The simplest way to specify a string is to enclose it in single quotes (the character ').

To specify a literal single quote, escape it with a backslash (\). To specify a literal backslash, double it (\). All other instances of backslash will be treated as a literal backslash: this means that the other escape sequences you might be used to, such as \r or \n, will be output literally as specified rather than having any special meaning.

如果字符串包含在双引号 (") 中,PHP 将解释 [转义序列,例如 \n]。