Php

Generate a PDF file using the JsPDF and PHP

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:

Related Post
  • 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"
            >



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.

Recent Posts

Configure and Retrieve S3 Information Using Laravel PHP-AWS-SDK

This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More

2 months ago

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

8 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

8 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

9 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

9 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

9 months ago

Categories