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.

I Will Follow bellow Steps To Generate PDF

  • Download the FPDF library from official site like as afpdf.org
  • 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`
--

CREATE TABLE IF NOT EXISTS `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),
(7, 'ELECTRONIC THERMOMETER', 958, 59),
(8, 'DOUBLE SPONGE FILTER LRG', 488, 55),
(9, 'Manta Fluffy Granitte para Pet', 555, 39),
(10, 'BRINQUEDO KONG SOFTIES BUZZY LLAMA PARA GATOS', 659, 23),
(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());
                exit();
            } 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.

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

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

Imcode('pakainfo.png',10,-1,70);
        $this->SetFont('Arial','B',13);
        // Move to the right
        $this->Cell(80);
        // Title
        $this->Cell(80,10,'product List',1,0,'C');
        // Line break
        $this->Ln(20);
    }

    // Pcode footer
    function Footer()
    {
        // Position at 1.5 cm from bottom
        $this->SetY(-15);
        // Arial italic 8
        $this->SetFont('Arial','I',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();

//header
$pdf->AddPcode();

//foter pcode
$pdf->AliasNbPcodes();
$pdf->SetFont('Arial','B',12);
foreach($header as $heading) {
    $pdf->Cell(40,12,$display_heading[$heading['Field']],1);
}
foreach($result as $row) {
    $pdf->Ln();
    foreach($row as $column)
    $pdf->Cell(40,12,$column,1);
}
$pdf->Output();
?>

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.

Step 4: Creating the Landing Page

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





Simple Example of PDF file using PHP and MySQL





Generate PDF file from MySQL Using PHP

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

Conclusion:

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 :

Summary

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 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.

Leave a Comment