PHP tutorials,a way to getting some thing new in web

Followers

Powered by Blogger.

Friday 5 September 2014

PHP Invoice Creation for Billing

No comments :
Today I will cover one great documentary topic which is know as pdf coding.or I will call it as pdf coding.

PHP has tremendous advantages.It has all aspects of features to get things done.Generating invoices is needed in every php developers life because they work with ecommerece and at the end ecommerece projects demand for invoice generation and billing system.so every php developer must know how to create invoice pdf using php.

PHP has pdf creation class which is very helpful for pdf class.There are fpdf,tcpdf classes which has different functions which is used to create PDF functions.Here I have created small code for billing invoice.I have used FPDF
Class and its functions.

You can get FPDF class from below link:

http://www.fpdf.org/

Go to Download Link and Download class which is as ZIP.
copy and paste fpdf.php class and fonts in your project folder and give path in your code like below:

require('u/fpdf.php');

Here u is folder and fpdf.php is class file.

create another folder named invoice where all invoices which you generate will be stored.

Take a look below code:

Here I have create one simple form which takes billing information:

<form action="" method="post" enctype="multipart/form-data">
<div id="body_l">
<div id="name"><input name="company" placeholder="Company Name" type="text" /></div>
<div id="name"><input name="address" placeholder="Company Address" type="text" /></div>
<div id="name"><input name="email" placeholder="Email" type="text" /></div>
<div id="name"><input name="telephone" placeholder="telephone number" type="text" /></div>
<div id="name"><input name="item" placeholder="Item" type="text" /></div>
</div>
<div id="body_r">
<div id="name"><input name="price" placeholder="price" type="text" /></div>
<div id="name"><input name="dis" placeholder="discount" type="text" /></div>
<div id="name"><input name="vat" placeholder="VAT" type="text" /></div>
</div>
<div id="up" align="center"><input name="submit" style="margin-top:60px;" value="Submit" type="submit" /><br /><br />
</div>
</form>

Here we can some CSS in code:


<style>

a{
color:#999999;
text-decoration:none;
}
a:hover{
color:#999999;
text-decoration:underline;
}
#content{
width:800px;
height:600px;
background-color:#FEFEFE;
border: 10px solid rgb(255, 255, 255);
border: 10px solid rgba(255, 255, 255, .5);
-webkit-background-clip: padding-box;
background-clip: padding-box;
border-radius: 10px;
opacity:0.90;
filter:alpha(opacity=90);
margin:auto;
}
#footer{
width:800px;
height:30px;
padding-top:15px;
color:#666666;
margin:auto;
}
#title{
width:770px;
margin:15px;
color:#999999;
font-size:18px;
font-family:Verdana, Arial, Helvetica, sans-serif;
}
#body{
width:770px;
height:360px;
margin:15px;
color:#999999;
font-size:16px;
font-family:Verdana, Arial, Helvetica, sans-serif;
}
#body_l{
width:385px;
height:360px;
float:left;
}
#body_r{
width:385px;
height:360px;
float:right;
}
#name{
width:width:385px;
height:40px;
margin-top:15px;
}
input{
margin-top:10px;
width:345px;
height:32px;
-moz-border-radius: 5px;
border-radius: 5px;
border:1px solid #ccc;

color:#999;
margin-left:15px;
padding:5px;
}
#up{
width:770px;
height:40px;
margin:auto;
margin-top:10px;
}
</style>




After submitting submit button we can get all data via form post and generate fine invoice:

<?php 
if(isset($_POST["submit"]))
{
$company = $_POST["company"];
$address = $_POST["address"];
$email = $_POST["email"];
$telephone = $_POST["telephone"];
$number = $_POST["number"];
$item = $_POST["item"];
$price = $_POST["price"];
$dis = $_POST["dis"];
$vat = $_POST["vat"];
$final_amt=$price - ($dis/100) + ($vat/100);
$pay = 'Payment information';
$price = str_replace(",",".",$price);
$vat = str_replace(",",".",$vat);
$p = explode(" ",$price);
$v = explode(" ",$vat);
$re = $p[0] + $v[0];
function r($r)
{
$r = str_replace("$","",$r);
$r = str_replace(" ","",$r);
$r = $r." $";
return $r;
}
$price = r($price);
$vat = r($vat);
require('u/fpdf.php');

class PDF extends FPDF
{
function Header()
{
if(!empty($_FILES["file"]))
  {
$uploaddir = "logo/";
$nm = $_FILES["file"]["name"];
$random = rand(1,99);
move_uploaded_file($_FILES["file"]["tmp_name"], $uploaddir.$random.$nm);
$this->Image($uploaddir.$random.$nm,10,10,20);
unlink($uploaddir.$random.$nm);
}
$this->SetFont('Arial','B',12);
$this->Ln(1);
}
function Footer()
{
$this->SetY(-15);
$this->SetFont('Arial','I',8);
$this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
function ChapterTitle($num, $label)
{
$this->SetFont('Arial','',12);
$this->SetFillColor(200,220,255);
$this->Cell(0,6,"$num $label",0,1,'L',true);
$this->Ln(0);
}
function ChapterTitle2($num, $label)
{
$this->SetFont('Arial','',12);
$this->SetFillColor(249,249,249);
$this->Cell(0,6,"$num $label",0,1,'L',true);
$this->Ln(0);
}
}

$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Times','',12);
$pdf->SetTextColor(32);
$pdf->Cell(0,5,$company,0,1,'R');
$pdf->Cell(0,5,$address,0,1,'R');
$pdf->Cell(0,5,$email,0,1,'R');
$pdf->Cell(0,5,'Tel: '.$telephone,0,1,'R');
$pdf->Cell(0,30,'',0,1,'R');
$pdf->SetFillColor(200,220,255);
$pdf->ChapterTitle('Invoice Number ',$number);
$pdf->ChapterTitle('Invoice Date ',date('d-m-Y'));
$pdf->Cell(0,20,'',0,1,'R');
$pdf->SetFillColor(224,235,255);
$pdf->SetDrawColor(192,192,192);
$pdf->Cell(170,7,'Item',1,0,'L');
$pdf->Cell(20,7,'Price',1,1,'C');
$pdf->Cell(170,7,$item,1,0,'L',0);
$pdf->Cell(20,7,$price,1,1,'C',0);
$pdf->Cell(0,0,'',0,1,'R');
$pdf->Cell(170,7,'Discount',1,0,'R',0);
$pdf->Cell(20,7,$dis,1,1,'C',0);
$pdf->Cell(170,7,'VAT',1,0,'R',0);
$pdf->Cell(20,7,$vat,1,1,'C',0);
$pdf->Cell(170,7,'Total',1,0,'R',0);
$pdf->Cell(20,7,$final_amt." $",1,0,'C',0);
$filename=rand(0,1999).".pdf";
$pdf->Output("invoice/".$filename,'F');
}
?>

Above code I have used rand() function for pdf name so that every pdf class is recognized with unique name.then we will store it in folder invoice which we have created at starting of tutorial.




No comments :

Post a Comment