Skip to content

表格

TCPDF-Next 支援透過 HTML 語法建立表格,使用 writeHtml() 方法可以快速將 HTML 表格轉換為 PDF 內容。本範例展示從基本表格到進階排版的各種技巧。

基本 HTML 表格

最直接的方式是撰寫 HTML 表格,再透過 writeHtml() 渲染到 PDF 中:

php
<?php

use YeeeFang\TcpdfNext\Document\PdfDocument;
use YeeeFang\TcpdfNext\Document\PageFormat;
use YeeeFang\TcpdfNext\Html\HtmlRenderer;

$pdf = PdfDocument::create()
    ->setTitle('表格範例')
    ->setPageFormat(PageFormat::A4)
    ->build();

$page = $pdf->addPage();
$renderer = new HtmlRenderer($pdf);

$html = <<<'HTML'
<table border="1" cellpadding="5" cellspacing="0" width="100%">
    <thead>
        <tr style="background-color: #2C3E50; color: #FFFFFF;">
            <th width="30%">產品名稱</th>
            <th width="15%">數量</th>
            <th width="25%">單價</th>
            <th width="30%">小計</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>筆記型電腦</td>
            <td style="text-align: center;">2</td>
            <td style="text-align: right;">NT$ 35,000</td>
            <td style="text-align: right;">NT$ 70,000</td>
        </tr>
        <tr style="background-color: #F8F9FA;">
            <td>無線滑鼠</td>
            <td style="text-align: center;">5</td>
            <td style="text-align: right;">NT$ 450</td>
            <td style="text-align: right;">NT$ 2,250</td>
        </tr>
        <tr>
            <td>機械鍵盤</td>
            <td style="text-align: center;">3</td>
            <td style="text-align: right;">NT$ 2,800</td>
            <td style="text-align: right;">NT$ 8,400</td>
        </tr>
        <tr style="background-color: #F8F9FA;">
            <td>螢幕支架</td>
            <td style="text-align: center;">4</td>
            <td style="text-align: right;">NT$ 1,200</td>
            <td style="text-align: right;">NT$ 4,800</td>
        </tr>
    </tbody>
</table>
HTML;

$renderer->writeHtml($html);

合併儲存格

使用 colspanrowspan 屬性合併儲存格,適合製作報表標題或彙總列:

php
$html = <<<'HTML'
<table border="1" cellpadding="5" cellspacing="0" width="100%">
    <tr style="background-color: #1ABC9C; color: white;">
        <th colspan="4" style="text-align: center; font-size: 14px;">
            2026 年度銷售報表
        </th>
    </tr>
    <tr style="background-color: #16A085; color: white;">
        <th width="25%">季度</th>
        <th width="25%">產品 A</th>
        <th width="25%">產品 B</th>
        <th width="25%">合計</th>
    </tr>
    <tr>
        <td>第一季</td>
        <td style="text-align: right;">$120,000</td>
        <td style="text-align: right;">$85,000</td>
        <td style="text-align: right; font-weight: bold;">$205,000</td>
    </tr>
    <tr style="background-color: #F0F0F0;">
        <td>第二季</td>
        <td style="text-align: right;">$135,000</td>
        <td style="text-align: right;">$92,000</td>
        <td style="text-align: right; font-weight: bold;">$227,000</td>
    </tr>
    <tr>
        <td>第三季</td>
        <td style="text-align: right;">$148,000</td>
        <td style="text-align: right;">$103,000</td>
        <td style="text-align: right; font-weight: bold;">$251,000</td>
    </tr>
    <tr style="background-color: #2C3E50; color: white;">
        <td style="font-weight: bold;">年度合計</td>
        <td colspan="2" style="text-align: center;">--</td>
        <td style="text-align: right; font-weight: bold;">$683,000</td>
    </tr>
</table>
HTML;

$renderer->writeHtml($html);

帶樣式的表格

透過 inline CSS 來控制表格的視覺呈現:

php
$html = <<<'HTML'
<table border="0" cellpadding="6" cellspacing="0" width="100%"
       style="border-collapse: collapse;">
    <tr>
        <th style="background-color: #8E44AD; color: white;
                   border-bottom: 2px solid #6C3483;" width="30%">部門</th>
        <th style="background-color: #8E44AD; color: white;
                   border-bottom: 2px solid #6C3483;" width="40%">負責人</th>
        <th style="background-color: #8E44AD; color: white;
                   border-bottom: 2px solid #6C3483;" width="30%">人數</th>
    </tr>
    <tr style="border-bottom: 1px solid #DDDDDD;">
        <td>工程部</td>
        <td>王大明</td>
        <td style="text-align: center;">42</td>
    </tr>
    <tr style="background-color: #F9F9F9; border-bottom: 1px solid #DDDDDD;">
        <td>設計部</td>
        <td>李美麗</td>
        <td style="text-align: center;">15</td>
    </tr>
    <tr style="border-bottom: 1px solid #DDDDDD;">
        <td>行銷部</td>
        <td>張國強</td>
        <td style="text-align: center;">23</td>
    </tr>
</table>
HTML;

$renderer->writeHtml($html);

巢狀表格

在表格的儲存格中嵌入另一個表格,適合呈現階層式資料:

php
$html = <<<'HTML'
<table border="1" cellpadding="5" width="100%">
    <tr>
        <th style="background-color: #34495E; color: white;" width="25%">部門</th>
        <th style="background-color: #34495E; color: white;" width="75%">成員</th>
    </tr>
    <tr>
        <td style="vertical-align: top;"><strong>工程部</strong></td>
        <td>
            <table border="1" cellpadding="3" width="100%">
                <tr style="background-color: #D5DBDB;">
                    <td width="40%">姓名</td>
                    <td width="30%">職稱</td>
                    <td width="30%">年資</td>
                </tr>
                <tr>
                    <td>王小明</td>
                    <td>資深工程師</td>
                    <td>5 年</td>
                </tr>
                <tr>
                    <td>李美麗</td>
                    <td>前端工程師</td>
                    <td>3 年</td>
                </tr>
            </table>
        </td>
    </tr>
</table>
HTML;

$renderer->writeHtml($html);

長表格自動分頁

當表格資料量大、超出頁面時,系統會自動分頁。搭配 <thead> 標籤,表頭會在每一頁重複出現:

php
// 產生包含 50 筆資料的長表格
$rows = '';
for ($i = 1; $i <= 50; $i++) {
    $bg = ($i % 2 === 0) ? ' style="background-color: #F7F9FC;"' : '';
    $rows .= "<tr{$bg}>";
    $rows .= "<td style=\"text-align: center;\">{$i}</td>";
    $rows .= "<td>客戶 {$i}</td>";
    $rows .= "<td style=\"text-align: right;\">NT$ " . number_format($i * 1250) . "</td>";
    $rows .= "<td style=\"text-align: center;\">已完成</td>";
    $rows .= '</tr>';
}

$html = <<<HTML
<table border="1" cellpadding="4" cellspacing="0" width="100%">
    <thead>
        <tr style="background-color: #34495E; color: white;">
            <th width="10%">編號</th>
            <th width="40%">客戶名稱</th>
            <th width="30%">訂單金額</th>
            <th width="20%">狀態</th>
        </tr>
    </thead>
    <tbody>
        {$rows}
    </tbody>
</table>
HTML;

$renderer->writeHtml($html);

效能提示

對於超過數百行的大型表格,建議分批渲染或使用串流輸出,避免一次載入所有資料造成記憶體壓力。詳見 記憶體最佳化 章節。

下一步

以 LGPL-3.0-or-later 授權釋出。