PHP-GTK2 Newsletter

PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou




How to print barcode using GS k (ESC/POS Command)
Written by kksou   
Wednesday, 08 October 2008

This article shows you how to use the GS k (ESC/POS Command) to print barcodes on the receipt printer.

It's a bit complicated because there are different rules for the different barcode systems.

Note: For all the following sample codes, I assume that the barcode is stored in the variable $barcode.


EAN13 (JAN13)

Sample code:

fwrite($handle, chr(hexdec('1D')).'k'.chr(2)); // EAN13
fwrite($handle, $barcode.chr(0));

Note that $barcode has to be 12 or 13 characters.

The 13th character for EAN13 is the check digit. On a standard barcode scanner, there is usually a dip switch that allows you to set whether to return the check digit. If you set it to return the check digit, the barcode scanner will give 13 digits. Otherwise, it will return only 12 digit.

As for the receipt printer, you can send in the barcode with or without the check digit.

You may want to refer to the following for a complete sample code:

Sample Code 479: How to print barcode label - Part 1 - EAN13 barcodes?

UPC-A

Sample code:

fwrite($handle, chr(hexdec('1D')).'k'.chr(0)); // UPC-A
fwrite($handle, $barcode.chr(0));

Note that $barcode has to be 11 or 12 characters.

The 12th character for UPC-A is the check digit. On a standard barcode scanner, there is usually a dip switch that allows you to set whether to return the check digit. If you set it to return the check digit, the barcode scanner will give 12 digits. Otherwise, it will return only 11 digit.

As for the receipt printer, you can send in the barcode with or without the check digit.

You may want to refer to the following for a complete sample code:

Sample Code 480: How to print barcode label - Part 2 - UPC A barcodes?

UPC-E

Sample code:

fwrite($handle, chr(hexdec('1D')).'k'.chr(1)); // UPC-E
fwrite($handle, $barcode.chr(0));

Important!!! the $barcode has to be 11 or 12 characters.

This is one of the most tricky ones!

The UPC-E barcode is the short form representation of a UPC number. It reduces the data length from 12 digits to 6 digits by compressing the extra zeros as shown below. It is generally used on products with very small packaging where a full UPC-A barcode couldn't reasonably fit.

Try scanning a UPC-E barcode on a barcode scanner. How many characters did it return? If you have turned on the check digit, most likely you'll get 8 characters.

However, on a receipt printer, it is required that the barcode be 11 or 12 characters. If you pump the 8 characters (i.e. the numbers returned directly from the barcode scanner) directly into the receipt printer, the printer will refuse to print any barcode!

The solution is to convert the UPC-E barcode to the UPC-A format first before you can print the barcode.

The complete solution is given in the following sample code:

Sample Code 481: How to print barcode label - Part 3 - UPC E barcodes?

EAN8 (JAN8)

Sample code:

fwrite($handle, chr(hexdec('1D')).'k'.chr(3)); // EAN8
fwrite($handle, $barcode.chr(0));

Note that $barcode has to be 7 or 8 characters.



User reviews   Average user ratings:    4.0   (from 2 users)
  1. Birkoff Ferguson
    July 13, 2009 10:08am
    CODE128

    Hello, Nice tutorial...
    What about CODE128? Please add the sample code for CODE128.

    Thank you

  2. cristian briones ruiz
    July 07, 2010 1:44pm
    Code for PDF417

    Good afternoon.

    Would be so kind as to tell me what are the control codes to activate the printing of PDF417 barcodes
    I have the encoded text, but I could not find the sequence of codes to activate the printing of PDF417
    The printer manual says that if you can print PDF417, but does not have a precise example of how.

    I am using a printer Sewoo WTP-150 is now called LK-T200 compatible with EPSON thermal printer

    thank you very much for taking the time.

Note: You have to be a registered member to leave a comment. Free registration here.

 
Next >

Blog - Forum - Privacy Policy - Contact Us
Copyright © 2006-2009. kksou.com. All Rights Reserved