Parser HTML
Il modulo Html (8 classi) fornisce un renderer HTML-to-PDF integrato. Analizza un subset di HTML/CSS e lo renderizza direttamente nel PDF — nessun browser esterno richiesto.
Classi Chiave
| Classe | Responsabilità |
|---|---|
HtmlParser | Punto ingresso principale — tokenizza e renderizza HTML |
CssRule | Analizza selettori CSS e dichiarazioni con specificità |
HtmlStyleState | Traccia contesto stile annidato (font, colore, allineamento) |
TableParser | Gestisce layout <table> — larghezze colonne, span, intestazioni |
HtmlTagHandler | Invia callback tag apertura/chiusura |
HtmlTokenizer | Divide HTML grezzo in token tag e testo |
HtmlEntity | Decodifica entità HTML nominate e numeriche |
InlineStyle | Analizza stringhe attributo style="..." |
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 = ''): staticwriteHtmlCell()
Renderizza HTML dentro una cella rettangolare posizionata:
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): staticTag HTML Supportati
Blocco: <h1>–<h6>, <p>, <div>, <blockquote>, <pre>, <hr>Inline: <b>, <strong>, <i>, <em>, <u>, <s>, <del>, <sup>, <sub>, <span>, <code>, <a>, <br>Liste: <ul>, <ol>, <li> — annidate fino a 4 livelli. Tabelle: <table>, <tr>, <th>, <td> — vedi Motore Tabelle sotto. Media: <img src="..." width="..." height="...">
Supporto CSS
Gli stili possono essere applicati tramite blocchi <style>, attributi style inline o entrambi. Il parser rispetta specificità e ordine cascade.
| Proprietà | Valori Esempio |
|---|---|
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 |