Generate PDF File from MySQL Database Using PHP

This is another php pdf export feature tutorials, I will create simple PHP script to fetch data from MySQL and create pdf file using php. We will use third party PHP FPDF library. The FPDF is very awesome PHP class to generate PDF using PHP from MySQL database.This is open source php library to generate pdf file using PHP.

PDF is a very common and popular file format to read, view, and write documents.PDF format is independent of application software, hardware, and operating systems.

FPDF has following main features

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

Video Tutorial:

If you are more comfortable in watching a video that explains about using pdf generation, then you should watch this video tutorial.

Generate PDF File Using PDF and PHP

There is one dependency php extension which is Zlib to enable compression and GD for GIF image support. The latest version requires at least PHP 5.1.

export-pdf-mysql-php

Checkout other php export tutorials,

We will follow following steps to Generate PDF

  • Download the FPDF library from fpdf.org
  • We will fetch data from MySQL database into the page.
  • We will use FPDF libs function to generate pdf file with header and footer.

Step 1: We will create employee table into MySQL database.

We will generate some sample data and insert it into the employee table.

Step 2: Let’s connect MySQL database with PHP. We will create connection.php file and add below code.

Above file is used to connect and select MySQL database using PHP. You need to change $dbhost, $username, $password, and $dbname variable’s value with your database credentials.

Step 3: We will create generate_pdf.php file and add below code.

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: We will create index.php file and added below code.

We have added HTML form tag and define action value generate_pdf.php file.

Conclusion:

We have generated pdf file using php and MySQL database, You can generate pdf using other database as well.You just need to create connection with other type database.

You can download source code and see Demo from below link.

20 thoughts on “Generate PDF File from MySQL Database Using PHP

  1. I can’t trace the error…….Warning: Invalid argument supplied for foreach() in C:wampwwwPhpbintermgenerate_pdf.php on line 60

  2. hello tnxs a lot worked for me but i want to export image from db
    but image is stored in the folder not in db (only url is stored in the db)
    how can i export the image ?

    • $pdf->Image(‘image_path’,x1,y1,width,height);
      x1 – space from left side
      y1 – top space
      width – image width
      height – image height

      Note – x1, y1, width, height values must be in integer values (value without decimal point)

  3. dbhost, $this->username, $this->password, $this->dbname) or die(“Connection failed: ” . mysqli_connect_error()); /* check connection */ if (mysqli_connect_errno()) { printf(“Connect failed: %s\n”, mysqli_connect_error()); exit(); } else { $this->conn = $con; } return $this->conn; } } ?>
    Fatal error: Uncaught Error: Class ‘dbObj’ not found in C:\xampp\htdocs\generate_pdf.php:34 Stack trace: #0 {main} thrown in C:\xampp\htdocs\generate_pdf.php on line 34

    i got this error

  4. I am getting this below error,could anyone please help me out with solution for this.

    Fatal error: Uncaught Exception: FPDF error: Some data has already been output, can’t send PDF file in D:\XAMPP\htdocs\pdfdemo\libs\fpdf.php:271 Stack trace: #0

  5. Thanks for the code. As I can see $header variables are extra, I changed to this:

    $header = array(‘id’=>’ID’, ’employee_name’=> ‘Name’, ’employee_age’=> ‘Age’,’employee_salary’=> ‘Salary’,);
    $result = mysqli_query($con, “SELECT id, employee_name, employee_age, employee_salary FROM employee”) or die(“database error:”. mysqli_error($con));

    foreach($header as $heading) {
    $pdf->Cell(40,12,$heading,1);
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

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