How to Create Printable PDF invoice using PHP MySQL
This examples shows, how to Create Invoice using PHP MySQL and Make Printable PDF using FPDF. The Output shown as below


Files and Directory
crud_application/ ├── fpdf/ ├── config.php ├── index.php ├── print.php ├── word.php ├── invoice_db.sql
Download FPDF Class from http://www.fpdf.org/en/download.php and extract zip file into fpdf folder.
config.php
<?php $con=mysqli_connect("localhost","root","","invoice_db"); ?>
index.php
<?php require "config.php"; ?> <html> <head> <title>Create Printable PDF invoice using PHP MySQL</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script> <link rel='stylesheet' href='https://code.jquery.com/ui/1.13.0/themes/base/jquery-ui.css'> <script src="https://code.jquery.com/ui/1.13.0-rc.3/jquery-ui.min.js" integrity="sha256-R6eRO29lbCyPGfninb/kjIXeRjMOqY3VWPVk6gMhREk=" crossorigin="anonymous"></script> </head> <body> <div class='container pt-5'> <h1 class='text-center text-primary'>Create Printable PDF invoice using PHP MySQL</h1><hr> <?php if(isset($_POST["submit"])){ $invoice_no=$_POST["invoice_no"]; $invoice_date=date("Y-m-d",strtotime($_POST["invoice_date"])); $cname=mysqli_real_escape_string($con,$_POST["cname"]); $caddress=mysqli_real_escape_string($con,$_POST["caddress"]); $ccity=mysqli_real_escape_string($con,$_POST["ccity"]); $grand_total=mysqli_real_escape_string($con,$_POST["grand_total"]); $sql="insert into invoice (INVOICE_NO,INVOICE_DATE,CNAME,CADDRESS,CCITY,GRAND_TOTAL) values ('{$invoice_no}','{$invoice_date}','{$cname}','{$caddress}','{$ccity}','{$grand_total}') "; if($con->query($sql)){ $sid=$con->insert_id; $sql2="insert into invoice_products (SID,PNAME,PRICE,QTY,TOTAL) values "; $rows=[]; for($i=0;$i<count($_POST["pname"]);$i++) { $pname=mysqli_real_escape_string($con,$_POST["pname"][$i]); $price=mysqli_real_escape_string($con,$_POST["price"][$i]); $qty=mysqli_real_escape_string($con,$_POST["qty"][$i]); $total=mysqli_real_escape_string($con,$_POST["total"][$i]); $rows[]="('{$sid}','{$pname}','{$price}','{$qty}','{$total}')"; } $sql2.=implode(",",$rows); if($con->query($sql2)){ echo "<div class='alert alert-success'>Invoice Added Successfully. <a href='print.php?id={$sid}' target='_BLANK'>Click </a> here to Print Invoice </div> "; }else{ echo "<div class='alert alert-danger'>Invoice Added Failed.</div>"; } }else{ echo "<div class='alert alert-danger'>Invoice Added Failed.</div>"; } } ?> <form method='post' action='index.php' autocomplete='off'> <div class='row'> <div class='col-md-4'> <h5 class='text-success'>Invoice Details</h5> <div class='form-group'> <label>Invoice No</label> <input type='text' name='invoice_no' required class='form-control'> </div> <div class='form-group'> <label>Invoice Date</label> <input type='text' name='invoice_date' id='date' required class='form-control'> </div> </div> <div class='col-md-8'> <h5 class='text-success'>Customer Details</h5> <div class='form-group'> <label>Name</label> <input type='text' name='cname' required class='form-control'> </div> <div class='form-group'> <label>Address</label> <input type='text' name='caddress' required class='form-control'> </div> <div class='form-group'> <label>City</label> <input type='text' name='ccity' required class='form-control'> </div> </div> </div> <div class='row'> <div class='col-md-12'> <h5 class='text-success'>Product Details</h5> <table class='table table-bordered'> <thead> <tr> <th>Product</th> <th>Price</th> <th>Qty</th> <th>Total</th> <th>Action</th> </tr> </thead> <tbody id='product_tbody'> <tr> <td><input type='text' required name='pname[]' class='form-control'></td> <td><input type='text' required name='price[]' class='form-control price'></td> <td><input type='text' required name='qty[]' class='form-control qty'></td> <td><input type='text' required name='total[]' class='form-control total'></td> <td><input type='button' value='x' class='btn btn-danger btn-sm btn-row-remove'> </td> </tr> </tbody> <tfoot> <tr> <td><input type='button' value='+ Add Row' class='btn btn-primary btn-sm' id='btn-add-row'></td> <td colspan='2' class='text-right'>Total</td> <td><input type='text' name='grand_total' id='grand_total' class='form-control' required></td> </tr> </tfoot> </table> <input type='submit' name='submit' value='Save Invoice' class='btn btn-success float-right'> </div> </div> </form> </div> <script> $(document).ready(function(){ $("#date").datepicker({ dateFormat:"dd-mm-yy" }); $("#btn-add-row").click(function(){ var row="<tr> <td><input type='text' required name='pname[]' class='form-control'></td> <td><input type='text' required name='price[]' class='form-control price'></td> <td><input type='text' required name='qty[]' class='form-control qty'></td> <td><input type='text' required name='total[]' class='form-control total'></td> <td><input type='button' value='x' class='btn btn-danger btn-sm btn-row-remove'> </td> </tr>"; $("#product_tbody").append(row); }); $("body").on("click",".btn-row-remove",function(){ if(confirm("Are You Sure?")){ $(this).closest("tr").remove(); grand_total(); } }); $("body").on("keyup",".price",function(){ var price=Number($(this).val()); var qty=Number($(this).closest("tr").find(".qty").val()); $(this).closest("tr").find(".total").val(price*qty); grand_total(); }); $("body").on("keyup",".qty",function(){ var qty=Number($(this).val()); var price=Number($(this).closest("tr").find(".price").val()); $(this).closest("tr").find(".total").val(price*qty); grand_total(); }); function grand_total(){ var tot=0; $(".total").each(function(){ tot+=Number($(this).val()); }); $("#grand_total").val(tot); } }); </script> </body> </html>
print.php
<?php require ("fpdf/fpdf.php"); require ("word.php"); require "config.php"; //customer and invoice details $info=[ "customer"=>"", "address"=>",", "city"=>"", "invoice_no"=>"", "invoice_date"=>"", "total_amt"=>"", "words"=>"", ]; //Select Invoice Details From Database $sql="select * from invoice where SID='{$_GET["id"]}'"; $res=$con->query($sql); if($res->num_rows>0){ $row=$res->fetch_assoc(); $obj=new IndianCurrency($row["GRAND_TOTAL"]); $info=[ "customer"=>$row["CNAME"], "address"=>$row["CADDRESS"], "city"=>$row["CCITY"], "invoice_no"=>$row["INVOICE_NO"], "invoice_date"=>date("d-m-Y",strtotime($row["INVOICE_DATE"])), "total_amt"=>$row["GRAND_TOTAL"], "words"=> $obj->get_words(), ]; } //invoice Products $products_info=[]; //Select Invoice Product Details From Database $sql="select * from invoice_products where SID='{$_GET["id"]}'"; $res=$con->query($sql); if($res->num_rows>0){ while($row=$res->fetch_assoc()){ $products_info[]=[ "name"=>$row["PNAME"], "price"=>$row["PRICE"], "qty"=>$row["QTY"], "total"=>$row["TOTAL"], ]; } } class PDF extends FPDF { function Header(){ //Display Company Info $this->SetFont('Arial','B',14); $this->Cell(50,10,"ABC COMPUTERS",0,1); $this->SetFont('Arial','',14); $this->Cell(50,7,"West Street,",0,1); $this->Cell(50,7,"Salem 636002.",0,1); $this->Cell(50,7,"PH : 8778731770",0,1); //Display INVOICE text $this->SetY(15); $this->SetX(-40); $this->SetFont('Arial','B',18); $this->Cell(50,10,"INVOICE",0,1); //Display Horizontal line $this->Line(0,48,210,48); } function body($info,$products_info){ //Billing Details $this->SetY(55); $this->SetX(10); $this->SetFont('Arial','B',12); $this->Cell(50,10,"Bill To: ",0,1); $this->SetFont('Arial','',12); $this->Cell(50,7,$info["customer"],0,1); $this->Cell(50,7,$info["address"],0,1); $this->Cell(50,7,$info["city"],0,1); //Display Invoice no $this->SetY(55); $this->SetX(-60); $this->Cell(50,7,"Invoice No : ".$info["invoice_no"]); //Display Invoice date $this->SetY(63); $this->SetX(-60); $this->Cell(50,7,"Invoice Date : ".$info["invoice_date"]); //Display Table headings $this->SetY(95); $this->SetX(10); $this->SetFont('Arial','B',12); $this->Cell(80,9,"DESCRIPTION",1,0); $this->Cell(40,9,"PRICE",1,0,"C"); $this->Cell(30,9,"QTY",1,0,"C"); $this->Cell(40,9,"TOTAL",1,1,"C"); $this->SetFont('Arial','',12); //Display table product rows foreach($products_info as $row){ $this->Cell(80,9,$row["name"],"LR",0); $this->Cell(40,9,$row["price"],"R",0,"R"); $this->Cell(30,9,$row["qty"],"R",0,"C"); $this->Cell(40,9,$row["total"],"R",1,"R"); } //Display table empty rows for($i=0;$i<12-count($products_info);$i++) { $this->Cell(80,9,"","LR",0); $this->Cell(40,9,"","R",0,"R"); $this->Cell(30,9,"","R",0,"C"); $this->Cell(40,9,"","R",1,"R"); } //Display table total row $this->SetFont('Arial','B',12); $this->Cell(150,9,"TOTAL",1,0,"R"); $this->Cell(40,9,$info["total_amt"],1,1,"R"); //Display amount in words $this->SetY(225); $this->SetX(10); $this->SetFont('Arial','B',12); $this->Cell(0,9,"Amount in Words ",0,1); $this->SetFont('Arial','',12); $this->Cell(0,9,$info["words"],0,1); } function Footer(){ //set footer position $this->SetY(-50); $this->SetFont('Arial','B',12); $this->Cell(0,10,"for ABC COMPUTERS",0,1,"R"); $this->Ln(15); $this->SetFont('Arial','',12); $this->Cell(0,10,"Authorized Signature",0,1,"R"); $this->SetFont('Arial','',10); //Display Footer Text $this->Cell(0,10,"This is a computer generated invoice",0,1,"C"); } } //Create A4 Page with Portrait $pdf=new PDF("P","mm","A4"); $pdf->AddPage(); $pdf->body($info,$products_info); $pdf->Output(); ?>
word.php
<?php //Convert Number to Indian Currency Format class IndianCurrency{ public function __construct($amount){ $this->amount=$amount; $this->hasPaisa=false; $arr=explode(".",$this->amount); $this->rupees=$arr[0]; if(isset($arr[1])&&((int)$arr[1])>0){ if(strlen($arr[1])>2){ $arr[1]=substr($arr[1],0,2); } $this->hasPaisa=true; $this->paisa=$arr[1]; } } public function get_words(){ $w=""; $crore=(int)($this->rupees/10000000); $this->rupees=$this->rupees%10000000; $w.=$this->single_word($crore,"Cror "); $lakh=(int)($this->rupees/100000); $this->rupees=$this->rupees%100000; $w.=$this->single_word($lakh,"Lakh "); $thousand=(int)($this->rupees/1000); $this->rupees=$this->rupees%1000; $w.=$this->single_word($thousand,"Thousand "); $hundred=(int)($this->rupees/100); $w.=$this->single_word($hundred,"Hundred "); $ten=$this->rupees%100; $w.=$this->single_word($ten,""); $w.="Rupees "; if($this->hasPaisa){ if($this->paisa[0]=="0"){ $this->paisa=(int)$this->paisa; } else if(strlen($this->paisa)==1){ $this->paisa=$this->paisa*10; } $w.=" and ".$this->single_word($this->paisa," Paisa"); } return $w." Only"; } private function single_word($n,$txt){ $t=""; if($n<=19){ $t=$this->words_array($n); }else{ $a=$n-($n%10); $b=$n%10; $t=$this->words_array($a)." ".$this->words_array($b); } if($n==0){ $txt=""; } return $t." ".$txt; } private function words_array($num){ $n=[0=>"", 1=>"One", 2=>"Two", 3=>"Three", 4=>"Four", 5=>"Five", 6=>"Six", 7=>"Seven", 8=>"Eight", 9=>"Nine", 10=>"Ten", 11=>"Eleven", 12=>"Twelve", 13=>"Thirteen", 14=>"Fourteen", 15=>"Fifteen", 16=>"Sixteen", 17=>"Seventeen", 18=>"Eighteen", 19=>"Nineteen", 20=>"Twenty", 30=>"Thirty", 40=>"Forty", 50=>"Fifty", 60=>"Sixty", 70=>"Seventy", 80=>"Eighty", 90=>"Ninety", 100=>"Hundred",]; return $n[$num]; } } ?>
MySQL query for create Tables
CREATE TABLE `invoice_db`.`invoice` ( `SID` int(11) NOT NULL AUTO_INCREMENT, `INVOICE_NO` int(11) NOT NULL, `INVOICE_DATE` date NOT NULL, `CNAME` varchar(50) NOT NULL, `CADDRESS` varchar(150) NOT NULL, `CCITY` varchar(50) NOT NULL, `GRAND_TOTAL` double(10,2) NOT NULL, PRIMARY KEY (`SID`) ); CREATE TABLE `invoice_db`.`invoice_products` ( `ID` int(11) NOT NULL AUTO_INCREMENT, `SID` int(11) NOT NULL, `PNAME` varchar(100) NOT NULL, `PRICE` double(10,2) NOT NULL, `QTY` int(11) NOT NULL, `TOTAL` double(10,2) NOT NULL, PRIMARY KEY (`ID`) );