Skip to content

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

ClasseResponsabilità
HtmlParserPunto ingresso principale — tokenizza e renderizza HTML
CssRuleAnalizza selettori CSS e dichiarazioni con specificità
HtmlStyleStateTraccia contesto stile annidato (font, colore, allineamento)
TableParserGestisce layout <table> — larghezze colonne, span, intestazioni
HtmlTagHandlerInvia callback tag apertura/chiusura
HtmlTokenizerDivide HTML grezzo in token tag e testo
HtmlEntityDecodifica entità HTML nominate e numeriche
InlineStyleAnalizza 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 = ''): static

writeHtmlCell()

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): static

Tag 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-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

Rilasciato sotto licenza LGPL-3.0-or-later.