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:
- Create a database called ‘ test’ from your PHPMYADMIN section.
2. Now create a products
table in your database using this query :
CREATE TABLE Products ( id int NOT NULL AUTO_INCREMENT, name varchar(255) NOT NULL, img varchar(255), company varchar(255), unit_price int, PRIMARY KEY (ID) );
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.
<html> <head></head> <body bgcolor="beige"> <h1 style="text-align:center;text-transform:uppercase;">My Store</h1> <hr/> <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" /> <h2>xyz</h2> <input type="hidden" value="" name="pname" /> <h4>Company goes here</h4> <input type="hidden" value="" name="pcompany" /> <div> <label>Quantity :</label> <input type="number" name="quantity" style="width:50px;"/> </div> <h5>Price : 50</h5> <input type="hidden" value="" name="pprice" /> <input type="submit" value="ADD TO CART"> </div> </form> </li> </ul> </body> </html>
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 :
<?php class Model{ private $db; function __construct(){ $this->db=new PDO('mysql:host='.HOST.';dbname='.Test.';charset=utf8mb4', USERNAME, PASSWORD); } function fetch_products(){ $data=[]; $stmt=$this->db->prepare('select * from products'); $stmt->execute(); 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.
<?php require('model.php'); $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.
<?php 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" /> <div> <label>Quantity :</label> <input type="number" name="quantity" style="width:50px;"/> </div> <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"> </div> </form> </li> <?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.
session_start();
Now,lets move with the add to cart
functionality.
if(!isset($_SESSION['cart'])){ $_SESSION['cart']=[]; } if ($_SERVER['REQUEST_METHOD'] == 'POST'){ $name = $_POST['pname']; $company=$_POST['pcompany']; $quantity = $_POST["quantity"]; $price=$_POST['pprice']; $single_cart = array("name"=>$name,"company"=>$company,"quantity"=>$quantity,"price"=>$price); array_push($_SESSION['cart'],$single_cart); }
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.