|
Problem This is in response to Mdchocho's post titled "Print barcode label".
He would like to know how to print barcode labels from within PHP-GTK application.
Printing barcode labels is a standard function in a POS (Point-of-Sales) system. With PHP-GTK2, you can also easily design a standalone application just for bulk printing of barcodes.
In this example, you will have a text entry to enter the barcode which you want to appear in the label. You can type the barcode manually. Or, if you have a barcode scanner, you can simply scan the product and the barcode will appear automatically in the textentry.

When you press Enter, or click the "Print Label" button, the barcode will appear in the receipt printer as shown below:

In this Part 1, I will first show you the printing of EAN13 barcodes, as this is the easiest and most straightforward.
Some other barcode formats such as UPC-E (which is very common in United States) are not that straightforward. We'll cover the printing of UPC-A barcodes in Part 2 and UPC-E barcodes in Part 3.
Solution
- The solution below assumes that you have a standard receipt printer such as the Epson TM-T88III receipt printer that uses the parallel port.

- These standard receipt printer comes with resident, or built-in, barcode fonts for the printing of barcodes.
- Important Note: Although you can find many php scripts out there that print barcodes in .gif or .png format, you will find that many of these barcodes, when printed on a inkjet or laset printers, are not readable by a standard barcode scanner. To print reliable barcodes that can be read by a standard barcode reader, you need to have barcode fonts. A standard receipt printer usually comes loaded with one or two barcode fonts. If your printer doesn't have one, you will need to purchase and download one onto your printer.
- The ESC/POS commands given below are for the standard Epson receipt printer such as the Epson TM-T88III. The ESC/POS commands are a set of proprietary POS printer command system developed by EPSON.
- If you have a different printer, please refer to the corresponding codes in your printer manual. But Epson is pretty much the industry standard for receipt printer and most receipt printers support these ESC/POS commands. You can just try the script below. Most likely it will work.
- You will find from the sample code below that the complexity of the barcode printing is handled by the receipt printer. Most standard receipt printers can handle a variety of barcode formats including UPC-A, UPC-E, JAN8 (EAN8), JAN13 (EAN13), CODE 39, CODE 93, CODE 128, ITF, CODABAR.
Steps for printing EAN13 barcodes
Printing of EAN13 barcodes is relatively easier and more straightforward than printing of some other barcode formats such as the UPC-E. Below are the steps:
- Select the barcode font.
- Select where to print the barcode text (above, below or none).
- Set the width and height of the barcode.
- Inform the printer that the barcode is in EAN13 format.
- Sends the EAN13 barcode to the printer. Note that you can send this as a 12-digit number (without the check digit) or a 13-digit number (with the check digit).
Sample 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
| <?php $window = new GtkWindow(); $window->set_size_request(400, 150); $window->connect_simple('destroy', array('Gtk','main_quit')); $window->add($vbox = new GtkVBox());
// display title
$title = new GtkLabel("Print Barcode Label (EAN13 only)"); $title->modify_font(new PangoFontDescription("Times New Roman Italic 10")); $title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff")); $title->set_size_request(-1, 40); $vbox->pack_start($title, 0, 0);
$hbox = new GtkHBox(); $vbox->pack_start($hbox, 0, 0); $hbox->pack_start(new GtkLabel("Enter/Scan Barcode: "), 0, 0); $hbox->pack_start($entry = new GtkEntry(), 0, 0); $hbox->pack_start($button = new GtkButton("Print Label"), 0, 0);
$entry->set_text('9781593270315'); $entry->connect('activate', 'on_enter', $button); $button->connect('clicked', 'on_click', $entry); $window->show_all(); Gtk::main();
function on_enter($entry, $button) { $keyword = $entry->get_text(); $button->clicked(); }
function on_click($button, $entry) { $barcode = $entry->get_text();
|
- 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.
- Please refer to Barcode printing on receipt printer using the ESC/POS Commands for explanations of these settings.
- Sends the EAN13 barcodes to the printer. You will now see the EAN13 barcode printed on the receipt printer! Note that this must be either 12 digits (without the checkdigit) or 13 digits (with the check digit).
- Done!
Related Links
Resources on barcodes
Here are some useful links on barcodes:
User reviews Average user ratings: 4.5 (from 3 users) Note: You have to be a registered member to leave a comment. Free registration here. |
May 29, 2008 5:00pm
May 01, 2009 9:04am
July 13, 2009 10:09am
Please give the sample code of CODE128.. Thank youuu