We will create a PDF table by reading records from the sample student database and displaying the values in rows and columns using FPDF. The same PDF-generation code can work with MySQL or SQLite when the database connection is created through PDO.
MySQL / SQLite
|
v
PDO connection
|
v
SELECT student records
|
v
PHP associative array
|
v
FPDF Cell()
|
v
PDF table
Learn how to create PDF tables using the FPDF Cell() method
Show Table of ContentsThe database connection is kept in config.php. Read the PHP PDO connection tutorial for the connection setup.
Once config.php creates the PDO object $dbo, the PDF script can use the same database-access code for MySQL or SQLite.
<?php
require "config.php";
The database-specific part is mainly the PDO connection string. The SELECT query and FPDF table-generation logic can remain the same for this example.
You can also read the SQL SELECT tutorial for more examples of retrieving database records.
We only need five columns for this PDF, so the query selects those columns instead of using SELECT *.
$sql="SELECT id,name,class,mark,gender
FROM student
ORDER BY id
LIMIT :limit";
$stmt=$dbo->prepare($sql);
$stmt->bindValue(':limit',10,PDO::PARAM_INT);
$stmt->execute();
$rows=$stmt->fetchAll(PDO::FETCH_ASSOC);
The value supplied to LIMIT is bound as an integer. The returned records are stored as associative arrays, so values can be accessed by column name:
$row['id']
$row['name']
$row['class']
$row['mark']
$row['gender']
Load FPDF, create the document and add the first page:
require 'fpdf.php';
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetAutoPageBreak(true,15);
The column widths used by this table are:
$width_cell=[20,50,40,40,40];
The total width is 190 mm:
20 + 50 + 40 + 40 + 40 = 190 mm
This fits the usable width of a normal A4 portrait page when the standard left and right margins are used.
Create the header cells:
$pdf->SetFont('Arial','B',14);
$pdf->SetFillColor(193,229,252);
$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,0,'C',true);
$pdf->Cell($width_cell[4],10,'GENDER',1,1,'C',true);
The last header uses 1 as the line-break argument so the next content starts on a new row.
Each associative array returned by PDO becomes one row in the PDF table.
foreach($rows as $row){
$pdf->Cell($width_cell[0],10,(string)$row['id'],1,0,'C',$fill);
$pdf->Cell($width_cell[1],10,(string)$row['name'],1,0,'L',$fill);
$pdf->Cell($width_cell[2],10,(string)$row['class'],1,0,'C',$fill);
$pdf->Cell($width_cell[3],10,(string)$row['mark'],1,0,'C',$fill);
$pdf->Cell($width_cell[4],10,(string)$row['gender'],1,1,'C',$fill);
$fill=!$fill;
}
The database values are converted to strings before being passed to Cell().
Set the fill color used for alternate rows:
$pdf->SetFillColor(235,236,236);
$fill=false;
At the end of every row, toggle the Boolean value:
$fill=!$fill;
The output alternates between an unfilled row and the configured background color, making database records easier to read.
The code uses Output('I','student-table.pdf') to display the generated PDF in the browser with a suggested filename.
The sample PDF generated from student records can be viewed here:
View PDF Outputfpdf.php with its required font files in the application directory used by this example.The project files are arranged so the database connection and PDF-generation code remain separate.
sql_dump.txt to create the sample student table in MySQL.config.php with the database connection details.index.php to display the records in the browser.index-pdf.php to generate the PDF table.Plus2net has examples using both PHP database interfaces:
This tutorial uses PDO for the main example because the same database-access structure can be used with both MySQL and SQLite by changing the connection configuration.
The project also supports SQLite. The SQLite version uses a PDO connection to the local database file, while the PDF table-generation logic remains the same.
See the PHP SQLite connection example for the connection setup.
The SQLite project includes create_table_sqlite.php, which creates the sample my_student.db database and student table used by the example.
This example intentionally uses only 10 records so the basic database-to-PDF process is easy to understand.
For larger datasets, the PDF may require multiple pages and repeated table headings. Continue with the dedicated tutorial:
Create Multi-page PDF Tables from Database RecordsThis keeps pagination logic separate from the basic example instead of making the first database-to-PDF tutorial unnecessarily complex.
A PDF response should be generated before HTML or other text is sent to the browser. Avoid echo, debugging output or extra whitespace before the PDF script calls Output().
Check the PDO settings in config.php and confirm that the student table is available in the selected MySQL or SQLite database.
Classic FPDF core fonts such as Arial do not provide complete Unicode coverage. If the database contains characters outside the supported encoding, use a suitable font/encoding setup before sending the text to FPDF.
Cell() uses a fixed width. For text that must wrap onto more than one line, continue with the FPDF MultiCell tutorial.
Add database values and variables to the PDF:
PDF using Variables and Database RecordsCreate enough pages to display a larger result set:
Multiple-page Database PDFLearn the basic table layout:
FPDF Table using Cell()Connect to MySQL using PDO, execute a SELECT query, create an FPDF document and loop through the returned records while adding each value with the Cell() method.
Yes. When PDO is used, the PDF-generation logic can remain the same. The main change is the database connection configuration used to connect to SQLite instead of MySQL.
The LIMIT value is numeric, so binding it as an integer ensures that PDO sends the value with the correct type instead of treating it as text.
Change the values in the column-width array and use the corresponding width for each Cell(). The combined widths should fit within the available page width.
Set a fill color with SetFillColor(), pass a Boolean fill value to Cell(), and toggle that Boolean after every database row.
Use the dedicated multi-page database PDF example, which extends the basic table and adds page handling for larger result sets.
Classic FPDF core fonts do not provide complete Unicode coverage. Use a compatible font and encoding setup when the database contains characters outside the supported character set.
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.
| Manik | 30-01-2019 |
| Thanks for sharing example. WE have used your code and we are using issue like not displaying the record in each row wise in the pdf based on your example. Can you please help how to disaplay each record in separate in the PDF. | |
| smo1234 | 08-02-2019 |
| There is a tutorial already there to display each record in a separate PDF | |
12-03-2021 | |
| thank you for sharing | |
07-02-2023 | |
| Hi there, How about if we text big in length, for example, one columns with 100 character, how can we deal with that? | |