Generate PDF File From MySQL Database Using PHP

Generate PDF File From MySQL Database Using PHP

In this post I will explain How to generate PDF file from Mysql database using PHP. To generate the PDF file with mysql data content and output. we will use most popular PHP library MPDF which help us to generate pdf file.
If you want to install this MPDF library using command prompt go to our project directory and hit this command.
$ composer require mpdf/mpdf

Step by Step Process to Generate PDF using MPDF

Step-1 Select the data from MySQL database into the page
Step-2 Download the MPDF library from MPDF
Step-3 Upload MPDF file into your application Folder
Step-4 Include the autoload.php file into your application file

Create Database and Table

-- Database: `registration`

-- Table structure for table `users`

CREATE TABLE `users` (
  `id` int(11) NOT NULL,
  `name` varchar(50) NOT NULL,
  `username` varchar(50) NOT NULL,
  `email` varchar(50) NOT NULL,
  `age` int(50) NOT NULL

-- Dumping data for table `users`

INSERT INTO `users` (`id`, `name`, `username`, `email`, `age`) VALUES
(1, 'john', 'john12', '', 32),
(2, 'roman', 'roman12', '', 34),
(3, 'undertaker', 'undertaker12', '', 56),
(4, 'kane', 'kane12', '', 54),
(5, 'aj style', 'ajstyle12', '', 32),
(6, 'seth', 'seth12', '', 34),
(7, 'dean', 'dean123', '', 32),
(8, 'jindal', 'jindal25', '', 34);

Create Database Configuration File

	// Database configuration 
	$dbHost     = "localhost"; 
	$dbUsername = "root"; 
	$dbPassword = ""; 
	$dbName     = "registration"; 
	// Create database connection 
	$con = mysqli_connect($dbHost, $dbUsername, $dbPassword, $dbName); 
	// Check connection 
	if ($con->connect_error) { 
	    die("Connection failed: " . $con->connect_error); 

Create HTML Page for Generate PDF File

<!DOCTYPE html>
<html lang="en">
  <title>Generate PDF File From MySQL Database Using PHP</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="">
  <script src=""></script>
  <script src=""></script>
  <script src=""></script>

<div class="jumbotron text-center">
  <h1>Generate PDF File From MySQL Database Using PHP</h1>
<div class="container">
  <div class="row">
    <div class="col-md-12 col-sm-12">
        require_once 'vendor/autoload.php';
        $query = "SELECT * FROM users";

        $result = mysqli_query($con, $query); 

        $output = "";

 $output .="<table class='table table-striped'>
      if (mysqli_num_rows($result) > 0) { 
      while ($row = mysqli_fetch_assoc($result)) {
                    <td> '.$row['id'].' </td>
                    <td> '.$row['name'].' </td>
                    <td> '.$row['username'].' </td>
                    <td> '.$row['email'].' </td>
                    <td> '.$row['age'].' </td>
            $output = "No record found";
        $output .="</table>";
        $mpdf = new MpdfMpdf();
        $fileName = rand().'.pdf';
        $mpdf->Output($fileName, 'D');


You can always support by sharing on social media or recommending my blog to your friends and colleagues.
If you have any suggestions / problems about this tutorial, please comment on the  form below.😊

Leave a Reply

Your email address will not be published.