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` ( `id` int(11) NOT NULL PRIMARY KEY AUTO_INCREAMENT, `name` varchar(50) NOT NULL, `username` varchar(50) NOT NULL, `email` varchar(50) NOT NULL, `age` int(50) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -- -- Dumping data for table `users` -- INSERT INTO `users` (`id`, `name`, `username`, `email`, `age`) VALUES (1, 'john', 'john12', 'john@gmail.com', 32), (2, 'roman', 'roman12', 'roman@gmail.com', 34), (3, 'undertaker', 'undertaker12', 'undertaker@gmail.com', 56), (4, 'kane', 'kane12', 'kane@gmail.com', 54), (5, 'aj style', 'ajstyle12', 'ajstyle@gmail.com', 32), (6, 'seth', 'seth12', 'seth@gmail.com', 34), (7, 'dean', 'dean123', 'dean@gmail.com', 32), (8, 'jindal', 'jindal25', 'jindal@gmail.com', 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.
- Upload MPDF file into your application Folder.
- 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:
dbConfig.php
<?php // 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
index.php
<!DOCTYPE html> <html lang="en"> <head> <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="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script> </head> <body> <div class="jumbotron text-center"> <h1>Generate PDF File From MySQL Database Using PHP</h1> </div> <div class="container"> <div class="row"> <div class="col-md-12 col-sm-12"> <?php require_once('dbConfig.php'); require_once 'vendor/autoload.php'; $query = "SELECT * FROM users"; $result = mysqli_query($con, $query); $output = ""; $output .="<table class='table table-striped'> <thead> <tr> <th>Id</th> <th>Name</th> <th>Username</th> <th>Email</th> <th>Age</th> </tr> </thead>"; if (mysqli_num_rows($result) > 0) { while ($row = mysqli_fetch_assoc($result)) { $output.='<tbody> <tr> <td> '.$row['id'].' </td> <td> '.$row['name'].' </td> <td> '.$row['username'].' </td> <td> '.$row['email'].' </td> <td> '.$row['age'].' </td> </tr> </tbody>'; } }else{ $output = "No record found"; } $output .="</table>"; $mpdf = new \Mpdf\Mpdf(); $mpdf->WriteHTML($output); $fileName = rand().'.pdf'; $mpdf->Output($fileName, 'D'); ?> </div> </div> </div> </body> </html>
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.😊
how can i download or get vendor/autoload.php
you go to this https://github.com/mpdf/mpdf link and download it and include in your file.
But the vendor/autoload.php is missing the the GitHub link itself
please give me your email id I will send your source code
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.
Now solve the download issue. you can download full source code