테이블
TCPDF-Next의 writeHtml() 메서드를 사용하여 전문적인 테이블을 만듭니다. <thead>, <tbody>, <tfoot>, colspan, rowspan, 인라인 CSS를 지원합니다.
전체 예제
php
<?php
declare(strict_types=1);
require __DIR__ . '/vendor/autoload.php';
use TcpdfNext\Document;
$html = <<<'HTML'
<style>
table { width: 100%; border-collapse: collapse; }
th {
background-color: #1a2634;
color: #ffffff;
padding: 8px;
font-size: 10pt;
}
td {
padding: 6px 8px;
border-bottom: 1px solid #dee2e6;
font-size: 10pt;
}
tr:nth-child(even) { background-color: #f8f9fa; }
.right { text-align: right; }
.center { text-align: center; }
.total {
background-color: #1a2634;
color: #ffffff;
font-weight: bold;
}
</style>
<table border="1" cellpadding="5">
<thead>
<tr>
<th>SKU</th>
<th>Product</th>
<th class="center">Qty</th>
<th class="right">Unit Price</th>
<th class="right">Subtotal</th>
</tr>
</thead>
<tbody>
<tr>
<td>WDG-001</td>
<td>Precision Widget</td>
<td class="center">120</td>
<td class="right">$24.99</td>
<td class="right">$2,998.80</td>
</tr>
<tr>
<td>GDG-042</td>
<td>Gadget Pro</td>
<td class="center">85</td>
<td class="right">$49.95</td>
<td class="right">$4,245.75</td>
</tr>
<tr>
<td>SPR-007</td>
<td>Spring Assembly Kit</td>
<td class="center">340</td>
<td class="right">$12.50</td>
<td class="right">$4,250.00</td>
</tr>
</tbody>
<tfoot>
<tr class="total">
<td colspan="4" class="right">Grand Total</td>
<td class="right">$11,494.55</td>
</tr>
</tfoot>
</table>
HTML;
Document::create()
->setTitle('Product Inventory')
->addPage()
->setFont('helvetica', size: 10)
->writeHtml($html)
->save(__DIR__ . '/tables.pdf');
echo 'PDF created.' . PHP_EOL;colspan과 rowspan
표준 HTML과 동일하게 colspan으로 수평 병합, rowspan으로 수직 병합이 가능합니다:
php
$html = <<<'HTML'
<table border="1" cellpadding="6" style="width:100%;">
<tr style="background:#1a2634; color:#fff; font-weight:bold;">
<th colspan="4" style="text-align:center;">Quarterly Sales Report</th>
</tr>
<tr style="background:#2c3e50; color:#fff;">
<th>Quarter</th>
<th>Product A</th>
<th>Product B</th>
<th>Total</th>
</tr>
<tr>
<td>Q1</td>
<td class="right">$120,000</td>
<td class="right">$85,000</td>
<td class="right" style="font-weight:bold;">$205,000</td>
</tr>
<tr style="background:#f5f7fa;">
<td>Q2</td>
<td class="right">$135,000</td>
<td class="right">$92,000</td>
<td class="right" style="font-weight:bold;">$227,000</td>
</tr>
<tr>
<td rowspan="2" style="vertical-align:middle;">H2</td>
<td class="right">$148,000</td>
<td class="right">$103,000</td>
<td class="right" style="font-weight:bold;">$251,000</td>
</tr>
<tr>
<td class="right">$162,000</td>
<td class="right">$118,000</td>
<td class="right" style="font-weight:bold;">$280,000</td>
</tr>
<tr style="background:#1a2634; color:#fff; font-weight:bold;">
<td>Annual</td>
<td colspan="2" style="text-align:center;">--</td>
<td class="right">$963,000</td>
</tr>
</table>
HTML;
Document::create()
->setTitle('Quarterly Sales')
->addPage()
->setFont('helvetica', size: 10)
->writeHtml($html)
->save(__DIR__ . '/sales-report.pdf');핵심 개념
writeHtml()
writeHtml(string $html): static은 HTML을 PDF에 직접 렌더링합니다. 지원되는 테이블 요소: <table>, <thead>, <tbody>, <tfoot>, <tr>, <th>, <td>.
인라인 CSS 지원
writeHtml() 내에서 동작하는 CSS 속성:
| 속성 | 예시 |
|---|---|
background-color | #1a2634, rgb(26,38,52) |
color | #ffffff |
font-weight | bold |
text-align | left, center, right |
padding | 6px 8px |
border-bottom | 1px solid #dee2e6 |
width | 100%, 60mm |
vertical-align | middle, top, bottom |
페이지 레이아웃
기본 10mm 여백의 A4 페이지에서 인쇄 가능 너비는 약 190mm입니다. 테이블에 width: 100%를 설정하면 이 전체 영역을 채웁니다.
출력
첫 번째 예제는 어두운 헤더의 인벤토리 테이블, 교차 행 색상, 오른쪽 정렬 통화 컬럼, colspan 기반 합계 바닥글이 있는 단일 페이지 PDF를 생성합니다.