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;">¥ 35,000</td>
            <td style="text-align: right;">¥ 70,000</td>
        </tr>
        <tr style="background-color: #F8F9FA;">
            <td>无线鼠标</td>
            <td style="text-align: center;">5</td>
            <td style="text-align: right;">¥ 450</td>
            <td style="text-align: right;">¥ 2,250</td>
        </tr>
        <tr>
            <td>机械键盘</td>
            <td style="text-align: center;">3</td>
            <td style="text-align: right;">¥ 2,800</td>
            <td style="text-align: right;">¥ 8,400</td>
        </tr>
        <tr style="background-color: #F8F9FA;">
            <td>显示器支架</td>
            <td style="text-align: center;">4</td>
            <td style="text-align: right;">¥ 1,200</td>
            <td style="text-align: right;">¥ 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;\">¥ " . 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 许可证发布。