php mysql pdf report generator – How to fetch data from database in PHP and display in PDF?

php mysql pdf report generator : FPDF PDF Generation from MySQL using PHP. we will use popular php library FPDF which will help us to generate PDF file.

php mysql pdf report generator

We will follow following steps to Generate PDF. How Generate pdf report/ portfolio from database using php mysql fpdf.

Step 1: Create Databse structure & insert Data

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,
  `department` 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`, `department`, `role`, `created_on`) VALUES
(5, 'monika', '[email protected]', 'Admin', 'admin', '2022-06-29 03:42:04'),
(6, 'leela', '[email protected]', 'Web Development', 'member', '2022-06-29 03:42:20'),
(7, 'Janki', '[email protected]','Web Development', 'member', '2022-07-05 18:27:24'),
(8, 'Bharti', '[email protected]', 'Manager', 'member', '2022-07-05 18:27:44'),
(9, 'Sejal', '[email protected]', 'Manager', 'member', '2022-07-05 18:28:03');

Step 2: Create a connection PHP with MySQL Database

dbconfig.php




Step 3: Generate PDF from Mysql Database using PHP

generate-member-pdf.php


Image('https://pakainfo.com/wp-content/uploads/2022/01/pakainfo-LOGO-1.png',10,10,50);
    $this->SetFont('Arial','B',13);

    $this->Cell(80);

    $this->Cell(80,10,'Member List',1,0,'C');

    $this->Ln(20);
}

function Footer()
{

    $this->SetY(-15);

    $this->SetFont('Arial','I',8);

    $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
}

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

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

$my_pdf = new PDF();

$my_pdf->AddPage();

$my_pdf->AliasNbPages();
$my_pdf->SetFont('Arial','B',16);
foreach($header as $heading) {
	$my_pdf->Cell(35,10,$display_heading[$heading['Field']],1);
}

foreach($output as $row) {
	$my_pdf->SetFont('Arial','',10);
	$my_pdf->Ln();
	foreach($row as $clm)
	$my_pdf->Cell(35,10,$clm,1);
}
$my_pdf->Output();
?>

Don’t Miss : Generating PDF reports in PHP

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

Leave a Comment