9

How do you make a table like this with FPDF using PHP?

I can't seem to figure out how to do this with $this->Cell.

Table

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Heather McVay
  • 939
  • 3
  • 15
  • 31

3 Answers3

17

FPDF does not recognize rowspan or colspan. Here is a workaround that you can try, using empty cells and the border attribute for 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);

and the result would be - fpdf table example

Sean
  • 12,443
  • 3
  • 29
  • 47
  • Cell function do not wrap the contents of cell, if content length exceeds the width of cell the it flow away. Ugly affect if u have address and show using cell, then it will run away from page. did you know how t fix this? my be by using MultiCell? – Shahrzad May 05 '14 at 13:00
  • This works, except the line breaks are in the wrong place, meaning where the row is supposed to stop and go to a new row, is wrong. This is determined by the 5th value, where 1 is true (the next cell starts a new line) and 0 is false. Another way to state it is when the 5th value is 1, that cell is the end of the row, so the 3rd line in the above code should read $pdf->Cell(50,5,'Words Here',1,1,'L',0); – Jason Oct 10 '18 at 22:10
5

I think i found other solution, here is my solution without empty cell needed.

$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);

And here is the result:

Result

Mike Doe
  • 16,349
  • 11
  • 65
  • 88
syamcode
  • 66
  • 1
  • 2
3

Thanks, that helped, this worked for me:

$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();
Heather McVay
  • 939
  • 3
  • 15
  • 31