Web Development

How to Generate PDF from MySQL Data using PHP

In this tutorial I explains how to generate PDF from MySQL Data using PHP. You are going to see how to convert MySQL Data into PDF using FPDF library.

What id FPDF ?
FPDF is a PHP class which allows to generate PDF files with  PHP, that is to say without using the PDFlib library. F from FPDF stands for Free: you may use it for any kind of usage and modify it to suit your needs.
FPDF has other advantages: high level functions. Here is a list of its main features:

  • Choice of measure unit, page format and margins
  • Page header and footer management
  • Automatic page break
  • Automatic line break and text justification
  • Image support (JPEG, PNG and GIF)
  • Colors
  • Links
  • TrueType, Type1 and encoding support
  • Page compression

The following snippet show a basic PDF generated with FPDF :

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

First a db with empadata. Under db empdata create table tblemployee-
Structure of tblemployee

CREATE TABLE IF NOT EXISTS `tblemployee` (
 `EmpId` int(11) NOT NULL,
 `EmpName` varchar(100) DEFAULT NULL,
 `EmpDepartment` varchar(100) DEFAULT NULL,
 `EmpRegDate` timestamp NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1;
INSERT INTO `tblemployee` (`EmpId`, `EmpName`, `EmpDepartment`, `EmpRegDate`) VALUES
(1, 'Anuj kumar', 'Development', '2017-06-08 01:47:41'),
(2, 'Amit ', 'HR', '2017-07-02 09:47:41'),
(3, 'Garima', 'Accountant', '2016-10-14 12:48:31'),
(4, 'John', 'Designing', '2017-07-01 09:48:31'),
(5, 'Rahul', 'Support', '2017-06-14 09:49:00'),
(6, 'Sanjeev', 'Operations', '2017-07-01 04:49:00');
ALTER TABLE `tblemployee`
 ADD PRIMARY KEY (`EmpId`);
ALTER TABLE `tblemployee`
 MODIFY `EmpId` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=7;

genratepdf.php page

<?php
require_once("config.php");
require('fpdf/fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
// code for print Heading of tables
$pdf->SetFont('Arial','B',12);
$ret ="SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA`='empdata' AND `TABLE_NAME`='tblemployee'";
$query1 = $dbh -> prepare($ret);
$query1->execute();
$header=$query1->fetchAll(PDO::FETCH_OBJ);
$cnt=1;
if($query1->rowCount() > 0)
{
foreach($header as $heading) {
foreach($heading as $column_heading)
$pdf->Cell(46,12,$column_heading,1);
}}
//code for print data
$sql = "SELECT * from  tblemployee ";
$query = $dbh -> prepare($sql);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
$cnt=1;
if($query->rowCount() > 0)
{
foreach($results as $row) {
$pdf->SetFont('Arial','',12);
$pdf->Ln();
foreach($row as $column)
$pdf->Cell(46,12,$column,1);
} }
$pdf->Output();
?>
View Demo
Download Source Code (How to Generate PDF from MySQL Data using PHP)
Size: 2.58 KB
Version: V 1.0

Recommended Tutorials for you


Related posts

How to prevent Cross-Site Request Forgery (CSRF) in PHP

Anuj Kumar

How to get current page URL in php

Anuj Kumar

How to fetch data in excel or generate excel file in PHP

Anuj Kumar

How to encrypt password on client side using Javascript

Anuj Kumar

How to create pagination in PHP

Anuj Kumar

PHP Email Verification Script

Anuj Kumar

How to convert html to Pdf using Dompdf in PHP

Anuj Kumar

How to Salt & Hash a Password using Sha256

Anuj Kumar

18 comments

nur November 28, 2018 at 4:34 am

Hi Mr. Anuj Kumar, is there any ways that i can contact you often for asking about php?

Reply
Anuj Kumar November 28, 2018 at 4:50 am

You can contact at phpgurukulofficial@gmail.com

Reply
Akshay Srivastava November 10, 2018 at 5:17 am

do i have to create a schema first

Reply
Anuj Kumar November 10, 2018 at 5:33 am

yes

Reply
Makama Umar November 10, 2018 at 5:17 am

thanks

Reply
priya November 10, 2018 at 5:17 am

hi….thanks u the code is working…but how to change a particular cell width in this…

Reply
Anuj Kumar November 10, 2018 at 5:34 am

make changes in this line $pdf->Cell(46,12,$column_heading,1);

Reply
priya November 10, 2018 at 6:03 am

Hi anuj,Thank you for ur reply… while changing with $pdf->Cell(46,12,$column_heading,1); all the cells width is increased …i need to increase the cell width of the last cell alone … any suggestions

Reply
me November 10, 2018 at 5:17 am

how to make page landscape

Reply
Anuj Kumar November 10, 2018 at 5:33 am

Visit this tutorial—https://phpgurukul.com/how-to-convert-html-to-pdf-using-dompdf-in-php/

Reply
priya November 28, 2017 at 12:02 pm

hi….thanks u the code is working…but how to change a particular cell width in this…

Reply
Anuj Kumar November 28, 2017 at 4:01 pm

make changes in this line $pdf->Cell(46,12,$column_heading,1);

Reply
priya November 29, 2017 at 6:38 am

Hi anuj,Thank you for ur reply… while changing with $pdf->Cell(46,12,$column_heading,1); all the cells width is increased …i need to increase the cell width of the last cell alone … any suggestions

Reply
Akshay Srivastava November 26, 2017 at 1:34 pm

do i have to create a schema first

Reply
Anuj Kumar November 26, 2017 at 3:12 pm

yes

Reply
me October 26, 2017 at 8:22 am

how to make page landscape

Reply
Anuj Kumar October 26, 2017 at 10:00 am

Visit this tutorial—https://phpgurukul.com/how-to-convert-html-to-pdf-using-dompdf-in-php/

Reply
Makama Umar October 17, 2017 at 3:10 pm

thanks

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.