Hello Everyone ! Today I am going to show you how to generate .pdf invoices in PHP and MySQL. Automatic generation of invoices and purchase orders are features of most of the ERP systems out there in the market, and so knowing the KNOW-HOW of this functionality can help you in understanding the mechanism and develop one on your own.

For this project I am going to use a php library for generating pdf files called FPDF, you can read about this library and its documentation here. Also , I assume that you have already installed PHP,MYSQL and APACHE stack.If not I suggest you to install XAMPP software which comes bundled with all mentioned features and is very handy to operate.For the sake of simplicity I have divided this tutorial into two parts.The first part deals with the UI setup and database setup.And the second part will deal with the actual creation of the invoice pdf from the data. Now jumping straight to our Part 1 of the tutorial:

  1. Create a database called ‘ test’  from your PHPMYADMIN section.

2. Now create a products table in your database using this query :

    name varchar(255) NOT NULL,
    img varchar(255),
    company varchar(255),
    unit_price int,

3. Populate the table with some dummy values as below :

insert into Products(name,img,company,unit_price) values('Test1','1.jpg','ABC ent',70);
insert into Products(name,img,company,unit_price) values('Test2','2.jpg','ABC ent',50);
insert into Products(name,img,company,unit_price) values('Test3','3.jpg','ABC ent',30);
insert into Products(name,img,company,unit_price) values('Test4','4.jpg','ABC ent',20);
insert into Products(name,img,company,unit_price) values('Test5','5.jpg','ABC ent',60);
insert into Products(name,img,company,unit_price) values('Test6','6.jpg','ABC ent',40);
insert into Products(name,img,company,unit_price) values('Test7','7.jpg','ABC ent',30);
insert into Products(name,img,company,unit_price) values('Test8','8.jpg','ABC ent',10);
insert into Products(name,img,company,unit_price) values('Test9','9.jpg','ABC ent',80);
insert into Products(name,img,company,unit_price) values('Test10','10.jpg','ABC ent',50);

4. Now our database part is done,and we can proceed to the next stage and make a simple UI for the user to select the products and generate the invoice for it.Lets create a new file index.php in our working directory.

<body bgcolor="beige">
<h1 style="text-align:center;text-transform:uppercase;">My Store</h1>
<ul style="list-style:none;padding-left:0;">
    <li style="display:inline-block;margin:10px;">
    <form action="" method="post">
        <div style="text-align:center;border:1px #e2e2e2 solid;padding:20px;">
            <img src="img/x.png" width="200" />
            <input type="hidden" value="" name="pname" />
            <h4>Company goes here</h4>
            <input type="hidden" value="" name="pcompany" />
                <label>Quantity :</label>
                <input type="number" name="quantity" style="width:50px;"/>
            <h5>Price : 50</h5>
            <input type="hidden" value="" name="pprice" />
            <input type="submit" value="ADD TO CART">

5. Now that our basic UI is complete we can now proceed with populating the page with the data from the database.Lets create a new file called model.php and write the below code :


class Model{
private $db;
function __construct(){
$this->db=new PDO('mysql:host='.HOST.';dbname='.Test.';charset=utf8mb4', USERNAME, PASSWORD);
function fetch_products(){
$stmt=$this->db->prepare('select * from products');
        if($stmt->rowCount()>0) {
            while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                array_push($data, $row);
        return $data;


6. Now include model.php file in our index.php file and write the below code above the <html> tag.

$pd=new Model();
$data = $pd->fetch_products();

7. Now lets utilize this data and make our store dynamic.Replace the li element code with the below one in your index.php file.

foreach($data as $d){ ?>
<li style="display:inline-block;margin:10px;">
    <form action="" method="post">
        <div style="text-align:center;border:1px #e2e2e2 solid;padding:20px;">
            <img src="img/<?php echo $d["img"];?>" width="200" />
            <h2><?php echo $d["name"];?></h2>
            <input type="hidden" value="<?php echo $d["name"];?>" name="pname" />
            <h4><?php echo $d["company"];?></h4>
            <input type="hidden" value="<?php echo $d["company"];?>" name="pcompany" />
                <label>Quantity :</label>
                <input type="number" name="quantity" style="width:50px;"/>
            <h5>Price : <?php echo $d["unit_price"];?></h5>
            <input type="hidden" value="<?php echo $d["unit_price"];?>" name="pprice" />
            <input type="submit" value="ADD TO CART">

<?php }


8. So we are done with products displaying and now we will proceed with the add to cart functionality.For this write the below code in your index.php. But before anything else first of all write the below line  just  below the starting php tag i.e. at the beginning of the page.


Now,lets move with the add to cart functionality.


$name = $_POST['pname'];
$quantity = $_POST["quantity"];
$single_cart = array("name"=>$name,"company"=>$company,"quantity"=>$quantity,"price"=>$price);

That’s all guys ! we are done with this part.In the next part of this tutorial we will utilize this cart data to generate the invoice.

Leave a Reply

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