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
| Class | Responsibility |
|---|---|
HtmlParser | Main entry point — tokenizes and renders HTML |
CssRule | Parses CSS selectors and declarations with specificity |
HtmlStyleState | Tracks nested style context (font, color, alignment) |
TableParser | Handles <table> layout — column widths, spans, headers |
HtmlTagHandler | Dispatches open/close tag callbacks |
HtmlTokenizer | Splits raw HTML into tag and text tokens |
HtmlEntity | Decodes named and numeric HTML entities |
InlineStyle | Parses style="..." attribute strings |
writeHtml()
use Yeeefang\TcpdfNext\Core\Document;
$pdf = Document::create()
->addPage()
->setFont('DejaVuSans', '', 10)
->writeHtml('<h1>Hello World</h1><p>This is a paragraph.</p>');writeHtml(string $html, bool $ln = true, bool $fill = false, bool $reseth = false, bool $cell = false, string $align = ''): staticwriteHtmlCell()
Render HTML inside a positioned rectangular cell:
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): staticSupported 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.
| Property | Example Values |
|---|---|
font-family | DejaVuSans, Helvetica, serif |
font-size | 12pt, 16px, 1.2em |
font-weight / font-style | bold, italic, normal |
color / background-color | #ff6600, rgb(255,102,0), red |
text-align | left, center, right, justify |
text-decoration | underline, line-through, none |
line-height | 1.5, 18pt |
margin / padding | 5px, 10px 20px |
border | 1px solid #ddd |
width / height | 100%, 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
widthon<td>or<th> - Header styling —
<th>receives bold text by default - Page breaks — tables exceeding page height break automatically
Complete Example
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()beforewriteHtml()— 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.