pdf file generation using php, mysql database

This tutorial is going to teach generation of a pdf file from php code using php, mysql database. portable document files files are secure, robust and convenient. Thats why we are going to implement it.
There is no any in-built function in php for generation of pdf files. So, we need to use a third party library for this task. There is an open-source FPDF library, download it from here. After downloading it, make a folder in your working directory as libs and extract the downloaded file contents in it.

Creating Database

First we’ll create a database and table with some data. So create database name phptopdf and run following SQL query in it to create a structure.

We now need to put some records in the structure. Do this by running the following query. It will create upto 16 records in the database, and those will be outputted in the file.

Connect to MySQL database

Now, we have to connect to the database. In order to achieve this, make a new file for that purpose. Name it as connection.php and paste the following code in it. The code will bind our php project to the values’ container. We are using an object oriented approach to make the connection because it is well formatted and prevents SQ injections. We have defined the class “dbObj” with all credentials as variables. They are used in the mysqli_connect() function. The query will display connect failed message with the proper error so that it will be easy to debug .

Now create the main index.php file as follows. It is a simple view with a button. The button will redirect to the post url which then builds a portable document file format with all the values we have entered earlier.

For a new fresh look, we have implemented some design using cascaded styles sheet. In order to achieve this, lets code some styling in style.css as a separate file.

The index.php file will look like as follows.

pdf file generation using php, mysql database

pdf file generation from MySQL database

In order to create portable document format, make file get_pdf.php with the following code.

In above file, we are including a downloaded library file and extending it’s FPDF class to inherit it’s predefined functions and use them in our file. We have defined a separate functions for header and footer of the file. Then, the further logic shows how to fetch the data from mysql and display it in the file with well formatting.

Output pdf file

That’s it! After clicking on the button, file generation will be done with all available records fetched and displayed as shown in image below.

pdf file generation using php, mysql database

If you have any doubts, let me know in comment box below. Alternatively, you can to download the source code from following link.

Download Source Code

2 thoughts on “pdf file generation using php, mysql database

  1. s
    sam says:

    $this->Cell( 70, 12, ‘Student Photo:’, 6, 0, ‘L’,1 );

    $this->MultiCell(120,12,$eachResult[“b_stpic”],1);

    all data is getting to pdf but profile not getting from database only showing image name . i used this code only to getting image from database

    but profile pic not getting pls help

    1. Ashish Joshi says:

      Hey Sam, did you used Image(‘[image-name-here]’,[cell-values]) function to show image? You have to put your $eachResult[“b_stpic”] variable in place of [image-name-here], as stated above.

Leave a Reply

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