Tutorial creates receipt note PHP. According to my promise, I will show you how to build a sales receipt PDF. Sales receipt used as evidence that a sales transaction has been made. Each POS application facilitates users to print sales proof.
If you are familiar, create receipt note using a desktop application such as Foxpro, Visual Basic, they provided GUI Tools that can be used easily to generate receipt note. But different when using PHP, the whole of code must be written line by line. They do not provide the tools.
The problem is, to styling the receipt note using PHP is pretty hard to do. But this time, I will share to you in free how to make a receipt sales PHP using FPDF library. In this case, we will create a receipt note in 1/2 A4 paper sizes.
Continuing my tutorial “Tutorial Build Point Of Sale PHP And Jquery,” I promised to share a gift how to create a PDF receipt note as a bonus article. My loyal visitor has reminded me about that (Thank you, Greg, already told me :)).
If you are creating a point of sale apps, it should have the feature to print the receipt note on the sales module. One alternative to making a sales receipt on PHP is to use the PDF file. Using the FPDF library, we can create PDF files easily. But for the receipt note is slightly different, the receipt note has a smaller size and more complicated content.
Another article: Trick How To Print Report Without Open New Tab Page With Javascript
I created a PHP script to print notes in a simple PDF form. This simple script can develop by yourself as you wish. The receipt structure that I created as shown below
Table of Contents
Build Receipt Note PDF With FPDF Library In 6 Steps
As a trial tool, we need to prepare some necessary materials, so the example will work properly.
1. Create an MYSQL database
We need a database that created earlier from the tutorial “create a sales form in Point Of Saleย PHP, MySQL, And Jquery.“. Download from this article.
2. Create new data tables
Copy dan paste the following code to create t_sale dan t_sale_detail tables.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | DROP TABLE IF EXISTS 't_sale'; CREATE TABLE 't_sale' ( 'sale_id' char(10) NOT NULL, 'id_user' int(11) NOT NULL, 'sale_date' date NOT NULL, 'input_date' timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, 'paid' int(14) DEFAULT NULL, 'disc_prcn' decimal(5,2) DEFAULT '0.00', 'disc_rp' int(14) DEFAULT '0', 'sts' int(1) NOT NULL DEFAULT '1' COMMENT '1 aktif, 0 delete', 'note' text, PRIMARY KEY ('sale_id') ); insert into t_sale ('sale_id','id_user','sale_date','input_date','paid','disc_prcn','disc_rp','sts','note') values ('J170420001',6,'2017-04-20','2017-08-10 10:00:05',11000,8.33,1000,1,'test note'); DROP TABLE IF EXISTS 't_sale_detail'; CREATE TABLE 't_sale_detail' ( 'autoid' int(11) NOT NULL AUTO_INCREMENT, 'sale_id' char(10) NOT NULL, 'id_item' varchar(6) NOT NULL, 'item_name' varchar(150) NOT NULL, 'qty' decimal(14,2) DEFAULT NULL, 'unit' varchar(4) NOT NULL, 'price' int(14) DEFAULT NULL, 'disc_prc' decimal(5,2) DEFAULT '0.00', 'disc_rp' int(14) DEFAULT '0', PRIMARY KEY ('sale_id','id_item','item_name','unit'), KEY 'autoid' ('autoid') ); insert into t_sale_detail ('autoid','sale_id','id_item','item_name','qty','unit','price','disc_prc','disc_rp') values (1,'J170420001','PB0001','Razor Blade',1.00,'PCS',12000,0.00,1000); |
3. Download JQuery Redirect Plugin
Using the redirect plugin, you can create HTTP POST and GET redirection quickly. Please download the library here.
4. Create a new PHP file named printnote.php
Copy the following script in the HTML body
1 2 3 4 5 6 7 8 9 10 11 | <table> <tr> <td>Sales ID</td> <td><input type="input" id="id" name="id"></td> </tr> <tr> <td> <button id="print">Print Struck PDF</button> </td> </tr> </table> |
Add JQuery script to do print the receipt note
1 2 3 4 5 6 7 | <script type="text/javascript" src="https://cdn.rawgit.com/mgalante/jquery.redirect/master/jquery.redirect.js"></script> <script type="text/javascript"> $("#print").click(function(){ var id = $("#id").val(); $.redirect("struck.php",{ id_sales: id ,duplicate:0},'POST','_blank'); }) </script> |
5. Download FPDF library
Edit FPDF library and add a new paper type and size called “struck.” Change the following code in FPDF.php:
1 2 3 4 5 6 7 8 | // Page sizes $this->StdPageSizes = array('a3'=>array(841.89,1190.55), 'a4'=>array(595.28,841.89), 'a5'=>array(420.94,595.28), 'letter'=>array(612,792), 'legal'=>array(612,1008), 'struck'=>array(612.28,396.85), 'folio'=>array(595.28,936)); |
6. Create a new PHP named struck.php
copy the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 | <?php session_start(); header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); header("Cache-Control: post-check=0, pre-check=0", false); header("Pragma: no-cache"); include "fpdf.php"; include " ../../ library/config. php"; require_once (".. /model/ dbconn. php"); require_once (".. /model/ pos. php"); if(!isset($_POST['id_sales'])) { die("Sorry, Browser can not be refreshed for data security. Please reprint through reprint form!!"); } $id_sales=$_POST['id_sales']; $pos = new pos(); $sale = $pos->getSaleId($id_sales); if($sale[0] == false) { die('Error : '.$sale[1]); } $transaction_date = $sale[1]['sale_date']; $customer = 'Customer'; $disc_rp =$sale[1]['disc_rp']; $tax =0; $note=$sale[1]['note']; $nopage = 1; $nopagelast = 0; $cashier = $sale[1]['username']; if($_POST['duplicate']==1){ $duplicate = ' (Duplicate Note)'; }else{ $duplicate = ''; } $xshop = $pos->getrefsytem(); $shop = $xshop[1]['name_shop']; $address = $xshop[1]['address_shop']; $phone = $xshop[1]['phone_shop']; class PDF extends FPDF { function AddPage($orientation='', $format='') { if($this->state==0) $this->Open(); $family=$this->FontFamily; $style=$this->FontStyle.($this->underline ? 'U' : ''); $size=$this->FontSizePt; $lw=$this->LineWidth; $dc=$this->DrawColor; $fc=$this->FillColor; $tc=$this->TextColor; $cf=$this->ColorFlag; if($this->page>0) { $this->InFooter=true; $this->Footer(); $this->InFooter=false; $this->_endpage(); } $this->_beginpage($orientation,$format); $this->_out('2 J'); $this->LineWidth=$lw; $this->_out(sprintf('%.2F w',$lw*$this->k)); if($family) $this->SetFont($family,$style,$size); $this->DrawColor=$dc; if($dc!='0 G') $this->_out($dc); $this->FillColor=$fc; if($fc!='0 g') $this->_out($fc); $this->TextColor=$tc; $this->ColorFlag=$cf; $this->InHeader=true; $this->Header(); $this->InHeader=false; if($this->LineWidth!=$lw) { $this->LineWidth=$lw; $this->_out(sprintf('%.2F w',$lw*$this->k)); } if($family) $this->SetFont($family,$style,$size); if($this->DrawColor!=$dc) { $this->DrawColor=$dc; $this->_out($dc); } if($this->FillColor!=$fc) { $this->FillColor=$fc; $this->_out($fc); } $this->TextColor=$tc; $this->ColorFlag=$cf; } function Header() { global $transaction_date,$customer,$id_sales,$shop,$address,$phone; $this->SetFont('Courier','',9); $this->setXY(10,10); $this->Cell(20,0,'No Trans.', 0, '0', 'L'); $this->Cell(10,0,':', 0, '0', 'C'); $this->Cell(20,0,$id_sales, 0, '0', 'L'); $this->setXY(10,15); $this->Cell(20,0,'Date', 0, '0', 'L'); $this->Cell(10,0,':', 0, '0', 'C'); $this->Cell(20,0,date('d/m/Y',strtotime($transaction_date)), 0, '0', 'L'); $this->setXY(10,20); $this->Cell(20,0,'Customer', 0, '0', 'L'); $this->Cell(10,0,':', 0, '0', 'C'); $this->Cell(20,0,$customer, 0, '0', 'L'); $this->SetFont('Arial','',14); $this->setXY(130,8); $this->Cell(20,0,$shop, 0, '0', 'L'); $this->SetFont('Courier','',9); $this->setXY(130,12); $this->MultiCell(80,3,$address, 0, 'L', false); $this->setXY(130,21); $this->Cell(20,0,'Phone : '.$phone, 0, '0', 'L'); $this->SetFont('Courier','',9); $this->SetDash(1,1); $this->line(10,25,206,25); $this->Ln(20); } function Footer() { global $duplicate,$note,$nopagelastx,$cashier; $this->SetY(-7); $this->SetDash(1,1); $this->line(10,130,206,130); $this->SetFont('Arial','I',7); $this->Cell(0,0,'cashier : '.$cashier.' , Hal : '.$this->PageNo().'/{nb}'.$duplicate,0,0,'l'); $this->SetXY(10,-4); $this->write(0,date('d/m/Y H:i')); $this->SetY(-15); $last = count($this->pages); if($this->PageNo() == $nopagelastx){ $this->SetY(-20); $this->cell(40,0,'Cashier/Manager', 0, 'L', false); $this->cell(0,0,'Customer', 0, 'L', false); $this->SetY(-35); $this->Cell(40,0,'Best Regard ,',0,0,'l'); $this->Cell(0,0,'Receiver ,',0,0,'l'); } } function SetDash($black=null, $white=null) { if($black!==null) $s=sprintf('[%.3F %.3F] 0 d',$black*$this->k,$white*$this->k); else $s='[] 0 d'; $this->_out($s); } function header_note() { global $y,$baris; $this->SetFont('Courier','',9); $this->setXY(10,27); $this->Cell(7,0,'NO', 0, '0', 'L'); $this->Cell(100,0,'Item', 0, '0', 'C'); $this->Cell(15,0,'Qty', 0, '0', 'C'); $this->Cell(30,0,'Price', 0, '0', 'R'); $this->Cell(15,0,'Disc%', 0, '0', 'C'); $this->Cell(30,0,'Total', 0, '0', 'R'); $this->SetDash(1,1); $this->line(10,30,206,30); } function AddPageNew() { global $y,$baris,$default_y,$nopage; $this->AddPage('P','struck'); $this->AliasNbPages(); $y = $default_y; $baris=1; $nopage++; } } $pdf=new PDF(); $pdf->SetAutoPageBreak(true,1); $pdf->AddPage('P','struck'); $pdf->AliasNbPages(); $pdf->header_note(); $pdf->isFinished = true; $default_y = 34; $y = $default_y; $default_footer = 19; $baris = 1; $subtotal=0; $detail_sale=$pos->getSaleDetailIdSale($id_sales); foreach ($detail_sale[1] as $key) { if($baris >= 16 ) { $pdf->line(10,$y,206,$y); $pdf->setXY(157,$y+3); $pdf->Cell(20,0,'Subtotal : Rp.', 0, '0', 'R'); $pdf->Cell(30,0,number_format($subtotal), 0, '0', 'R'); $pdf->setXY(187,$y+8); $pdf->SetFont('Courier','',7); $nexthal =$nopage + 1; $pdf->Cell(20,0,'Next page : '.$nexthal, 0, '0', 'R'); $pdf->SetFont('Courier','',9); $pdf->AddPage('P','struck'); $pdf->AliasNbPages(); $pdf->header_note(); $y = $default_y; $baris=1; $nopage++; } $pdf->setXY(10,$y); $pdf->Cell(7,0,$key['urutan'].'.', 0, '0', 'L'); $pdf->Cell(100,0,substr($key['item_name'], 0,60), 0, '0', 'L'); $pdf->Cell(15,0,number_format($key['qty']).' '.$key['unit'], 0, '0', 'C'); $pdf->Cell(30,0,number_format($key['price']), 0, '0', 'R'); $pdf->Cell(15,0,number_format($key['disc_prc'],2), 0, '0', 'C'); $pdf->Cell(30,0,number_format($key['total']), 0, '0', 'R'); $y+=5; $baris++; $subtotal+=$key['total']; } $y-=2; $pdf->SetDash(1,1); $pdf->line(10,$y,206,$y); if($baris>=$default_footer) { $pdf->AddPageNew(); }else { $y+=5; } $pdf->setXY(157,$y); $pdf->Cell(20,0,'Subtotal : Rp.', 0, '0', 'R'); $pdf->Cell(30,0,number_format($subtotal), 0, '0', 'R'); $baris++; if($baris>=$default_footer) { $pdf->AddPageNew(); }else { $y+=5; } $pdf->setXY(157,$y); $pdf->Cell(20,0,'Disc : Rp.', 0, '0', 'R'); $pdf->Cell(30,0,number_format($disc_rp), 0, '0', 'R'); $pdf->SetDash(1,1); $pdf->line(175,$y+2,206,$y+2); $baris++; if($baris>=$default_footer) { $pdf->AddPageNew(); }else { $y+=5; } $nopagelast=$nopage; $pdf->setXY(157,$y); $pdf->Cell(20,0,'Total : Rp.', 0, '0', 'R'); $pdf->SetFont('Courier','B',9); $pdf->Cell(30,0,number_format($subtotal - $disc_rp), 0, '0', 'R'); $pdf->SetFont('Courier','',9); $xtotal = $subtotal - $disc_rp; if($tax >0) { $nopagelast=$nopage; $pdf->setXY(157,$y); $pdf->Cell(20,0,'Total : Rp.', 0, '0', 'R'); $pdf->SetFont('Courier','B',9); $pdf->Cell(30,0,number_format($subtotal - $disc_rp), 0, '0', 'R'); $pdf->SetFont('Courier','',9); $xtotal = $subtotal - $disc_rp; $baris++; if($baris>=$default_footer) { $pdf->AddPageNew(); }else { $y+=5; } $nopagelast=$nopage; $pdf->setXY(157,$y); $pdf->Cell(20,0,'tax : Rp.', 0, '0', 'R'); $pdf->SetFont('Courier','B',9); $pdf->Cell(30,0,number_format($tax), 0, '0', 'R'); $pdf->SetFont('Courier','',9); $baris++; $pdf->SetDash(1,1); $pdf->line(175,$y+2,206,$y+2); if($baris>=$default_footer) { $pdf->AddPageNew(); }else { $y+=5; } $nopagelastx=$nopage; $pdf->setXY(157,$y); $pdf->Cell(20,0,'Grand Total : Rp.', 0, '0', 'R'); $pdf->SetFont('Courier','B',9); $pdf->Cell(30,0,number_format($xtotal + $tax), 0, '0', 'R'); $pdf->SetFont('Courier','',9); }else { $nopagelastx=$nopage; $pdf->setXY(157,$y); $pdf->Cell(20,0,'Total : Rp.', 0, '0', 'R'); $pdf->SetFont('Courier','B',9); $pdf->Cell(30,0,number_format($subtotal - $disc_rp), 0, '0', 'R'); $pdf->SetFont('Courier','',9); $xtotal = $subtotal - $disc_rp; } $pdf->Output(); |
Until this step, tutorial creating print receipt note PHP PDF has been complete. Run the script above from your browser. If working correctly, the application running like the following snippet video
Download Source Code
To download the receipt sales PHP PDF source code, please share this article from button below to open the download link
[sociallocker id=58]
URL :ย https://seegatesite.com/download-example-project-how-to-create-pdf-receipt-note-using-php/
Password : seegatesite.com
[/sociallocker]
Conclusion
Using the code above, you can implement the receipt note to your PHP Point Of Sale Application. You can modify the script as you wish.
Until now, I still used FPDF Library to create a report or receipt sales to my PHP project. If you want to try another way, using JSPDF is the second opinion. But, I not yet to try it.
So my article on PHPtutorial how to create a PDF receipt note using FPDF Plugin, hopefully, useful
Thanks so much Sigit.
Your site help me to refresh and learn new programming techniques and skills.
Best Regards,
Greg
you’re welcome greg ๐
FPDF error: Some data has already been output, can’t send PDF file
there are an error in your script…please check again ๐
cant see the download lnk help
please unlocked the link above to get download link ๐
Where do you put the files that are created?
the pdf file not created but open in the pdf browser
Hi Sigit,
Thanks for replying. What i mean is where do i put the font folder, the fpdf.php, printnote.php, and struck.php files?
Hi Robb,
put the font folder, the fpdf.php, printnote.php, and struck.php files in the same folder
๐
Hi Sigit,
I still can’t get it to work. I download the source code that contains the 3 php files and the font folder. However, I don’t know where to put it within the pos folder. I placed the files in the likely places within the pos folder but when i process the payment, a new window does open but it just goes back to the dashboard. hope you can help again.
Hi Sigit,
I downloaded the source code that contains the folder and the three php files. What I want to know is, where do I put them within the pos folder? Also, just to make sure, I extract the jquery-redirect-master.zip to the pos/plugins folder right?
the current location of the folder is not a big problem, you can place it anywhere and change the file path only.
Try put the three php file in the same folder with pos folder.
just adjust path in the following code in struck.php
change the path of your application code where the struck.php is called
what is zip password..?
seegatesite.com
Hi, there is not link to download after shared on facebook .
kindly provide the download link
here :
https://seegatesite.com/download-example-project-how-to-create-pdf-receipt-note-using-php/
Hi Sigit,
can I ask you, what is these three files actually?
include ” ../../ library/config. php”;
require_once (“.. /model/ dbconn. php”);
require_once (“.. /model/ pos. PHP”);
is it in fpdf library?
hello, you can download all file project to understand these file
https://seegatesite.com/create-a-sales-form-pos-tutorial-build-point-of-sale-with-php-pdo-mysql-and-jquery-part-5/
FPDF error: Unknown page size: a4. why am getting this error.
check your FPDF.php and the search a4 or A4