
A table can be created in an FPDF document by placing a series of Cell() elements next to each other. Each column keeps a fixed width, and the final Cell in every row moves the cursor to the next line.
Header row
|
+-- ID
+-- NAME
+-- CLASS
+-- MARK
|
v
Data row 1
|
v
Data row 2
|
v
Data row 3
This example creates a four-column table, adds a colored header and then displays sample student records.
Show Table of ContentsThe Cell() method draws one rectangular cell and can place text inside it.
$pdf->Cell(width,height,text,border,line,align,fill);
For a PDF table, these parameters control the table layout:
1 to draw a border around the cell.0 to keep the next cell on the same line and 1 to move to the next row.'L', 'C' or 'R' for left, center or right alignment.true to apply the current fill color or false for no fill.Store the column widths in an array:
$width_cell=[10,30,20,30];
The four values correspond to:
ID = 10 mm
NAME = 30 mm
CLASS = 20 mm
MARK = 30 mm
The table width is therefore:
10 + 30 + 20 + 30 = 90 mm
Using an array makes it easy to keep every Cell in the same column at exactly the same width.
Set a bold font and a background color for the header:
$pdf->SetFont('Arial','B',12);
$pdf->SetFillColor(193,229,252);
Then create the four header cells:
$pdf->Cell($width_cell[0],10,'ID',1,0,'C',true);
$pdf->Cell($width_cell[1],10,'NAME',1,0,'C',true);
$pdf->Cell($width_cell[2],10,'CLASS',1,0,'C',true);
$pdf->Cell($width_cell[3],10,'MARK',1,1,'C',true);
The first three cells use 0 for the line parameter so the next cell remains on the same row.
The final MARK cell uses:
1
This moves the cursor to the next line after the header row is complete.
$pdf->Cell($width_cell[2],10,'CLASS',1,0,'C',true);
This means:
$width_cell[2] - width of the CLASS column.10 - cell height.'CLASS' - text displayed inside the cell.1 - draw the border.0 - keep the next cell on the same line.'C' - center-align the text.true - apply the current fill color.After the header, change to a normal font:
$pdf->SetFont('Arial','',10);
A complete data row can be added like this:
$pdf->Cell($width_cell[0],10,'1',1,0,'C',false);
$pdf->Cell($width_cell[1],10,'John Deo',1,0,'L',false);
$pdf->Cell($width_cell[2],10,'Four',1,0,'C',false);
$pdf->Cell($width_cell[3],10,'75',1,1,'C',false);
Again, the last Cell uses 1 so the next record begins on a new line.
Repeating four Cell() calls for every record works, but storing the data in an array makes the example easier to extend.
$data=[
[1,'John Deo','Four',75],
[2,'Max Ruin','Three',85],
[3,'Arnold','Three',55]
];
Loop through each row:
foreach($data as $row){
$pdf->Cell($width_cell[0],10,(string)$row[0],1,0,'C',false);
$pdf->Cell($width_cell[1],10,$row[1],1,0,'L',false);
$pdf->Cell($width_cell[2],10,$row[2],1,0,'C',false);
$pdf->Cell($width_cell[3],10,(string)$row[3],1,1,'C',false);
}
This structure is also closer to the next step, where the rows will come from a MySQL or SQLite database instead of a manually created array.
The script creates one header row and three data rows. The browser displays the resulting PDF using the filename student-table.pdf.
Once the table layout is understood, the manually created $data array can be replaced by records collected from a database.
Database
|
v
PDO SELECT
|
v
Rows
|
v
foreach()
|
v
FPDF Cell()
The complete database example is available here:
Create PDF Table from MySQL or SQLite RecordsThe Cell() method is suitable when each value fits on a single line. If a column contains longer text that must wrap, use the FPDF MultiCell() method.
This is important because a normal Cell does not automatically increase the row height to accommodate long text.
Use the same width value for every Cell belonging to the same column. Keeping widths in one array helps prevent inconsistent column sizes.
The final Cell in each row must move to the next line. In this example the line parameter of the final Cell is set to 1.
Call SetFillColor() first and pass true as the fill argument of the header Cells.
Increase the column width or use MultiCell() when text needs to wrap.
Do not send HTML, spaces or debugging output before the PDF is generated. The FPDF script should call Output() before any unrelated browser output.
Create a row by placing multiple Cell() elements next to each other. Keep consistent widths for each column and move to the next line after the last Cell in each row.
Store the widths in an array and use the matching array value whenever a Cell is created for that column.
Pass 1 as the border parameter of Cell() to draw a border around the cell.
Set the color with SetFillColor() and pass true as the fill parameter for each header Cell.
Set the line parameter of the last Cell in the row to 1. This moves the current position to the beginning of the next line.
Yes. Fetch the database records using PDO and loop through the returned rows while adding the values with Cell().
Use MultiCell() when the content must wrap onto multiple lines. A normal Cell is best suited to single-line values.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| riddhi desai | 23-03-2018 |
| HOW CAN I USE MULTCELL WITH THIS? | |
| smo1234 | 14-09-2018 |
| One tutorial on MultiCell is available here, it can be used. We will try to develop one sample. | |
22-07-2020 | |
| Wonderful tutorial. Explained very well with examples and the result.Thank you very much. | |