PDF Table by taking data from MySQL database

PDF Student Table created using PHP
We will take records from our student database and then crate a PDF document by using the data. Records will be displayed in a tabular format.

How to create tables in PDF document by using Cell function

Displaying Data from MySQL table in PDF document by using Cell with alternate background color

In above tutorial it is explained how to draw tables, we will use the same concepts to display data with column headers to show the records.

Steps involved are

Connect to database,
Run SQL to collect records
Display in data inside a table and generate PDF document.

You can download the ZIP file containing all the above steps. Inside ZIP folder these files are used.
config.php :
Database connection details
index.php :
Shows the records in browser by using Query and PHP Script. ( No PDF is created here )
index-pdf.php :
Display the pdf file with records
sql_dump.txt :
contains SQL dump to create your student table at your local database.
readme.txt :
Help file with links to different solutions.

How to connect and collect the records from table

Read more about SELECT query here. You can read more on database connection using PHP PDO. The full code to generate PDF document with data from tables is here.
require "config.php";//connection to database
//SQL to get 10 records
$sql="select * from student LIMIT 0,10";
$pdf = new FPDF(); 


//Background color of header//

// Header starts /// 
//First header column //
//Second header column//
//Third header column//
//Fourth header column//
//Third header column//
//// header ends ///////

//Background color of header//
//to give alternate background fill color to rows// 

/// each record is one row  ///
foreach ($dbo->query($sql) as $row) {
//to give alternate background fill  color to rows//
$fill = !$fill;
/// end of records /// 

PDF Output with records is here

Download and Install script

Download and Install fpdf class from https://www.fpdf.org/
Keep a copy of fpdf.php file in the same directory
Keep the font directory inside in the same directory.

  • Use the SQL_dump.txt file to create student table in your MySQL database
  • Open config.php file to enter your MySQL login details.
  • Open index.php file to see the records in your browser ( Not PDF ).
  • Open index-pdf.php file to generate PDF document.
  • Open index1-pdf.php file to generate PDF document with link to breakup of marks.

Connecting database and executing Query

To manage data we have to connect to MySQL database and execute query to get our date. Here there are two ways to use PHP drivers to connect to MySQL and execute the functions for getting records.

One is using Portable Data Object ( PDO )
Second one is MySQLI ( MysQL Improved )

You can download both the scripts inside the same Zip file. Inside MySQLI folder you can get same scripts with MySQLi connection. ( change the config.php file here also and place fpdf.php with font directory inside this folder)

Create PDF by using variables and records from database


MultiCell Cell()
Adding Image to PDF File

Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net




    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.


    There is a tutorial already there to display each record in a separate PDF


    thank you for sharing


    Hi there,

    How about if we text big in length, for example, one columns with 100 character, how can we deal with that?

    Post your comments , suggestion , error , requirements etc here

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