In this article, we’ll show you how to create a PDF file from data pulled from a database. I’m using MySQL and the PHP core with jQuery.
We will use jsPDF library for this purpose. You can download the newest library version just from the GitHub repository or from the official website.
Generate pdf Using jspdf using PHP
Let’s start Xampp server and create a database with a table as you like. I have a database table with some employee information here. I need to create a PDF file after first showing a list of every employee on my website. The entire employee list will be in the pdf file.
Prerequisites
We have to download the newest version of the library and include it in the HEAD or at the end of the BODY section.
<head> <script src="js/jspdf.js"></script> </head>
There are following files will participate in this sample project:
- database.php: This file is used to connect the MySQL database to your application.
- index.php: This file ll have all PHP code to display the table and generate a PDF file.
MySQL Table with Dummy Data
CREATE TABLE tbl_employee ( id int(11) NOT NULL, name int(11) NOT NULL, age int(11) NOT NULL, salary int(11) NOT NULL ); -- -- Dumping data for table tbl_employee INSERT INTO tbl_employee (id, name, age, salary) VALUES (1, 'Tim', 23, 2000), (2, 'Mini', 34, 3400) (2, 'Amin', 34, 2300); ALTER TABLE tbl_employee ADD PRIMARY KEY (id); ALTER TABLE tbl_employee MODIFY id int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=11;
PHP 7 Database Connection With MySQL
We’ll add the below code to the database.php
file.
$servername = "localhost"; $username = "root"; $password = ""; $dbname = "test"; $conn = mysqli_connect($servername, $username, $password, $dbname) or die("Connection failed: " . mysqli_connect_error()); /* check connection */ if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); }
How to generate PDF Using PHP
Let’s write the below code index.php
file.
<?php require_once __DIR__ . '/database.php'; $sql = "SELECT * FROM tbl_employee"; $result = mysqli_query($conn, $sql); ?> <html> <title>Employee</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/3.5.6/jspdf.plugin.autotable.min.js"></script> <script src="assets/js/jspdf-autotable-custom.js"></script> <link href="assets/css/style.css" rel="stylesheet" type="text/css" /> </head> <body> <div class="container"> <h2 class="text-center heading">Employee detailed list</h2> <table class="table" id="emp-table"> <tr class="border"> <th class="text-align">SL.No</th> <th>Name</th> <th>Age</th> <th>Salary</th> </tr> <?php while($row = mysqli_fetach_array($result)) {?> <tr class="content border"> <td><?php echo $row["id"];?></td> <td><?php echo $row["name"];?></td> <td><?php echo $row["age"];?></td> <td><?php echo $row["salary"];?></td> </tr> <?php }?> </table> <input type="button" class="export-button" onclick="generateTable()" value="Generate PDF" /> </div> </body> </html>
Let’s create a javascript method to generate a table:
function generateTable() { var doc = new jsPDF('p', 'pt', 'letter'); var y = 20; doc.setLineWidth(2); doc.text(200, y = y + 30, "Product detailed report"); doc.autoTable({ html: '#product-table', startY: 70, theme: 'grid', columnStyles: { 0: { halign: 'right', tableWidth: 100, }, 1: { tableWidth: 100, }, 2: { halign: 'right', tableWidth: 100, }, 3: { halign: 'right', tableWidth: 100, } }, }) doc.save('auto_table_pdf'); }
This script shows the generateTable() function and prepares the title and the table data to display in the PDF. The above method parsing the HTML table source, it outputs a PDF document that can be downloaded and saved.