Skip to content

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

ClassTanggung Jawab
HtmlParserTitik masuk utama — tokenisasi dan render HTML
CssRuleParse CSS selector dan deklarasi dengan specificity
HtmlStyleStateMelacak konteks style bersarang (font, warna, alignment)
TableParserMenangani layout <table> — lebar kolom, span, header
HtmlTagHandlerDispatch callback tag open/close
HtmlTokenizerMemecah HTML mentah menjadi token tag dan teks
HtmlEntityDecode entitas HTML bernama dan numerik
InlineStyleParse string atribut style="..."

writeHtml()

php
use Yeeefang\TcpdfNext\Core\Document;

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

writeHtmlCell()

Render HTML di dalam cell persegi panjang berposisi:

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

PropertiContoh Nilai
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

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 width pada <td> atau <th>
  • Styling header<th> menerima teks bold secara default
  • Page break — tabel yang melebihi tinggi halaman otomatis dipecah

Contoh Lengkap

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">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() sebelum writeHtml() — 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.

Didistribusikan di bawah lisensi LGPL-3.0-or-later.