【问题标题】:FPDF add-on "Write-Html-Tables" with multicell support / line breaks in cellFPDF 插件“Write-Html-Tables”,支持多单元格/单元格中的换行符
【发布时间】:2018-07-24 16:49:02
【问题描述】:

我使用 FPDF 插件“write-html-tables”。该插件非常适用于 HTML 表格。

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

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

我尝试从“write-html-tables”插件扩展代码,但它不起作用。有没有人告诉我如何扩展插件?

【问题讨论】:

  • 你试过单元格中的<br>标签吗?
  • 是的,不幸的是
    标签在带有
    的单元格下生成了一个全新的单元格,并且表格的结构不再正确。

标签: php pdf fpdf


【解决方案1】:

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]。

【讨论】:

    猜你喜欢
    • 2018-05-22
    • 1970-01-01
    • 1970-01-01
    • 2018-04-27
    • 2013-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多