Incidentally, the receipt printed in the above video is an example from the escpos-php repository. I’ll step through this print-out, as it demonstrates all of the basic ESC/POS features.
Command structure
Four specific ASCII characters make appearances in the command sequences-
Abbreviation
Name
Code (Hex)
NUL
Null
0x00
LF
Line Feed
0x0A
ESC
Escape
0x1B
GS
Group Separator
0x1D
Regular text is simply sent to the printer, separated by line-breaks. Commands begin with ESC or GS, and are followed by a printable character, and sometimes some numbers
Numbers are simply passed as a character. For example, ‘5’ is passed as 0x05.
Examples
These examples are taken from the output of demo.php.
Initialisation
When you first connect to the printer, you should initialise it. This reverts to default formatting, rather than the triple-underlined double-strike font which the previous print-out may have been using.
This is the simplest type of receipt, and contains only unformatted text.
Text is simply sent to the printer.
Each line must be terminated by a line feed.
1
2
3
4
5
6
7
8
9
10
require__DIR__.'/autoload.php';useMike42\Escpos\Printer;useMike42\Escpos\PrintConnectors\FilePrintConnector;$connector=newFilePrintConnector("/dev/usb/lp0");$printer=newPrinter($connector);/* Text */$printer->text("Hello world\n");$printer->cut();$printer->close();
The printer can quickly skip past a given number of lines with this command.
The commands are:
LF
ESC d [ number ]
ESC v [ number ]
The first command feeds forward, the second feeds in reverse. From the example, it can be seen that the demo printer does not support reverse paper feeding.
1
2
3
4
5
6
7
8
9
10
11
12
require_once(dirname(__FILE__)."/escpos-php/Escpos.php");$printer=newEscpos();/* Line feeds */$printer->text("ABC");$printer->feed(7);$printer->text("DEF");$printer->feedReverse(3);$printer->text("GHI");$printer->feed();$printer->cut();$printer->close();
The argument is set to 0 for no underline, 1 for underline, 2 for heavy underline.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
require__DIR__.'/autoload.php';useMike42\Escpos\Printer;useMike42\Escpos\PrintConnectors\FilePrintConnector;$connector=newFilePrintConnector("/dev/usb/lp0");$printer=newPrinter($connector);/* Underline */for($i=0;$i<3;$i++){$printer->setUnderline($i);$printer->text("The quick brown fox jumps over the lazy dog\n");}$printer->setUnderline(0);// Reset
$printer->cut();$printer->close();
require__DIR__.'/autoload.php';useMike42\Escpos\Printer;useMike42\Escpos\PrintConnectors\FilePrintConnector;$connector=newFilePrintConnector("/dev/usb/lp0");$printer=newPrinter($connector);/* Emphasis */for($i=0;$i<2;$i++){$printer->setEmphasis($i==1);$printer->text("The quick brown fox jumps over the lazy dog\n");}$printer->setEmphasis(false);// Reset
$printer->cut();$printer->close();
Use 1 to enable, or 0 to disable. On the model tested here, this appears to be identical to the “emphasis” mode above.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
require__DIR__.'/autoload.php';useMike42\Escpos\Printer;useMike42\Escpos\PrintConnectors\FilePrintConnector;$connector=newFilePrintConnector("/dev/usb/lp0");$printer=newPrinter($connector);/* Double-strike (looks basically the same as emphasis) */for($i=0;$i<2;$i++){$printer->setDoubleStrike($i==1);$printer->text("The quick brown fox jumps over the lazy dog\n");}$printer->setDoubleStrike(false);$printer->cut();$printer->close();
There are three possible fonts, documented as “A”, “B” and “C”, and numbered 0, 1, and 2. Many printers, including this one, don’t have Font C.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
require__DIR__.'/autoload.php';useMike42\Escpos\Printer;useMike42\Escpos\PrintConnectors\FilePrintConnector;$connector=newFilePrintConnector("/dev/usb/lp0");$printer=newPrinter($connector);/* Fonts (many printers do not have a 'Font C') */$fonts=array(Printer:::FONT_A,Printer:::FONT_B,Printer:::FONT_C);for($i=0;$i<count($fonts);$i++){$printer->setFont($fonts[$i]);$printer->text("The quick brown fox jumps over the lazy dog\n");}$printer->setFont();// Reset
$printer->cut();$printer->close();
Use 0 to justify left, 1 to centre the text, or 2 to right-align it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
require__DIR__.'/autoload.php';useMike42\Escpos\Printer;useMike42\Escpos\PrintConnectors\FilePrintConnector;$connector=newFilePrintConnector("/dev/usb/lp0");$printer=newPrinter($connector);/* Justification */$justification=array(Printer:::JUSTIFY_LEFT,Printer:::JUSTIFY_CENTER,Printer:::JUSTIFY_RIGHT);for($i=0;$i<count($justification);$i++){$printer->setJustification($justification[$i]);$printer->text("A man a plan a canal panama\n");}$printer->setJustification();// Reset
$printer->cut();$printer->close();
1
2
3
4
5
6
7
8
00000000 1b 40 1b 61 00 41 20 6d 61 6e 20 61 20 70 6c 61 |.@.a.A man a pla|
00000010 6e 20 61 20 63 61 6e 61 6c 20 70 61 6e 61 6d 61 |n a canal panama|
00000020 0a 1b 61 01 41 20 6d 61 6e 20 61 20 70 6c 61 6e |..a.A man a plan|
00000030 20 61 20 63 61 6e 61 6c 20 70 61 6e 61 6d 61 0a | a canal panama.|
00000040 1b 61 02 41 20 6d 61 6e 20 61 20 70 6c 61 6e 20 |.a.A man a plan |
00000050 61 20 63 61 6e 61 6c 20 70 61 6e 61 6d 61 0a 1b |a canal panama..|
00000060 61 00 1d 56 41 03 |a..VA.|
00000066
Barcodes
The commands are:
GS h [ number ]
ESC k [ number ] [ text ] NUL
The first command sets the barcode height — measured in dots, while the second one prints the actual barcode. The number represents the barcode standard, which for most purposes should be “4”, representing CODE39. 6 standards are supported by the PHP driver.
You will notice that due to driver glitches or printer incompatibility, not all of the barcodes print! As above, my advice is to use CODE39 if you run into this.