Invoice Management in a PHP Invoicing System - Code for Creating an Invoice
(Page 4 of 6 )
Let’s take a look at the code behind creating a PDF invoice:
<?php
include "config.php";
//retrieve the invoice info for the client
$query="SELECT *,DATE_FORMAT(inv_date,'%D %M %Y') as idate FROM invoices where invno='".$_GET['cid']."'";
$results=mysql_query($query);
if(!$results){
exit(mysql_error());
}else{
$row = mysql_fetch_array($results);
}
$query="SELECT name,address FROM client where id='".$_GET
['cid']."'";
$result1 = mysql_query($query);
if (!$result1) {
$error = mysql_error();
}else{
$cname = mysql_fetch_array($result1);
}
$pdf = pdf_new();
//SET PDF INFO
pdf_open_file($pdf, 'c:\\'.$cname['name'].''.$td.'.pdf');
pdf_set_info($pdf, "Author","InvoiceGen");
pdf_set_info($pdf, "Title","Invoice");
pdf_set_info($pdf, "Creator", "InvoiceGen");
pdf_set_info($pdf, "Subject", "Invoice");
$x=595;
$y=842;
//start building the page
pdf_begin_page($pdf, $x, $y);
pdf_set_value($pdf, 'textrendering', 0); // fill
//set the font
$tahoma = pdf_findfont($pdf, "Tahoma", "host", 1);
//write the word INVOICE at the top of the page
pdf_setfont($pdf, $tahoma, 11);
pdf_show_xy($pdf, "RE: Invoice ",250,$y-20);
//Now add the text - your address
pdf_setfont($pdf, $tahoma, 11);
pdf_show_xy($pdf, 'PDF Invoice Generator plc.',400,$y-40);
pdf_continue_text($pdf, '64 Martyn Lane');
pdf_continue_text($pdf, 'Totley, TA4 6QQ');
pdf_continue_text($pdf, '');
pdf_continue_text($pdf, 'Invoice Number:'.$row['invno'].'');
pdf_continue_text($pdf, ''.$row['idate'].'');
//Client Address
pdf_setfont($pdf, $tahoma, 11);
pdf_show_xy($pdf, ''.$cname['name'].'',20,$y-100);
pdf_continue_text($pdf, '12 Second Street');
pdf_continue_text($pdf, 'Yoeville, Y19 3AS');
pdf_setfont($pdf, $tahoma, 11);
pdf_show_xy($pdf, "RE: Invoice ",250,$y-20);
//Draw the lines
$offset = 184; $i=0;
pdf_moveto($pdf, 20,$y-160);
pdf_lineto($pdf, $x-20,$y-160);
pdf_stroke($pdf);
pdf_moveto($pdf, $x-140,$y-160);
pdf_lineto($pdf, $x-140,80);
pdf_stroke($pdf);
//Add product description and cost on top of the line
pdf_setfont($pdf, $tahoma, 11);
pdf_show_xy($pdf, "Product Description",30,$y-150);
pdf_show_xy($pdf, "Total(incl. VAT)",$x-100,$y-150);
//write the items for which the invoice was issued
pdf_setfont($pdf, $tahoma, 10);
pdf_show_xy($pdf, "".$row['descr']."",20,$y-200);
pdf_show_xy($pdf, "".$row['totwvat']." ",$x -100,$y-200);
pdf_setfont($pdf, $tahoma, 8);
pdf_continue_text($pdf, '');
pdf_continue_text($pdf, '');
pdf_continue_text($pdf, 'VAT charged @ 17.5');
pdf_end_page($pdf);
pdf_close($pdf);
?>
I will not explain everything about creating a PDF document because it is an entire subject in its own right. I’ve put some comments in the code to give you a general idea of what is going on. Also, it might be useful if you can read the excellent article on the Dev Articles site that demonstrates how to create a PDF document with PHP. It is very basic but it will give you a very good start in exploring the various functions available. And believe me, there are a lot of PDF functions. The important ones that you need to know are:
pdf_open_file() – Creates a blank pdf file.
pdf_set_info() - Assigns properties to a pdf file, such as the name of the author, title and subject of the file.
pdf_begin_page() – This function allows you to set the width and height of the page. Currently there are three defined measures, each defining a particular type of page:
- A4 = 595 x 842
- Letter = 612 x 792
- Legal = 612 x 1008
pdf_findfont() – Searches for a font type in the location that you specify.
pdf_setfont() – Sets the font type that you specified above.
pdf_show_xy() – Writes text to a specified location on a page. X defines the horizontal location and Y defines the vertical location.
pdf_continue_text() – Writes the text on the next line; basically inserts a line break.
pdf_end_page() – Shows the end of the page.
pdf_close() – Frees the pdf object.
The above functions are enough to create a simple PDF document. For more information on how to use PDF functions in PHP, visit http://www.php.net/.
That’s all there is to showing a list of invoices in the database.
Next: Creating Unpaid.php >>
More PHP Articles
More By Leidago