Skip to content

Text Output (HasTextOutput)

The HasTextOutput trait provides the primary methods for placing text on a page. Each method serves a different layout model: fixed cells, flowing text, absolute positioning, and HTML rendering.

Quick Reference

MethodLayout ModelWrapping
cell()Single-line boxNo — text is clipped
multiCell()Multi-line blockYes — auto-wraps at cell width
text()Absolute positionNo
write()Inline flowYes — flows like a word processor
writeHtml()HTML blockYes — full HTML layout
writeHtmlCell()HTML in a positioned cellYes
ln()Line breakN/A

Basic Example

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.');

All methods return static, so every call can be chained.

cell()

Draws a single-line rectangular cell with optional border, background fill, alignment, and link.

php
$pdf->cell(
    float  $w,           // Width (0 = extend to right margin)
    float  $h,           // Height
    string $txt,         // Text content
    mixed  $border = 0,  // 0, 1, or 'LTRB' combination
    bool   $newLine = false,
    string $align  = '',  // L, C, R, J
    bool   $fill   = false,
    mixed  $link   = '',
);

The cursor moves to the right of the cell (or to the next line when $newLine is true).

Alignment

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

Borders

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

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

// Individual sides — Left, Top, Right, Bottom
$pdf->cell(60, 10, 'Top and bottom', border: 'TB');
$pdf->cell(60, 10, 'Left only', border: 'L');

Fill

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

multiCell()

Renders a multi-line text block that auto-wraps at the specified width. After output, the cursor moves below the cell.

php
$pdf->multiCell(
    float  $w,           // Width (0 = extend to right margin)
    float  $h,           // Minimum line height
    string $txt,         // Text content
    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()

Places a single string at an absolute (x, y) position. The cursor is not moved afterward — this is a "fire and forget" method.

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

Use text() for watermarks, labels, or overlay content where you control the exact position.

write()

Writes text inline at the current cursor position. Text wraps automatically when it reaches the right margin, just like typing in a word processor.

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.');

The first parameter ($h) is the line height. Use write() when you need mid-sentence font or style changes.

writeHtml()

Renders an HTML string using the built-in HTML parser. Supports common tags including headings, paragraphs, tables, lists, and inline styles.

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

Tables

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()

Combines HTML rendering with cell positioning. The HTML content is placed inside a cell at the specified coordinates.

php
$pdf->writeHtmlCell(
    float  $w,     // Width
    float  $h,     // Minimum height
    float  $x,     // X position
    float  $y,     // Y position
    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()

Inserts a line break. The cursor moves to the left margin and down by the specified height.

php
$pdf->ln();       // Line break using the last cell height
$pdf->ln(10);     // Line break with 10mm vertical spacing
$pdf->ln(0);      // Move to left margin without vertical movement

Choosing the Right Method

ScenarioMethod
Table cells, labels, single-line datacell()
Paragraphs, descriptions, long textmultiCell()
Watermarks, stamps, absolute labelstext()
Inline text with mixed formattingwrite()
Rich content with HTML markupwriteHtml()
HTML content at a specific positionwriteHtmlCell()

Released under the LGPL-3.0-or-later License.