Skip to content

HTML Parser

The Html module (8 classes) provides a built-in HTML-to-PDF renderer. It parses a subset of HTML/CSS and renders it directly into the PDF — no external browser required.

Key Classes

ClassResponsibility
HtmlParserMain entry point — tokenizes and renders HTML
CssRuleParses CSS selectors and declarations with specificity
HtmlStyleStateTracks nested style context (font, color, alignment)
TableParserHandles <table> layout — column widths, spans, headers
HtmlTagHandlerDispatches open/close tag callbacks
HtmlTokenizerSplits raw HTML into tag and text tokens
HtmlEntityDecodes named and numeric HTML entities
InlineStyleParses style="..." attribute strings

writeHtml()

php
use Yeeefang\TcpdfNext\Core\Document;

$pdf = Document::create()
    ->addPage()
    ->setFont('DejaVuSans', '', 10)
    ->writeHtml('<h1>Hello World</h1><p>This is a paragraph.</p>');
php
writeHtml(string $html, bool $ln = true, bool $fill = false, bool $reseth = false, bool $cell = false, string $align = ''): static

writeHtmlCell()

Render HTML inside a positioned rectangular cell:

php
writeHtmlCell(float $w, float $h, float $x, float $y, string $html, mixed $border = 0, int $ln = 0, bool $fill = false, bool $reseth = true, string $align = '', bool $autopadding = true): static

Supported HTML Tags

Block: <h1><h6>, <p>, <div>, <blockquote>, <pre>, <hr>Inline: <b>, <strong>, <i>, <em>, <u>, <s>, <del>, <sup>, <sub>, <span>, <code>, <a>, <br>Lists: <ul>, <ol>, <li> — nested up to 4 levels. Tables: <table>, <tr>, <th>, <td> — see Table Engine below. Media: <img src="..." width="..." height="...">

CSS Support

Styles can be applied via <style> blocks, inline style attributes, or both. The parser respects specificity and cascade order.

PropertyExample Values
font-familyDejaVuSans, Helvetica, serif
font-size12pt, 16px, 1.2em
font-weight / font-stylebold, italic, normal
color / background-color#ff6600, rgb(255,102,0), red
text-alignleft, center, right, justify
text-decorationunderline, line-through, none
line-height1.5, 18pt
margin / padding5px, 10px 20px
border1px solid #ddd
width / height100%, 200px

CSS Rule Parsing (CssRule)

CssRule computes specificity for cascade resolution:

  • Element selectors h1, td — specificity (0, 0, 1)
  • Class selectors .highlight — specificity (0, 1, 0)
  • ID selectors #header — specificity (1, 0, 0)
  • Compound selectors table td.active — specificities are summed

Equal specificity is resolved by source order (last declaration wins).

Style State (HtmlStyleState)

HtmlStyleState maintains a stack of style contexts. Opening tags push state; closing tags pop. This ensures nested styles resolve correctly without leaking to siblings.

Table Engine (TableParser)

  • colspan / rowspan — horizontal and vertical cell merging
  • Auto column width — proportional distribution based on content
  • Fixed column width — via CSS width on <td> or <th>
  • Header styling<th> receives bold text by default
  • Page breaks — tables exceeding page height break automatically

Complete Example

php
use Yeeefang\TcpdfNext\Core\Document;

$pdf = Document::create()
    ->addPage()
    ->setFont('DejaVuSans', '', 10)
    ->writeHtml('
        <style>
            h1 { color: #ff6600; font-size: 18pt; }
            .highlight { background-color: #ffffcc; padding: 5px; }
            table { border-collapse: collapse; width: 100%; }
            th { background-color: #333; color: #fff; padding: 8px; }
            td { border: 1px solid #ddd; padding: 8px; }
        </style>
        <h1>Invoice #2026-001</h1>
        <p class="highlight">Due: 2026-03-01</p>
        <table>
            <tr><th>Item</th><th>Qty</th><th>Price</th></tr>
            <tr><td>Widget A</td><td>10</td><td>$50.00</td></tr>
            <tr><td colspan="2">Total</td><td><b>$50.00</b></td></tr>
        </table>
    ');

Tips

  • Always call setFont() before writeHtml() — the parser uses the current font as the default for unstyled text.
  • For full CSS3 support (Flexbox, Grid, web fonts), use the Artisan package instead.
  • Large HTML tables automatically break across pages when auto page break is enabled.

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