我正在尝试生成一个 PDF,其中包含一个带有拆分/合并单元格的表格,如下所示:
细胞 | 细胞 | 细胞
细胞 | C1 | C2 | 细胞
| C1 | C2 | 细胞
我正在使用 fpdf 并被称为多单元表脚本,我以前在类似的 pdf 中使用过该脚本。我查看了代码,想不出一种方法可以根据我的需要使单元格拆分或合并。有谁知道如何做到这一点?
只需手动执行,使单元格宽度成为两个合并单元格的总和。
根据您的示例:
$column_widths = ["50","50","50","50"];
// First row.
$pdf->Cell($column_widths[0], 5, "Cell", "", 0, "C", false);
$pdf->Cell($column_widths[1] + $column_widths[2], 5, "Cell", "", 0, "C", false);
$pdf->Cell($column_widths[3], 5, "Cell", "", 0, "C", false);
// Second row.
$pdf->Cell($column_widths[0], 5, "Cell", "", 0, "C", false);
$pdf->Cell($column_widths[1], 5, "C1", "", 0, "C", false);
$pdf->Cell($column_widths[2], 5, "C2", "", 0, "C", false);
$pdf->Cell($column_widths[3], 5, "Cell", "", 0, "C", false);
// Third row.
$pdf->Cell($column_widths[0], 5, "", "", 0, "C", false);
$pdf->Cell($column_widths[1], 5, "C1", "", 0, "C", false);
$pdf->Cell($column_widths[2], 5, "C2", "", 0, "C", false);
$pdf->Cell($column_widths[3], 5, "Cell", "", 0, "C", false);
或类似的东西。
您可以使用 EasyTable https://github.com/fpdf-easytable/fpdf-easytable
一个简单的例子:
$table=new easyTable($pdf, 3, 'width:100;');
$table->easyCell('Text 1', 'rowspan:2; valign:T');
$table->easyCell('Text 2', 'bgcolor:#b3ccff; rowspan:2');
$table->easyCell('Text 3');
$table->printRow();
$table->rowStyle('min-height:20');
$table->easyCell('Text 4', 'bgcolor:#3377ff; rowspan:2');
$table->printRow();
$table->easyCell('Text 5', 'bgcolor:#99bbff;colspan:2');
$table->printRow();
$table->endTable();