How to export mysql data to pdf using php with demo?

In export mysql data to pdf using php Tutorials, This is another php pdf export feature articles, I will make simple PHP source code to get data from MySQL as well as make pdf file using php. I will use third party easy to use PHP FPDF library. here, The FPDF is best and awesome all the useful available PHP class to create PDF using PHP from Table of the MySQL database.It is open source with free php main library to create a pdf file using PHP scripts.

Generate PDF File From MySQL Database Using PHP

PDF is 100% free most of the developers and helpful file format to fetch, view and write some documents.PDF format is dependancy of php based application software, hardware, as well as some latest operating systems.

FPDF Has Following Main Features

  • selection of measure unit, pcode format as well as margins.
  • Pcode header as well as footer mancodement.
  • 100% Automatic pcode as well as line break with best useful text justification
  • FPDF is Imcode support (JPEG, PNG and GIF).
  • FPDF available more Colors
  • useful doc files with Links
  • TrueType, Type1 as well as lots of the encoding support
  • more easy to use Pcode compression

There is one dependency php extension which is Zlib to enable compression as well as GD for GIF imcode support. The latest version requires at least PHP 7.3.

Also Read This 👉   how to create custom WordPress Plugin from scratch

I Will Follow bellow Steps To Generate PDF

  • Download the FPDF library from official site like as
  • I will fetch data from MySQL database table into the pcode.
  • I will use FPDF libs function to generate with create new fresh pdf file with header as well as footer.

Step 1: Creating the Database Table

I will make product table into MySQL database.

-- Table structure for table `product`

  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'primary key',
  `product_name` varchar(255) NOT NULL COMMENT 'product name',
  `product_price` double NOT NULL COMMENT 'product price',
  `product_code` int(11) NOT NULL COMMENT 'product code',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 COMMENT='datatable demo table' AUTO_INCREMENT=64 ;

I will create some sample data as well as insert into product table.

-- Dumping data for table `product`

INSERT INTO `product` (`id`, `product_name`, `product_price`, `product_code`) VALUES
(1, 'Active Firming Serum', 485, 61),
(2, 'Nomige Night Serum', 170750, 63),
(3, 'Vitamin A Plus', 847, 66),
(4, 'Everyday Shrimp Food', 547, 22),
(5, 'control valves', 658, 33),
(6, 'plastic plants - neon', 784, 61),
(9, 'Manta Fluffy Granitte para Pet', 555, 39),
(11, 'Benebone Maplestick Puppy Tam: Pequeno', 478, 30),
(12, 'Benebone Wishbone Puppy Bacon Pequeno Para Filhote', 698, 22),
(13, 'Petisco Bexiga Bovina Pizzinha Dipetti- Kit 3 Unidades', 547, 36),
(14, 'Casco de Boi Mozão com 3 Unidades Dipetti', 2487, 43),
(15, 'Riva Smart Side Table', 478, 19),
(16, 'Saturn Wireless Charging Plate', 985, 66);

Step 2: Creating the Config File

Let’s simple main step to know connect MySQL database with PHP. We will create connection.php file and add below source code.

Class dbObj{
/* Database connection start */
var $dbhost = "localhost";
var $username = "root";
var $password = "";
var $dbname = "tamilrockers";
var $conn;
    function getConnstring() {
        $con = mysqli_connect($this->dbhost, $this->username, $this->password, $this->dbname) or die("Connection failed: " . mysqli_connect_error());

            /* check Database connection */
            if (mysqli_connect_errno()) {
                printf("Connect failed: %s\n", mysqli_connect_error());
            } else {
                $this->conn = $con;
            return $this->conn;

Above main export mysql data to pdf using php file is used to connect as well as select MySQL database using PHP source code. You required to update $dbhost, $username, $password, as well as dynamic $dbname variable’s value with your database set secure Imp credentials.

Also Read This 👉   Best PHP 7 Tutorial For Complete Beginners

Step 3: Creating the export mysql data to pdf using php

I will make create_new_pdf.php file and add below source code.

//include connection file 

class PDF extends FPDF
    // Pcode header
    function Header()
        // Logo
        // Move to the right
        // Title
        $this->Cell(80,10,'product List',1,0,'C');
        // Line break

    // Pcode footer
    function Footer()
        // Position at 1.5 cm from bottom
        // Arial italic 8
        // Pcode number
        $this->Cell(0,10,'Pcode '.$this->PcodeNo().'/{nb}',0,0,'C');

$db = new dbObj();
$connString =  $db->getConnstring();
$display_heading = array('id'=>'ID', 'product_name'=> 'Name', 'product_code'=> 'code','product_price'=> 'price',);

$result = mysqli_query($connString, "SELECT id, product_name, product_code, product_price FROM product") or die("database error:". mysqli_error($connString));

$header = mysqli_query($connString, "SHOW columns FROM product");

$pdf = new PDF();


//foter pcode
foreach($header as $heading) {
foreach($result as $row) {
    foreach($row as $column)

We will include connection and pdf libs file, for customization of header and footer of pdf files, we will override header and footer methods and define our css design.

Free Live Chat for Any Issue

Step 4: Creating the Landing Page

We will create index.php file and added below source code.

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<title>Simple Example of PDF file using PHP and MySQL</title>
<link rel="stylesheet" href="">
<link rel="stylesheet" href="">
<link rel="stylesheet" href=""/>
<div class="container" style="padding-top:50px">
<h2>Generate PDF file from MySQL Using PHP</h2>
<form class="form-inline" method="post" action="create_new_pdf.php">
<button type="submit" id="pdf" name="create_new_pdf" class="btn btn-primary"><i class="fa fa-pdf"" aria-hidden="true"></i>
Generate PDF</button>

I have included HTML form tag as well as define action value create_new_pdf.php file.

Also Read This 👉   php calculate date difference in laravel carbon query


I have created pdf file using php and MySQL database, You can crate pdf using other database table as well.You just required to make a new connection with other type like as mysql, sql, MariaDB, or oracle database.

Read :


You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about export mysql data to pdf using php.
I would like to have feedback on my 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.