Hello Everyone ! Welcome to the second part of this tutorial series ‘Generating Invoice Using FPDF in PHP and MySQL’. For a quick reminder about what we have accomplished till now , please go through the below list

  1. Creation of database ‘test’ and table ‘products’ in mysql databse.
  2. Creation of products display page.
  3. Created a ‘model.php’ file to fetch data from the database.
  4. Addition of ‘ADD TO CART’ functionality to each product.

Now  proceeding further with the generation of Invoice from the cart products data.

As per our last tutorial we were able to store our cart data in a session variable called $_SESSION['cart']. So we will utilize this data here.

First of all lets add a anchor link after the ul closing tag.

<?php 
    if(isset($_SESSION['cart']) && !empty($_SESSION['cart'])){ ?>
<a href="generate_invoice.php" target="_blank" style="text-decoration:none;text-align:center;line-height:40px;color:#fff;width:200px;height:40px;background:green;display:block;margin:auto;">Generate Invoice</a>
<?php } ?>

That’s done, and we now proceed with creating the ‘generate_invoice.php’ file in our working directory.Once  the file is created write the below lines in it.

<?php 
session_start();

?>

Now its time to download our FPDF library in our working directory.You can download this library from here. Once the library is downloaded, extract the files in a new folder and name it as fpdf. Now copy the fpdf folder in your working directory.

After this, create a new php file called invoice.php in your project root directory and add the below lines.

<?php 
require('fpdf/fpdf.php');

class Invoice extends FPDF{

    function Header(){
        $this->SetFont('Arial','B',24);
        $this->Cell(60);
        $this->Cell(60,10,'XYZ ENTERPRISES');
        $this->Ln(10);
        $this->Line(0,20,$this->GetPageWidth(),20);
        $this->Ln(10);
        $this->SetFont('Arial','B',14);
        $this->Cell(70);
        $this->Cell(60,10,'PURCHASE ORDER',1,1,'C',true);
        $this->Image('ENTER URL TO YOUR LOGO HERE',95,45,20);
        $this->Ln(10);
    }

    function add_from($str){
        $this->SetFont('Arial','',10);
        $this->setXY(10,50);
        $this->Cell(70,7,'From',1,2,'L',true);
        $this->MultiCell(70,8,$str,'LRB',1);
    }

    function add_to($str){
        $x = $this->GetX();
        $this->setXY($x+120,50);
        $this->Cell(60,7,'To',1,2,'L',true);
        $y = $this->GetY();
        $this->MultiCell(60,8,$str,'LRB',1);
        $this->Ln(10);
    }

    function add_order_detail($rdate,$cur){
        $this->Cell(50);
        $x=$this->GetX();
        $y=$this->GetY();
        $this->setXY($x,$y);
        $this->Cell(50,7,'Order date',1,2,'L',true);
        $this->Cell(50,8,$rdate,'LRB',1);
        $this->setXY($x+50,$y);
        $this->Cell(40,7,'Currency',1,2,'L',true);
        $this->Cell(40,8,$cur,'LRB',1);
        $this->Ln(10);
    }

    function populate_table($data){
        $x=$this->GetX();
        $y=$this->GetY();
        $this->setXY($x,$y);
        $this->Cell(20,7,'S.No.',1,2,'L',true);
        $this->setXY($x+20,$y);
        $this->Cell(50,7,'Product name',1,2,'L',true);
        $this->setXY($x+70,$y);
        $this->Cell(30,7,'Company',1,2,'L',true);
        $this->setXY($x+100,$y);
        $this->Cell(20,7,'Qty.',1,2,'L',true);
        $this->setXY($x+120,$y);
        $this->Cell(30,7,'Unit Price',1,2,'L',true);
        $this->setXY($x+150,$y);
        $this->Cell(30,7,'Total Price',1,2,'L',true);
        $i=1;
        $total=0;
        foreach($data as $d){
            $price = $d["price"]*$d["quantity"];
            $this->Ln(0);
            $x=$this->GetX();
            $y=$this->GetY();
            $this->setXY($x,$y);
            $this->Cell(20,7,$i,1,2,'L');
            $this->setXY($x+20,$y);
            $this->Cell(50,7,$d["name"],1,2,'L');
            $this->setXY($x+70,$y);
            $this->Cell(30,7,$d["company"],1,2,'L');
            $this->setXY($x+100,$y);
            $this->Cell(20,7,$d["quantity"],1,2,'L');
            $this->setXY($x+120,$y);
            $this->Cell(30,7,$d["price"],1,2,'L');
            $this->setXY($x+150,$y);
            $this->Cell(30,7,$price,1,2,'L');
            $total += $price;
            $i++;
        }
        $this->Ln(0);
        $x=$this->GetX();
        $y=$this->GetY();
        $this->setXY($x+120,$y);
        $this->Cell(30,7,'Grand Total',1,2,'C',true);
        $this->setXY($x+150,$y);
        $this->Cell(30,7,round($total,2),1,2,'L');
    }

    function Footer()
    {
        $this->SetY(-15);
        $this->SetFont('Arial','I',8);
        $this->Cell(0,10,'Copyright 2018 XYZ Enterprises. All Rights Reserved.',0,0,'C');
    }
}

?>

OMG 🙂 ! That’s huge right ! But basically if you go through it line by line you will be able to get an idea of what I am trying to do over here.However , if you face any difficulty ,I would suggest you to go through the documentation of the fpdf library, and for any assistance related to the codes here you are free to comment below.

Now lets proceed further and import this invoice class in our ‘generate_invoice.php’ file.

<?php
session_start(); 
require('invoice.php');
$data =$_SESSION['cart'];
$pdf = new Invoice();
$pdf->SetFillColor(214,214,214);
$pdf->AddPage();
$pdf->add_from("XYZ Enterprises\nabc street,PO 8976\nTel:9876567891");
$pdf->add_to("ABC Enterprises\ndef street,PO 8975\nTel:9876567756");
$pdf->add_order_detail(date(),'USD');
$pdf->populate_table($data);
$pdf->Output();

?>

Now finally, lets have a look of what we have achieved.Please click here to view the generated invoice.

That’s it ! We are done with this part and also with this tutorial series.Hope you enjoyed it ! Please comment your view.


Related Posts

Leave a Reply

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