Parser HTML
Modul Html (8 class) menyediakan renderer HTML-to-PDF built-in. Modul ini mem-parse subset HTML/CSS dan me-render langsung ke dalam PDF — tidak memerlukan browser eksternal.
Class Utama
| Class | Tanggung Jawab |
|---|---|
HtmlParser | Titik masuk utama — tokenisasi dan render HTML |
CssRule | Parse CSS selector dan deklarasi dengan specificity |
HtmlStyleState | Melacak konteks style bersarang (font, warna, alignment) |
TableParser | Menangani layout <table> — lebar kolom, span, header |
HtmlTagHandler | Dispatch callback tag open/close |
HtmlTokenizer | Memecah HTML mentah menjadi token tag dan teks |
HtmlEntity | Decode entitas HTML bernama dan numerik |
InlineStyle | Parse string atribut style="..." |
writeHtml()
use Yeeefang\TcpdfNext\Core\Document;
$pdf = Document::create()
->addPage()
->setFont('DejaVuSans', '', 10)
->writeHtml('<h1>Hello World</h1><p>Ini adalah paragraf.</p>');writeHtml(string $html, bool $ln = true, bool $fill = false, bool $reseth = false, bool $cell = false, string $align = ''): staticwriteHtmlCell()
Render HTML di dalam cell persegi panjang berposisi:
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 yang Didukung
Block: <h1>-<h6>, <p>, <div>, <blockquote>, <pre>, <hr>Inline: <b>, <strong>, <i>, <em>, <u>, <s>, <del>, <sup>, <sub>, <span>, <code>, <a>, <br>List: <ul>, <ol>, <li> — bersarang hingga 4 level. Tabel: <table>, <tr>, <th>, <td> — lihat Engine Tabel di bawah. Media: <img src="..." width="..." height="...">
Dukungan CSS
Style dapat diterapkan melalui blok <style>, atribut style inline, atau keduanya. Parser menghormati specificity dan urutan cascade.
| Properti | Contoh Nilai |
|---|---|
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 |
Parsing Aturan CSS (CssRule)
CssRule menghitung specificity untuk resolusi cascade:
- Element selector
h1,td— specificity (0, 0, 1) - Class selector
.highlight— specificity (0, 1, 0) - ID selector
#header— specificity (1, 0, 0) - Compound selector
table td.active— specificity dijumlahkan
Specificity yang sama diselesaikan berdasarkan urutan sumber (deklarasi terakhir menang).
State Style (HtmlStyleState)
HtmlStyleState memelihara stack konteks style. Tag pembuka melakukan push state; tag penutup melakukan pop. Ini memastikan style bersarang terselesaikan dengan benar tanpa bocor ke sibling.
Engine Tabel (TableParser)
colspan/rowspan— penggabungan cell horizontal dan vertikal- Lebar kolom otomatis — distribusi proporsional berdasarkan konten
- Lebar kolom tetap — melalui CSS
widthpada<td>atau<th> - Styling header —
<th>menerima teks bold secara default - Page break — tabel yang melebihi tinggi halaman otomatis dipecah
Contoh Lengkap
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">Jatuh tempo: 2026-03-01</p>
<table>
<tr><th>Item</th><th>Qty</th><th>Harga</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
- Selalu panggil
setFont()sebelumwriteHtml()— parser menggunakan font saat ini sebagai default untuk teks tanpa style. - Untuk dukungan CSS3 penuh (Flexbox, Grid, web font), gunakan package Artisan sebagai gantinya.
- Tabel HTML besar secara otomatis dipecah lintas halaman saat auto page break diaktifkan.