Barcode Printing via PHP

The Project

For the past two weeks, I’m very busy developing a web based application using both barcode scanner and barcode printer. It was a lot of fun and excitement. I really love this job.

The project is about an automation system for an SD card manufacturing company. Each SD card chips has its own barcode on it. Depending on its type, every 320 chips (other types are 25) the system will create a Lot Number that will serve as ID for that group when placed in a box.

Programming the Barcode Scanner is not that really hard. All you have to do is allocate a textbox with maximum length and put a submit button. When the barcode is scanned, it will release a Carriage Return at the end of the code, then will trigger your submit button. Then a JavaScript code will catch the event, therefore processing the scanned barcode so easy.

The tricky part is the barcode printer.

How to

To print barcode into a barcode printer, you need to use the printers own programming language and some standard codes. Moreover, you need to configure Apache in order to print. Here are the initial configurations:

  1. Install the Barcode Printer on any PC / workstation you want.
  2. Share the Barcode Printer and be sure to grant the necessary access so that our server will be able to print.
  3. On the Web Server, configure Apache to run as a user and grant that user access to the printer.
  4. Configure PHP to have the php_printer.dll enabled (sorry, Windows only!)
  5. The step above only applies if you are running PHP via web. When you run it via command line, you only need to grant print access to the user whom the script is run.
  6. Do the dirty codes.

The codes

The codes for printing is actually very simple. Of course you need to configure your PHP first as mentioned above.

First: Open the printer. printer_open()

Second: Set printing mode to raw. printer_set_option()

Third: Write data (printer commands) printer_write()

You will only need these three simple functions as far as barcode printing is concern. The rest of the complexity if on the printer commands and printer settings.

So to sum up the codes, here it is:

//open printer
$handle = printer_open('Printer Name'); //or it could be \\pcname\printername
//set printing option to raw
printer_set_option($handle, PRINTER_MODE, 'RAW');
//write date to printer
$data = 'sample printer command';
$ret = printer_write($handle, $data);
//close the printer handle
printer_close($handle);

Barcode Printer Programming with SATO Barcode Printer

This last part is dedicated to the barcode printer command specified as $data on the above code. Let us discuss first the basics of Barcode Printer programming.

The client is using SATO GT408e Barcode Printer, thus I will be programming based on the printers manual. However, most of the basic commands are more or less the same as other brands.

To send commands to the printer, using any language, you need to send data to the printer as data stream. To send command correctly, we need to set the printing option to RAW so that the data will be interpreted as commands. Take this command for example:

{ESC}A
{ESC}A3H1374V0001
{ESC}H0050{ESC}V0100{ESC}L0303{ESC}XMSATO
{ESC}H0050{ESC}V0200{ESC}B103100*SATO*
{ESC}H0170{ESC}V0310{ESC}L0101{ESC}XUSATO
{ESC}Q1
{ESC}Z

{ESC} is the escape character chr(27) on your keyboard. It means that a command will follow after this character. Please note that the command above should not have any new line character. The command is a continuous string with new new line break. It is just break for readability.

The A command means that it is the beginning of all print jobs. The code is: {ESC}A

A3H1374V0001

The actual command here is A3 which means you are setting the base reference point. The base reference point is the position (in points) where all your printing positioning starts. You can think of it as the reference point where all your position calculations are based. For example, if you print something at position 0,100 (x, y) – the text or image is printed at position 0,100 starting from the base reference point.

It has parameters, the X and Y or horizontal and vertical parameters.

A3H1374V0001 = A3 H1374 V0001

The format is A3HaaaaVbbbb – where a is the 4-digit horizontal integer value and bbbb is the vertical value. Based on the code, you are setting the base reference point to 1374,1 (x,y) beginning from the topmost left portion of the printing area.

Mostly, you will only have to get the horizontal value. This is because the printing paper is not always as wide as the maximum printing area. For further details, read your manual.

{ESC}H0050{ESC}V0100{ESC}L0303{ESC}XMSATO – This is a series of commands that we need to dissect first its parts.

H0050 – sets the horizontal position (from the base reference point) for the next item to print. The syntax is Haaaa – where aaaa is a 4-digit integer value for horizontal position.

V0100 – sets the vertical position (from the base reference point) for the next item to print. Syntax is Vaaaa – still it is 4-digit.

L0303 – sets the character expansion. Expands the character horizontally and vertically. The syntax is Laabb where aa is the horizontal expansion from 0-12 and bb is the vertical expansion from 0-12.

XMSATO – the command is XM SATO, where XM is a font type. Specifies the 24W x 24H dot matrix font (includes descenders) and SATO is the text to be printed. Anything that follow XM will be printed using its font. The size is affected by the previous L0303 command which enlarges the text.

{ESC}H0050{ESC}V0200{ESC}B103100*SATO* – once again we need to separate its parts.

H0050 and V0200 positions the next text to be printed just below the previous text 200 points below.

B103100*SATO* – this is the barcode command that prints the actual barcode. The syntax is Babbcccd.

B – starts the barcode command
a – type of barcode used. Possible values are:

0: Codabar
1: Code 39
2: Interleaved 2 of 5 (I 2/5)
3: UPC-A / EAN-13
4: EAN-8
5: Industrial 2 of 5
6: Matrix 2 of 5
7: reserved
8: reserved
9: reserved
A: MSI
B: reserved
C: Code 93
D: reserved
E: UPC-E
F: Bookland
G: Code 128
I: SSCC

Note: We used Code 128 (G)

bb – Number of dots (01-12) for narrow bar and narrow space

ccc – Bar height in dots (001-999)

d – SSCC only. Not used for other bar code types
0: No human readable text
1: Human readable at top
2: Human readable at bottom

The very next data will be printed as barcodes.

On the next line, is the same as the other lines. The only difference is the XUSATO command.

XU – Font type. Specifies the 5W x 9L dot matrix font (includes descenders).

Q1 – Print Quantity. Specifies the total number of labels to print. syntax: Qaaaaa

Z – Stop Code. Ends all print jobs.

The code will print one copy of the image below:

barcode

The PHP Code

Finally, this is the code for our example:

//open printer
$handle = printer_open(’Printer Name’); //or it could be \\pcname\printername
//set printing option to raw
printer_set_option($handle, PRINTER_MODE, ‘RAW’);

//write data to printer
$esc = chr(27);
$data = ’’;
$data .= $esc . 'A';
$data .= $esc . 'A3H1374V0001';
$data .= $esc . 'H0050' . $esc . 'V0100' . $esc . 'L0303' . $esc . 'XMSATO';
$data .= $esc . 'H0050' . $esc . 'V0200' . $esc . 'B103100*SATO*';
$data .= $esc . 'H0170' . $esc . 'V0310' . $esc . 'L0101' . $esc . 'XUSATO';
$data .= $esc . 'Q1';
$data .= $esc . 'Z';

$ret = printer_write($handle, $data);
//close the printer handle
printer_close($handle);

That’s it!

This entry was posted in barcode, Web Development and tagged , , . Bookmark the permalink.

20 Responses to Barcode Printing via PHP

  1. shawn says:

    Hi there, Great article was wondering if you would like to help me with a php barcode project at my company for extra money? Let me know.

    Thanks!

  2. lysender says:

    Hi Shawn,

    I am not open for any outsourcing jobs and don’t have plans either in the future. It is not that hard, trust me. Your only problem is the underlying network and the Windows platform.

    Have several problems on that until now but we already made sort of work-around for that.

    Good luck to your business.

  3. shawn says:

    thanks for the response it looks like the latest version of php 5.3.4 has dropped the support for the windows printer dll I can’t seam to find the dll anywhere. thats pretty much where im stuck.

  4. lysender says:

    I’m using Zend Server CE for PHP 5.2 and I think that was 5.2.11 that time. I got a DLL for 5.2.8 which was still compatible. Well, that’s the only solution we got two years ago.

  5. Khen says:

    Hi lysender,

    The above codes is it workable on Linux server? I’m wondering if client side (Windows OS) triggered the printing command, will it look for client side printer device?

    Thanks in advance.

  6. lysender says:

    Khen,

    The printing extension is for Windows only and it is server side.

  7. Tred says:

    Great information. I like your ESCP examples.

    Here’s a tool I found that can print those without sharing the printer.

    http://code.google.com/p/jzebra

  8. faisal says:

    help me please..i’am use printer epson TM-T88IV. i can’t print label barcode..
    please information

  9. lysender says:

    Hi faisal,

    Check the Epson documentation if you are sending the right commands as I’m using SATO brand. Try playing around with the options to see if you can really print on the printer.

    It would be really difficult to give solution to your problem since you have not given any information.

  10. kyser says:

    Hi

    Thanks for your tuto!!!!!!!!
    very useful brother.

    I have a sato GT412, how can we print UTF-8 text
    what is the Sato command to print Japanese kanji for example
    can you give me an example of command

    thanks a lot

  11. lysender says:

    Hi kyser,

    This project is actually for a Japanese client but unfortunately they are not using any Japanese characters so I didn’t bother digging deeper about it. We need to read the SATO manual for sure they have support for such character set.

    Thanks

  12. kyser says:

    I would like to contact you in private by mail, please contact me trough mail due to the fact I don t have your(mail adress)

    thanks

  13. Guru says:

    Hello lysender,

    I’m trying to print two barcode for item number and item description
    using
    Code128 in one command.

    When printer try to execute the command, then prints as following

    1. Item number bar code and its label

    2. Only item description label not item description bar code

    Following is bar code command

    AA04060203H0475V0045BG02100ITEM NAMEH0875V0055BG02100ITEM DESCRIPTIONH0534V00155XMITEM NAMEH0534V00455XMITEM DESCRIPTIONQ3Z

  14. lysender says:

    As long as it fits on the barcode sticker well, it should be good. But if it will not fit, you better use two consecutive commands.

  15. karen says:

    hi, i able to print out the label using sato printer. but the problem is the font size is too big and my label will be standard at 5cm x 3cm . does anyone know how to change the font size just as 8px and etc?

    thanks.

  16. Waruna says:

    hello,
    this code didn’t wotk ZDesigner GC420T that printer.please help me..

  17. Yogesh Kale says:

    Hi,

    Is there any way to print barcode using TSC TTP 244 Plus printer.Currently I am using SDK of TSC TTP 244 Plus printer where It must require activeX plugin and google chrome version 39. I want an independent solution.

    Please help me in this.

  18. lysender says:

    @Yogesh Kale, this post is about server side PHP printing so active X is not required.

    This post is very old by the way and some PHP extensions may not work anymore.

  19. ATinfotech says:

    wonderful article ………it is helpful to us

  20. mary j says:

    i hv to buy the barcode printer tomorrow
    🙁
    i don’t hv any experience

Leave a Reply

Your email address will not be published. Required fields are marked *