How to fetch data from database in PHP and display in PDF
In this article, you will learn how to fetch data from the database and display it in PDF using PHP FPDF library.
Today, document security is the most important concern for sharing information over the web. A PDF is a read-only document that cannot be altered by users until they have the right electronic impression. By utilizing PDF, the associations can put username and password on a PDF level to secure document information. There is also demand for producing PDF dynamically increased by the associations, like generating an invoice, salary receipt, visiting card, etc. with a single click.
There are several PHP libraries available to generate PDF. In this article, we are using 'FPDF' to generate a PDF. The F from FPDF stands for Free. This PHP library is used to generate a PDF from UTF-8 encoded HTML. The FPDF contains high-level functions and is rich in features like image support, colour support, page compression, automatic page break, and link break. This library supports PHP version 5.1.
These are the steps to generate PDF to fetch data from database in PHP-
Download FPDF
Download the latest version of the FPDF library from its official website-
FPDF LibraryExtract the zip file into your project directory.
Now, let's create a main PHP file, 'index.php', that we will call in the browser. Include the FPDF library file at the top of this page.
require('fpdf/fpdf.php');
Make a Database Connection
Make sure to provide the right fpdf.php path in your main PHP file. After this, write the database connection code and make sure to replace 'hostname', 'username', 'password', and 'database' with your database credentials and name.
// Database Connection
$conn = new mysqli('hostname', 'username', 'password', 'database');
//Check for connection error
if($conn->connect_error){
die("Error in DB connection: ".$conn->connect_errno." : ".$conn->connect_error);
}
FPDF Class
Next, instantiate the fpdf class -
$pdf = new FPDF();
The FPDF library has many pre-defined methods. We have used these methods among others.
AddPage()- To add a new page.
SetFont()- To set the font.
Cell()- To print a cell.
Ln() - Line break.
Complete Code: index.php
Here, we have merged all the codes that were explained in detail above to generate PDF using the PHP FPDF library.
<?php
require('fpdf/fpdf.php');
// Database Connection
$conn = new mysqli('localhost', 'root', '', 'company');
//Check for connection error
if($conn->connect_error){
die("Error in DB connection: ".$conn->connect_errno." : ".$conn->connect_error);
}
// Select data from MySQL database
$select = "SELECT * FROM `empdata` ORDER BY id";
$result = $conn->query($select);
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',14);
while($row = $result->fetch_object()){
$id = $row->id;
$name = $row->name;
$address = $row->address;
$phone = $row->phone;
$pdf->Cell(20,10,$id,1);
$pdf->Cell(40,10,$name,1);
$pdf->Cell(80,10,$address,1);
$pdf->Cell(40,10,$phone,1);
$pdf->Ln();
}
$pdf->Output();
?>
When, you will call this on the browser, it will look something like this-
Related Articles
How to insert image in database using PHPPHP reverse a string without predefined function
PHP random quote generator
PHP convert string into an array
PHP remove HTML and PHP tags from string
Import Excel File into MySQL using PHP
PHP array length
Import Excel File into MySQL Database using PHP
PHP String Contains
How to fetch data from database in PHP and display in PDF
How to read CSV file in PHP and store in MySQL
How to create a doc file using PHP
PHP SplFileObject Standard Library
File upload in PHP MySQL database
Send HTML form data to email using PHP
Forgot password code in PHP mysqli
PHP form validation example
Ajax submit form without reloading page
PHP form validation example
PHP Emojis in subject and body lines
PHP 7.3 new features
Insert in database without page refresh PHP