如何使用PHP和FPDF制作此类表格?

9

如何使用PHP的FPDF制作像这样的表格?

我似乎无法使用$this->Cell实现此操作。

Table


如果可以使用TCPDF,我愿意使用它。 - Heather McVay
3个回答

17

FPDF 不支持 rowspancolspan。以下是一个解决方法,您可以尝试使用空单元格和 border 属性来代替,使用 Cell

$pdf->Cell(40,5,' ','LTR',0,'L',0);   // empty cell with left,top, and right borders
$pdf->Cell(50,5,'Words Here',1,0,'L',0);
$pdf->Cell(50,5,'Words Here',1,0,'L',0);
$pdf->Cell(40,5,'Words Here','LR',1,'C',0);  // cell with left and right borders
$pdf->Cell(50,5,'[ x ] abc',1,0,'L',0);
$pdf->Cell(50,5,'[ x ] checkbox1',1,0,'L',0);
$pdf->Cell(40,5,'','LBR',1,'L',0);   // empty cell with left,bottom, and right borders
$pdf->Cell(50,5,'[ x ] def',1,0,'L',0);
$pdf->Cell(50,5,'[ x ] checkbox2',1,0,'L',0);

结果将会是 - fpdf table example


单元格函数不会自动换行,如果内容长度超过单元格宽度,则会溢出。如果您有地址并使用单元格显示,则可能会从页面中溢出,影响美观。您知道如何解决这个问题吗?也许可以使用MultiCell函数来解决? - Shahrzad
这个代码是可以运行的,但是换行的位置不对,也就是说当一行结束并且需要换到下一行时,位置不正确。这是由第五个值决定的,其中1表示真(下一个单元格开始新的一行),0表示假。另一种表述方式是当第五个值为1时,该单元格是该行的末尾,因此上面代码中的第三行应该是 $pdf->Cell(50,5,'Words Here',1,1,'L',0);。 - Jason

5

我认为我找到了另一种解决方案,以下是我的解决方案,而且不需要空单元格。

$pdf->Cell(40,18,'Words Here', 1,0, 'C');
$x = $pdf->GetX();
$pdf->Cell(40,6,'Words Here', 1,0);
$pdf->Cell(40,6,'Words Here', 1,1);
$pdf->SetX($x);
$pdf->Cell(40,6,'[x] abc', 1,0);
$pdf->Cell(40,6,'[x] Checkbox 1', 1,1);
$pdf->SetX($x);
$pdf->Cell(40,6,'[x] def', 1,0);
$pdf->Cell(40,6,'[x] Checkbox 1', 1,1);

以下是结果:

结果


3
谢谢,这很有帮助,对我有用:

$this->Cell(40,5,' ','LTR',0,'L',0);   // empty cell with left,top, and right borders
$this->Cell(50,5,'111 Here',1,0,'L',0);
$this->Cell(50,5,'222 Here',1,0,'L',0);

                $this->Ln();

$this->Cell(40,5,'Solid Here','LR',0,'C',0);  // cell with left and right borders
$this->Cell(50,5,'[ o ] che1','LR',0,'L',0);
$this->Cell(50,5,'[ x ] che2','LR',0,'L',0);

                $this->Ln();

$this->Cell(40,5,'','LBR',0,'L',0);   // empty cell with left,bottom, and right borders
$this->Cell(50,5,'[ x ] def3','LRB',0,'L',0);
$this->Cell(50,5,'[ o ] def4','LRB',0,'L',0);

                $this->Ln();
                $this->Ln();
                $this->Ln();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接