PHP Generate PDF from MySQL Database using FPDF

PHP Generate PDF from MySQL Database using FPDF

In this Post We Will Explain About is PHP Generate PDF from MySQL Database using FPDF With Example and Demo.

Welcome on Pakainfo.com – Examples ,The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to How to Generate PDF from MySQL Data using PHP

In this post we will show you Best way to implement Generate PDF using PHP from MySQL database, hear for How to generate pdf from php data retrieve from mysql with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

FPDF has other More Basic advantages: high level all the PHP fpdf functions.

  • PHP to Choice of measure unit simple, page format new and margins
  • PHP to Page header and footer data management
  • PHP to Automatic all the page break
  • PHP to Automatic line as break and simple text justification
  • PHP to Image support Like as a(JPEG, PNG and GIF)
  • PHP to Colors
  • PHP to Links
  • PHP to TrueType,simple Type1 and all the data encoding support
  • PHP to Page more compression
Read Also:  VueJS onload Function Call Example with Demo

The following snippet show a basic PDF generated with FPDF :

<?php require('fmypdf/fmypdf.php');
$mypdf = new FPDF();
$mypdf->AddPage();
$mypdf->SetFont('Arial','B',12);	
$mypdf->Cell(46,12,$column_heading,1);
$mypdf->Output();
?>

Structure of students

CREATE TABLE IF NOT EXISTS `students` (
 `studId` int(11) NOT NULL,
 `StudName` varchar(100) DEFAULT NULL,
 `StudDepart` varchar(100) DEFAULT NULL,
 `StudDate` timestamp NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1;
 
INSERT INTO `students` (`studId`, `StudName`, `StudDepart`, `StudDate`) VALUES
(1, 'Mayur Dhameliya', 'Development', '2019-06-08 01:47:41'),
(2, 'Gordhan Padya ', 'HR', '2019-07-02 09:47:41'),
(3, 'Sejal Ramnai', 'Professors', '2016-10-14 12:48:31'),
(4, 'Magan Odedara', 'Designing', '2019-07-01 09:48:31'),
(5, 'Deepa Patel', 'Support', '2019-06-14 09:49:00'),
(6, 'Manshi dharmi', 'Operations', '2019-07-01 04:49:00');
ALTER TABLE `students`
 ADD PRIMARY KEY (`studId`);
ALTER TABLE `students`
 MODIFY `studId` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=7;

genratemypdf.php page

<?php
require_once("config.php");
require('fmypdf/fmypdf.php');
$mypdf = new FPDF();
$mypdf->AddPage();
 
// code for print Heading of Simple tables
$mypdf->SetFont('Arial','B',12);	
$ret ="SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA`='studdate' AND `TABLE_NAME`='students'";
$sql_query1 = $dbh -> prepare($ret);
$sql_query1->execute();
$header=$sql_query1->fetchAll(PDO::FETCH_OBJ);
$cnt=1;
if($sql_query1->rowCount() > 0)
{
	foreach($header as $heading) 
	{
		foreach($heading as $column_heading)
		$mypdf->Cell(46,12,$column_heading,1);
	}
}
//code for print data
$sql = "SELECT * from  students ";
$sql_query = $dbh -> prepare($sql);
$sql_query->execute();
$results=$sql_query->fetchAll(PDO::FETCH_OBJ);
$cnt=1;
if($sql_query->rowCount() > 0)
{
	foreach($results as $row) 
	{
		$mypdf->SetFont('Arial','',12);	
		$mypdf->Ln();
		foreach($row as $column)
		$mypdf->Cell(46,12,$column,1);
	} 
}
$mypdf->Output();
?> 

Example

I hope you have Got Generate PDF using PHP from MySQL database And how it works.I would Like to have FeadBack From My Blog(Pakainfo.com) readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article(Pakainfo.com) Are Most Always Welcome.