Sample Code 397: How to interface to receipt printer in a point of sale system? |
|
Written by kksou
|
|
Sunday, 23 December 2007 |
|
Problem This is in response to duez1981's post titled 'php-gtk2 point of sale system'.
He is writing a point-of-sale (POS) system using PHP-GTK2, and he wants to know how to link to a receipt printer as shown below.

Solution
- A standard receipt printer such as the Epson TM-T88III receipt printer uses the parallel port.
- To print to the parallel-port receipt printer, you print through port PRN (exactly the same as printing from DOS prompt).
- From within PHP-GTK2, you need to first establish the connection with the printer by using
$handle = fopen("PRN", "w");
- Thereafter, to print anything to the printer, you just "write" to it like the file handle:
fwrite($handle, 'text to printer');
- There are newer receipt printer that uses USB. I believe you should be able to print to such printers through PRN too.
Sample Code 1 2 3
| <?php $handle = fopen("PRN", "w"); // note 1
fwrite($handle, 'text to printer'); // note 2
|
- Note that this is only 70% of the sample code. You have to be a registered member to see the entire sample code. Please login or register.
- Registration is free and immediate.
- Have some doubt about the registration? Please read this forum article.
Explanation
- Establish the connection with the receipt printer through PRN.
- Writes to the receipt printer through the file handle.
- Disconnect the printer.
Related Links
User reviews Average user ratings: 4.0 (from 13 users) Note: You have to be a registered member to leave a comment. Free registration here. |
January 20, 2008 3:18pm
Hey, Thanks for the info you have given here. I'm currently working on a similar project as duez1981, and was doing some preparational research. I will try this as soon as i get to the receipt printing stage.
But i have a question: how do i set up columns for, say, Item code, Description, price?
Please do enlighten me here...
thanks,
Micro
January 20, 2008 9:26pm
Hi Micro, Not sure if you have played with those stone-age dot-matrix printers before? You're be surprised that a standard thermal receipt printer such as the Epson TM-T88III receipt printer mentioned above works exactly like a dot-matrix printer! Each printer usually come bundled with some fixed-width fonts. And a standard receipt printer is usually 40 characters in width. (There are some with 32 characters width.) So to print columns such as item code and description, it's direct, "manual" positioning of strings with padded spaces, since they are all fixed width characters.
Regards,
/kksou
January 20, 2008 11:03pm
IC. Thanks man. I'll try it, like i said, when i come to the receipt printing stage. ur help might be needed ;)
April 07, 2008 11:16pm
April 18, 2008 11:16am
April 23, 2008 2:39am
July 29, 2008 1:12am
July 30, 2008 5:17am
September 16, 2008 1:57am
October 18, 2008 10:16am
Hi,
I'm currently in the process of creating a system that requires interaction with a receipt printer such as this. I tried various methods but found that writing directly to the printer becomes very tricky if you wish to produce a receipt that looks anywhere near decent. creating the code to produce images or tables etc is very tricky. Also we wanted to allow the clients using the system to create their own receipt templates. The method we opted for was to create a php file containing a html template. PHP code within the template allows content to be added on the fly, then pass the template through dompdf (a php based html->pdf convertor) to produce a pdf file of the exact width and length required. Then pass the pdf file to the windows printer using pdfp.exe. pdfp.exe will print the file through adobe silently in the background. To ensure we have control of the whole process and the print job we're using a class called EasyWMI to interface with the windows wmi printer objects. Using the class we can first check the status of the printer, check all its attributes, then track the print job by name and ensure its been printed. it also facilitates a way to get the exact printer error message into your application to present to the user.
As I'm sure people will comment, its better to use the printer directly as you have more control. We certainly wasn't keen on passing anything to windows to assume it would do its job properly. With this method however we've been able to emulate the same amount of control over the print job from start to finish, we are able to print very nice looking receipts, and, we can save the pdf file if required.
Cheers
Paul
December 08, 2008 10:24pm
February 16, 2009 8:16am
April 03, 2009 11:01pm
How do i convert the data on a html page to the correct dimensions of that printer on the output papel. I have redimension it isnīt ? I haev no idea, Itīs my forst time working with POS printers and i ainīt got any to test the printing . Anyone know any POS printer simulator os something like
Thans in advance