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

users.sql

--
-- 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
) 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);

 
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);
}

?>

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


Leave a Reply

This Post Has 2 Comments

  1. Unknown

    how can i download or get vendor/autoload.php