How to create pdf files in php

In this tutorial, I will show you how you can create pdf files using php and a php class called fpdf.

This tutorial is only for making mysql tables appear in pdf files. So basically, its like creating a report for a certain project.

Let’s begin by downloading fpdf from their site.

After that, extract the fpdf folder into your project file in the ‘www’ folder  if you’re using wamp. And ‘htdocs’ if you’re using xampp.

Like this:



If you’re going to browser through the fpdf site. And go to scripts, you will see a ready-made php scripts to generate reports from a mysql database. I just copied the code below from the site:


class PDF_MySQL_Table extends FPDF
var $ProcessingTable=false;
var $aCols=array();
var $TableX;
var $HeaderColor;
var $RowColors;
var $ColorIndex;
function Header()
    //Print the table header if necessary
function TableHeader()
    foreach($this->aCols as $col)
function Row($data)
    foreach($this->aCols as $col)
function CalcWidths($width,$align)
    //Compute the widths of the columns
    foreach($this->aCols as $i=>$col)
    //Compute the abscissa of the table
function AddCol($field=-1,$width=-1,$caption='',$align='L')
    //Add a column to the table
function Table($query,$prop=array())
    //Issue query
    $res=mysql_query($query) or die('Error: '.mysql_error()."<BR>Query: $query");
    //Add all columns if none was specified
    //Retrieve column names when not specified
    foreach($this->aCols as $i=>$col)
    //Handle properties
    //Compute column widths
    //Print header
    //Print rows

That is long indeed, but we only need to understand one task that it does. For me, it basically takes care of all the formatting and page rendering for all the pdf files that were going to create, so we won’t have to have long lines of codes like this if we are now going to generate the actual page.

The code below would be the actual php script that will generate the page. In firefox 3.6 there’s no built in functionality that would render the pdf file on the actual page. So I recommend using chrome, so you won’t have to download a pdf file everytime you try to test it.

All you have to do now is to:

  1. import the mysql_table.php
  2. include your database server information. Stuff like server name, username and password.
  3. the actual query that would produce the report



class PDF extends PDF_MySQL_Table
function Header()
    $this->Cell(0,6,’All Products’,0,1,'C');
$pdf=new PDF();
$pdf->Table(‘SELECT * FROM prod_table’);

After you have done all of that, the generated page will now look something like this, if ever you try to access the ‘pdfgeneratingpage.php’ or whatever name you gave to your script:



Some points you have to check if ever it didn’t work out for you:

  1. Check the include or require function. You must include the actual directory where the ‘fpdf.php’ or ‘mysql_table.php’. Don’t just copy and paste, try to examine everything before complaining that it doesn’t work. Keep in mind the ‘..’ symbol is used to indicate that you’re going to move backwards. So if the file you’re trying to require/include is not in the same directory then try that.
  2. Check your database server information.
  3. Check your query in the mysql console first or in the phpmyadmin before putting it into your script, there might be some errors in your query.

Other than that I cannot see any reason why it wouldn’t work for you. So thanks for reading.

10 thoughts on “How to create pdf files in php

  1. hello kyokasuigetsu25,
    the script get printed as like:

    {rtf1ansiansicpg1252deff0deflang1033{fonttbl{f0fswissfcharset0 Arial;}} {*generator Msftedit;}viewkind4uc1pardf0fs20 SetFont(‘Arial’,”,18);par $this->Cell(0,6,rquote All Productsrquote ,0,1,’C’);par $this->Ln(10);par par parent::Header();par }par }par par mysql_connect(‘localhost’,’root’,”);par mysql_select_db(‘db’);par par $pdf=new PDF();par $pdf->AddPage();par par $pdf->Table(lquote SELECT * FROM browserviewrquote );par par $pdf->Output();par ?>par par }

    when i executed the above code ………………

    can u figure out the pbm….. waiting for ur reply……… thanks in advance

  2. hay i am using this code…and when i click to the button it shows empty PDF file….it not showing a single page…but showing up result that page 1 OF 3…with no data

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s