How to Generate PDF from Mysql Database using PHP?

Today, We want to share with you export mysql data to pdf using php.In this post we will show you How to Generate PDF from Mysql Database using PHP?, hear for popular php library FPDF we will give you demo and example for implement.In this post, we will learn about PHP How-To Create Dynamic PDF Files Using FPDF with an example.

Q: how to generate pdf file in php with demo?

Export data mysql table to csv file using PHP
Export data mysql table to csv file using PHP

generate pdf from php data retrieve from mysql

members.sql

--
-- Table structure for table `members`
--

CREATE TABLE IF NOT EXISTS `members` (
  `member_id` int(255) NOT NULL AUTO_INCREMENT,
  `name` varchar(1000) NOT NULL,
  `email` varchar(1000) NOT NULL,
  `section` varchar(1000) NOT NULL,
  `role` varchar(100) NOT NULL,
  `created_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`member_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

--
-- Dumping data for table `members`
--

INSERT INTO `members` (`member_id`, `name`, `email`, `section`, `role`, `created_on`) VALUES
(5, 'bhavik', '[email protected]', 'Admin', 'admin', '2021-06-29 03:42:04'),
(6, 'sejal', '[email protected]', 'Angularjs', 'member', '2021-06-29 03:42:20'),
(7, 'dipti', '[email protected]','Angularjs', 'member', '2021-07-05 18:27:24'),
(8, 'Komal', '[email protected]', 'PHP', 'member', '2021-07-05 18:27:44'),
(9, 'Dhara', '[email protected]', 'PHP', 'member', '2021-07-05 18:28:03');

dbconfig.php

<?php
session_start();
$host="localhost";
$username="app_v1";
$pass="[email protected]";
$db="pakainfo";

$conn=mysqli_connect($host,$username,$pass,$db);
if(!$conn){
	die("Sorry, Database connection error");
}


?>

generate-member-pdf.php

<?php
//include connection file 
include "dbconfig.php";
include_once('pdf/fpdf.php');

class PDF extends FPDF
{
// Page header
function Header()
{
    // Pakainfo Logo
    $this->Image('https://www.pakainfo.com/wp-content/uploads/2021/01/pakainfo-LOGO-1.png',10,10,50);
    $this->SetFont('Arial','B',13);
    // Move to the right
    $this->Cell(80);
    // Title
    $this->Cell(80,10,'Employee List',1,0,'C');
    // Line break
    $this->Ln(20);
}

// Page footer
function Footer()
{
    // Position at 1.5 cm from bottom
    $this->SetY(-15);
    // Arial italic 8
    $this->SetFont('Arial','I',8);
    // Page number
    $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
}

$display_heading = array('member_id'=>'ID', 'name'=> 'Name', 'email'=> 'Email','section'=> 'Section','role'=> 'Role');

$result = mysqli_query($conn, "SELECT member_id, name, email, section, role FROM members") or die("database error:". mysqli_error($conn));
$header = mysqli_query($conn, "SHOW columns FROM members WHERE field != 'created_on'");

$pdf = new PDF();
//header
$pdf->AddPage();
//foter page
$pdf->AliasNbPages();
$pdf->SetFont('Arial','B',16);
foreach($header as $heading) {
$pdf->Cell(35,10,$display_heading[$heading['Field']],1);
}
foreach($result as $row) {
$pdf->SetFont('Arial','',10);
$pdf->Ln();
foreach($row as $column)
$pdf->Cell(35,10,$column,1);
}
$pdf->Output();
?>

I hope you get an idea about generate pdf from php data retrieve from mysql.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.