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.
<?Php
require "config.php";//connection to database
//SQL to get 10 records
$sql="select * from student LIMIT 0,10";
require('fpdf.php');
$pdf = new FPDF(); 
$pdf->AddPage();

$width_cell=array(20,50,40,40,40);
$pdf->SetFont('Arial','B',16);

//Background color of header//
$pdf->SetFillColor(193,229,252);

// Header starts /// 
//First header column //
$pdf->Cell($width_cell[0],10,'ID',1,0,'C',true);
//Second header column//
$pdf->Cell($width_cell[1],10,'NAME',1,0,'C',true);
//Third header column//
$pdf->Cell($width_cell[2],10,'CLASS',1,0,'C',true); 
//Fourth header column//
$pdf->Cell($width_cell[3],10,'MARK',1,0,'C',true);
//Third header column//
$pdf->Cell($width_cell[4],10,'GENDER',1,1,'C',true); 
//// header ends ///////

$pdf->SetFont('Arial','',14);
//Background color of header//
$pdf->SetFillColor(235,236,236); 
//to give alternate background fill color to rows// 
$fill=false;

/// each record is one row  ///
foreach ($dbo->query($sql) as $row) {
$pdf->Cell($width_cell[0],10,$row['id'],1,0,'C',$fill);
$pdf->Cell($width_cell[1],10,$row['name'],1,0,'L',$fill);
$pdf->Cell($width_cell[2],10,$row['class'],1,0,'C',$fill);
$pdf->Cell($width_cell[3],10,$row['mark'],1,0,'C',$fill);
$pdf->Cell($width_cell[4],10,$row['gender'],1,1,'C',$fill);
//to give alternate background fill  color to rows//
$fill = !$fill;
}
/// end of records /// 

$pdf->Output();
?>
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)


MultiCell Cell()
Adding Image to PDF File
Subscribe to our YouTube Channel here

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    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

    Post your comments , suggestion , error , requirements etc here




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