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();
?>
Download Source Code (How to Generate PDF from MySQL Data using PHP)
Size: 2.58 KB
Version: V 1.0
Leave A Reply

Your email address will not be published.

37 Comments
  1. paul says

    please, how can i get the fpdf file because i tried to download but when i load it gives me errors.

    1. Anuj Kumar says

      Visit this tutorial http://www.fpdf.org/

  2. Bilal Khan says

    Sir i am from pakistan .! your all stuffs are very helpful, Sir in my FYP(final year project) there is also need to generate pdf form, sir in this there is table,, is there is any source code to generate pdf form (which contains input names, fname, geneder etc) ? Thanks .! will be wait for your response

  3. Bilal Khan says

    Sir email verification ka code mil sakta hy . .
    i am a student of BS Computer Science final semester, i need this for final year project..Thank you sir

    1. Anuj Kumar says

      For email, verification visit this tutorials : https://phpgurukul.com/php-email-verification-script/
      You can find the projects here: https://phpgurukul.com/php-projects-free-downloads/

  4. Bilal Khan says

    Sir yi bahut helpful hy . .Thank you so much

  5. Chisomo says

    Want to use this but shouldn’t be using PDO…Is there an alternative?

    1. Anuj Kumar says

      NO

  6. mani says

    i want source code, not able to download

    1. Anuj Kumar says

      Download link given above you can download from there.

      1. Kevin says

        Hi sir,
        link unvailable 404 page not found

        1. Anuj Kumar says

          Download link is fixed. Now you can download.

          1. Kevin says

            Thanks you so much

      2. kris says

        where is it to download?

        1. Anuj Kumar says

          Download link is fixed. You can download the script.

          1. kris says

            thank you, sir.

          2. kris says

            page not found..

          3. Anuj Kumar says

            Check now.

          4. kris says

            perfect, thank you. looking forward to learn more from you

  7. nur says

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

    1. Anuj Kumar says

      You can contact at phpgurukulofficial@gmail.com

  8. Akshay Srivastava says

    do i have to create a schema first

    1. Anuj Kumar says

      yes

  9. Makama Umar says

    thanks

  10. priya says

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

    1. Anuj Kumar says

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

      1. priya says

        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

  11. me says

    how to make page landscape

    1. Anuj Kumar says

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

  12. priya says

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

    1. Anuj Kumar says

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

      1. priya says

        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

  13. Akshay Srivastava says

    do i have to create a schema first

    1. Anuj Kumar says

      yes

  14. me says

    how to make page landscape

    1. Anuj Kumar says

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

  15. Makama Umar says

    thanks

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Privacy & Cookies Policy