Skip to content

文字输出 (HasTextOutput)

HasTextOutput trait 提供在页面上放置文字的主要方法。每个方法对应不同的排版模型:固定单元格、流式文字、绝对定位,以及 HTML 渲染。

快速参考

方法排版模型自动换行
cell()单行方框否 — 超出部分会被裁切
multiCell()多行区块是 — 自动在单元格宽度处换行
text()绝对定位
write()行内流式是 — 如同文字处理器般自然流动
writeHtml()HTML 区块是 — 完整 HTML 排版
writeHtmlCell()定位的 HTML 单元格
ln()换行不适用

基本示例

php
use Yeeefang\TcpdfNext\Core\Document;

$pdf = Document::create()
    ->addPage()
    ->setFont('Helvetica', '', 12)
    ->cell(0, 10, 'Single line cell', newLine: true)
    ->multiCell(0, 10, 'This is a longer text that will automatically wrap to multiple lines when it reaches the edge of the printable area.')
    ->ln(5)
    ->text(50.0, 100.0, 'Absolute positioned text')
    ->write(10, 'Inline text that ')
    ->write(10, 'continues flowing.');

所有方法皆返回 static,因此每个调用都可以链式串接。

cell()

绘制一个单行矩形单元格,可选择性地加上边框、背景填充、对齐方式与链接。

php
$pdf->cell(
    float  $w,           // 宽度(0 = 延伸至右边距)
    float  $h,           // 高度
    string $txt,         // 文字内容
    mixed  $border = 0,  // 0、1 或 'LTRB' 组合
    bool   $newLine = false,
    string $align  = '',  // L、C、R、J
    bool   $fill   = false,
    mixed  $link   = '',
);

光标会移动到单元格右侧(若 $newLinetrue,则移至下一行)。

对齐方式

php
use Yeeefang\TcpdfNext\Contracts\Enums\Alignment;

$pdf->cell(0, 10, 'Left aligned')
    ->ln()
    ->cell(0, 10, 'Centered', align: 'C')
    ->ln()
    ->cell(0, 10, 'Right aligned', align: 'R')
    ->ln()
    ->cell(0, 10, 'Justified text in a cell', align: 'J');

可用的对齐选项:

说明
'L' 或留空左对齐(默认)
'C'居中对齐
'R'右对齐
'J'两端对齐(齐行)

单元格边框

php
// 无边框
$pdf->cell(60, 10, 'No border', border: 0);

// 完整外框
$pdf->cell(60, 10, 'Full frame', border: 1);

// 个别边 — L(左)、T(上)、R(右)、B(下)
$pdf->cell(60, 10, 'Top and bottom', border: 'TB');
$pdf->cell(60, 10, 'Left only', border: 'L');

边框参数说明:

说明
0无边框
1完整外框(四边皆有)
'L'左边框
'T'上边框
'R'右边框
'B'下边框
'LTRB'可自由组合,例如 'TB' 表示上下边框

填充模式

php
$pdf->setFillColor(230, 230, 250)
    ->cell(0, 10, 'Lavender background', fill: true, newLine: true);

设置 fill: true 时,单元格会以当前的填充颜色作为背景。请先调用 setFillColor() 设置所需的背景色。

multiCell()

渲染多行文字区块,自动在指定宽度处换行。输出后光标会移至单元格下方。

php
$pdf->multiCell(
    float  $w,           // 宽度(0 = 延伸至右边距)
    float  $h,           // 最小行高
    string $txt,         // 文字内容
    mixed  $border = 0,
    string $align  = 'J',
    bool   $fill   = false,
);
php
$pdf->setFont('Helvetica', '', 11)
    ->multiCell(80, 6, 'This paragraph will wrap at 80mm width. The text flows naturally, respecting word boundaries and hyphenation rules.', border: 1, align: 'J');

text()

在绝对坐标 (x, y) 放置一段文字。调用后光标不会移动 — 这是一个「放置即完成」的方法。

php
$pdf->text(20.0, 50.0, 'Positioned at x=20, y=50');

适合用于水印、标签,或需要精确控制位置的叠加内容。

write()

在当前光标位置以行内方式写入文字。当文字到达右边距时会自动换行,就像在文字处理器中输入一样。

php
$pdf->setFont('Helvetica', '', 12)
    ->write(6, 'This sentence starts here and ')
    ->setFont('Helvetica', 'B', 12)
    ->write(6, 'this part is bold')
    ->setFont('Helvetica', '', 12)
    ->write(6, ' then back to normal.');

第一个参数($h)是行高。当需要在句子中间切换字体或样式时,请使用 write()

writeHtml()

使用内置 HTML 解析器渲染 HTML 字符串。支持常见标签,包括标题、段落、表格、列表与行内样式。

php
$pdf->writeHtml('<h2>Section Title</h2><p>Paragraph with <b>bold</b> and <i>italic</i> text.</p>');

HTML 表格

php
$html = '
<table border="1" cellpadding="4">
    <thead>
        <tr>
            <th>Product</th>
            <th>Qty</th>
            <th>Price</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Widget A</td>
            <td align="center">10</td>
            <td align="right">$25.00</td>
        </tr>
        <tr>
            <td>Widget B</td>
            <td align="center">5</td>
            <td align="right">$42.50</td>
        </tr>
    </tbody>
</table>';

$pdf->writeHtml($html);

writeHtmlCell()

结合 HTML 渲染与单元格定位。HTML 内容会被放置在指定坐标的单元格内。

php
$pdf->writeHtmlCell(
    float  $w,     // 宽度
    float  $h,     // 最小高度
    float  $x,     // X 位置
    float  $y,     // Y 位置
    string $html,
    mixed  $border = 0,
    bool   $fill   = false,
);
php
$pdf->writeHtmlCell(90, 0, 10, 50, '<p style="color:#336699;">Positioned HTML content with <b>formatting</b>.</p>', border: 1);

ln()

插入换行。光标会移至左边距并向下移动指定高度。

php
$pdf->ln();       // 使用上一个单元格高度作为间距
$pdf->ln(10);     // 换行并加入 10mm 垂直间距
$pdf->ln(0);      // 移至左边距,不做垂直移动

方法选择指南

使用场景建议方法
表格单元格、标签、单行数据cell()
段落、描述、长篇文字multiCell()
水印、印章、绝对定位标签text()
行内混合格式文字write()
含 HTML 标记的丰富内容writeHtml()
定位在特定位置的 HTML 内容writeHtmlCell()

以 LGPL-3.0-or-later 许可证发布。