PHP-GTK2 Newsletter

PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou



Sample Code 481: How to print barcode label - Part 3 - UPC E barcodes?
Written by kksou   
Thursday, 17 April 2008
Problem

We have covered printing of EAN13 in Part 1 and priting of UPC-A barcodes in Part 2.

In this Part 3, we will print UPC-E barcodes.

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.

To learn more about the UPC-E format and how to convert from UPC-E to UPC-A, please refer to the UPC-E Specification.

How to print barcode label - Part 3 - UPC E barcodes?


Solution

Below are the steps for printing UPC-E barcodes:

  1. Select the barcode font.
  2. Select where to print the barcode text (above, below or none).
  3. Set the width and height of the barcode.
  4. Inform the printer that the barcode is in UPC-E format.
  5. Convert the UPC-E barcode to UPC-A format.
  6. Send the UPC-A barcode to the printer. This should be a 11-digit number (without the check digit) or a 12-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   
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   
<?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('224290');
$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();
    $upc_a = upc_e_to_a($barcode); // note 1
    echo "upc_a equivalent for $barcode = $upc_a\n";
    print_barcode($upc_a);
}

function print_barcode($barcode) {
    $handle = fopen('PRN', 'w'); // note 4
    fwrite($handle, chr(hexdec('1D')).'f'.chr(0)); // note 5
    fwrite($handle, chr(hexdec('1D')).'H'.chr(2)); // note 5
    fwrite($handle, chr(hexdec('1D')).'h'.chr(60)); // note 5
    fwrite($handle, chr(hexdec('1D')).'w'.chr(2)); // note 5
    fwrite($handle, chr(hexdec('1D')).'k'.chr(1)); // note 5
    fwrite($handle, $barcode.chr(0)); // note 6
    fclose($handle); // note 7
}

function upc_e_to_a($barcode) {
    $len = strlen($barcode);
    if ($len==7 || $len==8) {
        $barcode = substr($barcode,1,6); // note 2
    }
    if (strlen($barcode)!=6) return; //note 3

    $upc='';
    $last_digit = substr($barcode, -1, 1);

    switch ($last_digit) {
        case 0:
  • 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
  1. Convert the barcode from UPC-E to UPC-A.
  2. Note that some barcode scanner will produce a 7 or 8 digits when you scan UPC-E barcodes! The first digit is the number system, which is usually 0. The 8th digit, if there's any, is the check digit.
  3. Double check that the UPC-E contains only 6 digits.
  4. Establish the connection with the receipt printer through PRN.
  5. Please refer to Barcode printing on receipt printer using the ESC/POS Commands for explanations of these settings.
  6. Sends the UPC-A barcode to the printer. You will now see the UPC-E barcode printed on the receipt printer! Note that the UPC-A number must be either 11 digits (without the checkdigit) or 12 digits (with the check digit).
  7. Done!

Related Links

Resources on barcodes

Here are some useful links on barcodes:

 

Add comment


Security code
Refresh

< Prev   Next >

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