Generate Printable Invoice in PHP using FPDF library
Nowadays, when almost everything is connected to internet. More people chooses web based application (or "webapp" in short) over desktop-based applications with various reasons but mostly because its flexibility and mobility.WebApp can become potentially powerful in computing power compared to stand alone application. Because the processing is done in server-side, even a low-end mobile devices can access robust information which requires complex calculation and can also manipulate it.
But nothing is perfect, web app also have a lot of drawbacks. One of them is when it comes to physical document handling, such as document printing.
PDF generation in PHP
PDF generation in PHP gave a web-based application abilities to represent a printable, fixed- layout document to millimeter precision.Such as printable invoices, printable reports, printable manifests, and other printable documents.
In short, PDF generation is a best method to represent a printable document, compared to any other method such as html, which is "dynamic layout" by nature.
The down side is, we cannot alter the produced document and it depends on browser's PDF plugins whether to display it immediately on browser or just download it.
Here's a tutorial of how to make a PDF invoice in PHP using FPDF class.
You can download fpdf library package then extract it's content to your htdocs directory.
This package contains fpdf php library, example tutorials, and documentation.
FPDF has many methods to make and manipulate PDF content as you can see in its documentation here.
Make PDF Invoice using PHP FPDF library
First, you need to download the fpdf library here.Then extract it somewhere in your htdocs directory. Then make a php file as the pdf generator.
In this example, i am going t use portrait, A4 size paper with mm measurement unit.
<?php //call the FPDF library require('fpdf17/fpdf.php'); //A4 width : 219mm //default margin : 10mm each side //writable horizontal : 219-(10*2)=189mm //create pdf object $pdf = new FPDF('P','mm','A4'); ?>
Next, add a blank page using $pdf->AddPage(); method. Then output the result using $pdf->Output(); method.
<?php //call the FPDF library require('fpdf17/fpdf.php'); //A4 width : 219mm //default margin : 10mm each side //writable horizontal : 219-(10*2)=189mm //create pdf object $pdf = new FPDF('P','mm','A4'); //add new page $pdf->AddPage(); //output the result $pdf->Output(); ?>
You'll get something like:
Congratulations! you've just made a pdf file by yourself... Now what?...
Add content to pdf file with cells
Text content in pdf is written in cells (Imagine cells as a container). You can put text inside cells, and each cells has its own properties such as width, height, and border.Cells are stacked horizontally one after another from left to right and end with a cell which defined as a line ending by its parameter or until $pdf->Ln(); method is invoked (imagine what "enter" button do when you're typing in text editor).
Fpdf cell method have following parameters (taken from it's documentation here):
Cell(float w [, float h [, string txt [, mixed border [, int ln [, string align [, boolean fill [, mixed link]]]]]]])
w and h is for width and height.
txt is the content.
border to define the border. It could be 1 for all border, 0 for no border, and combination between L,T,R,B. Please refer to the doc for further explanation. what we need right now is 1 and 0.
ln is line ending. 1 to end the line with this cell, otherwise 0.
align is the text alignment L,C,R, no justified.
as for fill and link, we don't need it right now so kindly refer to the docs.
Setting font properties in pdf
Before adding anything to the pdf, we should first set the font that will be used. Following is the SetFont method parameters taken from here:SetFont(string family [, string style [, float size]])
family is the font such as 'Arial','Courier','Times', etc. Should be standard font or font defined by AddFont method (i'll explain it in another tutorial, meanwhile please watch this).
style is either one or combination between B,I,U or an empty string for none.
size is the font size in points.
Now we got the basics required to build our pdf invoice, let's move to the main point.
Make the printable PDF Invoice
Here is the codes for the invoice. Put this just after AddPage() and before Output().I included some comments as explanation in the code.
//set font to arial, bold, 14pt $pdf->SetFont('Arial','B',14); //Cell(width , height , text , border , end line , [align] ) $pdf->Cell(130 ,5,'GEMUL APPLIANCES.CO',0,0); $pdf->Cell(59 ,5,'INVOICE',0,1);//end of line //set font to arial, regular, 12pt $pdf->SetFont('Arial','',12); $pdf->Cell(130 ,5,'[Street Address]',0,0); $pdf->Cell(59 ,5,'',0,1);//end of line $pdf->Cell(130 ,5,'[City, Country, ZIP]',0,0); $pdf->Cell(25 ,5,'Date',0,0); $pdf->Cell(34 ,5,'[dd/mm/yyyy]',0,1);//end of line $pdf->Cell(130 ,5,'Phone [+12345678]',0,0); $pdf->Cell(25 ,5,'Invoice #',0,0); $pdf->Cell(34 ,5,'[1234567]',0,1);//end of line $pdf->Cell(130 ,5,'Fax [+12345678]',0,0); $pdf->Cell(25 ,5,'Customer ID',0,0); $pdf->Cell(34 ,5,'[1234567]',0,1);//end of line //make a dummy empty cell as a vertical spacer $pdf->Cell(189 ,10,'',0,1);//end of line //billing address $pdf->Cell(100 ,5,'Bill to',0,1);//end of line //add dummy cell at beginning of each line for indentation $pdf->Cell(10 ,5,'',0,0); $pdf->Cell(90 ,5,'[Name]',0,1); $pdf->Cell(10 ,5,'',0,0); $pdf->Cell(90 ,5,'[Company Name]',0,1); $pdf->Cell(10 ,5,'',0,0); $pdf->Cell(90 ,5,'[Address]',0,1); $pdf->Cell(10 ,5,'',0,0); $pdf->Cell(90 ,5,'[Phone]',0,1); //make a dummy empty cell as a vertical spacer $pdf->Cell(189 ,10,'',0,1);//end of line //invoice contents $pdf->SetFont('Arial','B',12); $pdf->Cell(130 ,5,'Description',1,0); $pdf->Cell(25 ,5,'Taxable',1,0); $pdf->Cell(34 ,5,'Amount',1,1);//end of line $pdf->SetFont('Arial','',12); //Numbers are right-aligned so we give 'R' after new line parameter $pdf->Cell(130 ,5,'UltraCool Fridge',1,0); $pdf->Cell(25 ,5,'-',1,0); $pdf->Cell(34 ,5,'3,250',1,1,'R');//end of line $pdf->Cell(130 ,5,'Supaclean Diswasher',1,0); $pdf->Cell(25 ,5,'-',1,0); $pdf->Cell(34 ,5,'1,200',1,1,'R');//end of line $pdf->Cell(130 ,5,'Something Else',1,0); $pdf->Cell(25 ,5,'-',1,0); $pdf->Cell(34 ,5,'1,000',1,1,'R');//end of line //summary $pdf->Cell(130 ,5,'',0,0); $pdf->Cell(25 ,5,'Subtotal',0,0); $pdf->Cell(4 ,5,'$',1,0); $pdf->Cell(30 ,5,'4,450',1,1,'R');//end of line $pdf->Cell(130 ,5,'',0,0); $pdf->Cell(25 ,5,'Taxable',0,0); $pdf->Cell(4 ,5,'$',1,0); $pdf->Cell(30 ,5,'0',1,1,'R');//end of line $pdf->Cell(130 ,5,'',0,0); $pdf->Cell(25 ,5,'Tax Rate',0,0); $pdf->Cell(4 ,5,'$',1,0); $pdf->Cell(30 ,5,'10%',1,1,'R');//end of line $pdf->Cell(130 ,5,'',0,0); $pdf->Cell(25 ,5,'Total Due',0,0); $pdf->Cell(4 ,5,'$',1,0); $pdf->Cell(30 ,5,'4,450',1,1,'R');//end of line
Save then refresh your browser. If everything is alright. you should have something like this.
Congratulation! You've just made a printable pdf invoice.
If by any chance you're still confused after following this tutorial, please do enjoy this video-version of this tutorial. And if you want the source code, feel free to ask me in the comment section of aforementioned video.
Subscribe to:
Post Comments
(
Atom
)
Thanks for sharing,. I enjoy reading of this article a lot. I love to do PSD to Cratejoy conversion in programming languages.
ReplyDeleteGreat Article
DeleteCyber Security Projects for CSE Students
JavaScript Training in Chennai
Project Centers in Chennai
JavaScript Training in Chennai
Bro I need source code
ReplyDeletekit tattoo When Cortez landed on the Mexican coast in 1519 he was horrified to find the natives practicing devil worshiping and had somehow permanently marked images of their idols on their skin. He called it the work of the devil.
ReplyDeletemas, kalo semisal ngambil data di database nya text nya banyak itu harus diapaain mas biar nggak lurus gitu isiny di cell nya.
ReplyDeletePakai Multicell mas.
DeleteMisal:
$message = "Thank you for ordering at the Nettuts+ online store. Our policy is to ship your materials within two business days of purchase. On all orders over $20.00, we offer free 2-3 day shipping. If you haven't received your items in 3 busines days, let us know and we'll reimburse you 5%.\n\nWe hope you enjoy the items you have purchased. If you have any questions, you can email us at the following email address:";
$pdf->MultiCell(0, 15, $message);
I just want to let you know that I just check out your site and I find it very interesting and informative.. online invoice app
ReplyDeleteI don’t have any idea about commenting on someone’s blog but it’s the first time I really want to appreciate the hard work of this blog admin for sharing this post with us.
ReplyDeleteEducation Franchise India
Spoken English Franchise
Franchise For Spoken English Classes
Top Education Franchise In India
Best Education Franchise In India
Computer Education Franchise
Education Franchise India
Computer Center Franchise
Education Franchise Opportunities In India
Great info. The content you wrote is very interesting to read. This will be loved by all age groups.
ReplyDeleteDevOps Training in Chennai
Best DevOps Training in Chennai
DevOps Training institute in Chennai
Azure Training in Chennai
VMware Training in Chennai
RPA Training in Chennai
DevOps Training in Velachery
DevOps Training in Tambaram
DevOps Training in Adyar
DevOps Training in Vadapalani
Please my dear, send me the source code (Generate Printable Invoice in PHP using FPDF library). my e-mail is: levis21307@gmail.com
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteamazing.........
ReplyDeleteyou are the best.......
but could you please teach us how to send fpdf as attachment in email using the fpff Attachments library.... thank you in advance
I really liked and I got some innovative ideas for improving my thoughts from well defined content.
ReplyDeleteIELTS Coaching in Chennai
IELTS Coaching centre in Chennai
French Classes in Chennai
pearson vue test center in chennai
Informatica Training in Chennai
spoken english coaching centre in chennai
Data Analytics Courses in Chennai
spanish classes in chennai
content writing course in chennai
IELTS Coaching in OMR
IELTS Coaching in Porur
Very good points you wrote here..Great stuff...I think you've made some truly interesting points.Keep up the good work. Free TikTok Fans
ReplyDeleteI believe there are many more pleasurable opportunities ahead for individuals that looked at your site.
ReplyDeleteQuoting & Invoicing Software
It is an informative blog. I would like to know more information. Anyway thanks a lot for sharing this post. Those guidelines additionally worked to become a good way to recognize that other people online have the identical favour like mine to grasp great deal more around this condition.
ReplyDeleteData Science Training In Chennai
Data Science Online Training In Chennai
Data Science Training In Bangalore
Data Science Training In Hyderabad
Data Science Training In Coimbatore
Data Science Training
Data Science Online Training
Expected to form you a next to no word to thank you once more with respect to the decent recommendations you've contributed here.
ReplyDeleteQuoting & Invoicing Software
Amazing Article, Really useful information to all So, I hope you will share more information to be check and share here.
ReplyDeleteinplant training
inplant training chennai
inplant training in chennai
inplant training at chennai
inplant training
inplant training chennai
inplant training in chennai
inplant training at chennai
inplant training
Inplant Training for cse
https://www.oobbg.com 우리카지노사이트
ReplyDeletehttps://www.oobbg.com/theking 더킹카지노
https://www.oobbg.com/sands 샌즈카지노
https://www.oobbg.com/first 퍼스트카지노
https://www.oobbg.com/yes 예스카지노
https://www.oobbg.com/coin 코인카지노
https://www.oobbg.com/33 33카지노
https://www.oobbg.com/world 월드카지노
https://www.oobbg.com/merit 메리트카지노
https://www.oobbg.com/gatsby 개츠비카지노
https://www.omcyy.com 우리카지노사이트
ReplyDeletehttps://www.omcyy.com/thekingcasino 더킹카지노
https://www.omcyy.com/sandscasino 샌즈카지노
https://www.omcyy.com/firstcasino 퍼스트카지노
https://www.omcyy.com/yescasino 예스카지노
https://www.omcyy.com/supercasino 슈퍼카지노
https://www.omcyy.com/gatsbycasino 개츠비카지노
https://www.omcyy.com/33casino 33카지노
https://www.omcyy.com/worldcasino 월드카지노
https://www.omcyy.com/merit 메리트카지노
https://www.bbdd66.com 우리카지노사이트
ReplyDeletehttps://www.bbdd66.com/theking 더킹카지노
https://www.bbdd66.com/sands 샌즈카지노
https://www.bbdd66.com/first 퍼스트카지노
https://www.bbdd66.com/yes 예스카지노
https://www.bbdd66.com/super 슈퍼카지노
https://www.bbdd66.com/gatsby 개츠비카지노
https://www.bbdd66.com/33 33카지노
https://www.bbdd66.com/world 월드카지노
https://www.bbdd66.com/merit 메리트카지노
https://www.omgab.com 우리카지노사이트
ReplyDeletehttps://www.omgab.com/theking 더킹카지노
https://www.omgab.com/sands 샌즈카지노
https://www.omgab.com/first 퍼스트카지노
https://www.omgab.com/yes 예스카지노
https://www.omgab.com/super 슈퍼카지노
https://www.omgab.com/gatsby 개츠비카지노
https://www.omgab.com/33 33카지노
https://www.omgab.com/world 월드카지노
https://www.omgab.com/merit 메리트카지노
Great job, this is essential information that is shared by you. This information is meaningful and very important for us to increase our knowledge about it. Always keep sharing this type of information. Thanks once again for sharing it. Free Online Invoice Generator India
ReplyDeleteterimakasih atas blog nya, sekarang aplikasi invoice sudah dapat mempermudah segalanya
ReplyDeletenice blog, click for best wholesaling software for your need.
ReplyDelete