Create a PDF Table in PHP using FPDF Cell()

PDF table created in PHP using FPDF Cell method

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.

FPDF Cell() Parameters Used for Tables Top ↑

The 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:

Width
Width of the current table column.
Height
Height of the current row.
Text
Content displayed inside the cell.
Border
Use 1 to draw a border around the cell.
Line
Use 0 to keep the next cell on the same line and 1 to move to the next row.
Align
Use 'L', 'C' or 'R' for left, center or right alignment.
Fill
Use true to apply the current fill color or false for no fill.

Set the Width of Each PDF Table Column Top ↑

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.

Understanding One Header Cell Top ↑

$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.

Add Data Rows to the PDF Table Top ↑

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.

Generate PDF Table Rows using a PHP Loop Top ↑

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.

Complete PHP FPDF Table Example Top ↑

The script creates one header row and three data rows. The browser displays the resulting PDF using the filename student-table.pdf.

View the PDF Table Output Top ↑

View PDF Table Output

Video: Create a Table using FPDF Cell() Top ↑

Adding table to PDF document by using Cell with equal width by using alignment and position after cell

Display MySQL or SQLite Records in an FPDF Table Top ↑

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 Records

Use MultiCell for Long or Wrapped Text Top ↑

The 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.

Common Problems when Creating FPDF Tables Top ↑

Columns do not line up Top ↑

Use the same width value for every Cell belonging to the same column. Keeping widths in one array helps prevent inconsistent column sizes.

All Cells appear on one line Top ↑

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.

Header background color is not visible Top ↑

Call SetFillColor() first and pass true as the fill argument of the header Cells.

Long text crosses the Cell boundary Top ↑

Increase the column width or use MultiCell() when text needs to wrap.

PDF output gives an output-already-started error Top ↑

Do not send HTML, spaces or debugging output before the PDF is generated. The FPDF script should call Output() before any unrelated browser output.

Frequently Asked Questions Top ↑

Q1: How do I create a table in FPDF using PHP?

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.

Q2: How do I set different widths for FPDF table columns?

Store the widths in an array and use the matching array value whenever a Cell is created for that column.

Q3: How do I add borders around FPDF table cells?

Pass 1 as the border parameter of Cell() to draw a border around the cell.

Q4: How do I add a background color to the table header?

Set the color with SetFillColor() and pass true as the fill parameter for each header Cell.

Q5: How do I move to the next row after the final table 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.

Q6: Can I display MySQL or SQLite data inside an FPDF table?

Yes. Fetch the database records using PDO and loop through the returned rows while adding the values with Cell().

Q7: What should I use when text is too long for a normal Cell?

Use MultiCell() when the content must wrap onto multiple lines. A normal Cell is best suited to single-line values.


MultiCell Cell() Add Image to PDF

Database Records to PDF Table


Subscribe to our YouTube Channel here



plus2net.com







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.




PHP video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2026   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer