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

Create Database and Table by SQL query

In first step, we need to create database and table, so here I created registration database and users table. You can simply create users table as following SQL query.

-- Database: `registration`

-- Table structure for table `users`

CREATE TABLE `users` (
  `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);

Download the MPDF library

mPDF is a PHP library which generates PDF files from UTF-8 encoded HTML.

It is based on FPDF and HTML2FPDF with a number of enhancements.

The original author, Ian Back, wrote mPDF to output PDF files ‘on-the-fly’ from his website, handling different languages. It is slower than the original scripts e.g. HTML2FPDF and produces larger files when using Unicode fonts, but support for CSS styles etc. and has been much enhanced – see the features.

  1. Upload MPDF file into your application Folder.
  2. Include the autoload.php file into your application file.

Create Database connection File

In this step, we require to create database configuration file, here we will set database name, username and password. So let’s create dbConfig.php file on your root directory and put bellow code:


	// 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 PDF HTML Form Page


<!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 \Mpdf\Mpdf();
        $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 or problems about this tutorial, please comment on the  form below.😊

Generate PDF File From MySQL Database Using PHP


Leave a Reply

Your email address will not be published. Required fields are marked *

  1. Hello, this is a good article, but I can't download the source code. Google sais "We're sorry, but you do not have access to this page. That’s all we know."

    Also there is no autoload.php from the github.