This project implements a subset of Epson's ESC/POS protocol for thermal receipt printers. It allows you to print receipts with basic formatting, cutting, and barcode printing on a compatible printer.
It is intended for use with networked printers, but you can also use the library to save print jobs to a file.
The library should be initialised with a file pointer to the printer. For a networked printer, this can be opened via fsockopen(). For a local printer, use fopen() to open /dev/lp0 (or a serial interface, etc). If no file pointer is specified, then standard output is used.
A "hello world" receipt can be printed easily (Call this hello-world.php):
<?php
require_once(dirname(__FILE__) . "/escpos.php");
$printer = new escpos();
$printer -> text("Hello World!\n");
$printer -> cut();This would be executed as:
php hello-world.php | nc 10.x.x.x. 9100
The same output can be passed directly to a socket:
<?php
require_once(dirname(__FILE__) . "/escpos.php");
$fp = fsockopen("10.x.x.x", 9100);
$printer = new escpos($fp);
$printer -> text("Hello World!\n");
$printer -> cut();
fclose($fp);A complete receipt can be found in the code of Auth in ReceiptPrinter.php. It includes justification, boldness, and a barcode.
Construct new print object.
Parameters:
resource $fp: File pointer to print to. Will openphp://stdoutif none is specified.
Add text to the buffer. Text should either be followed by a line-break, or feed() should be called after this.
Parameters:
string $str: The string to print.
Print and feed line / Print and feed n lines
Parameters:
int $lines: Number of lines to feed
Select print mode(s).
Parameters:
int $mode: The mode to use. Default isescpos::NUL, which has a similar effect to runninginitialize().
Any of the MODE_* constants can be OR'd together and used as $mode. Valid modes are:
MODE_FONT_AMODE_FONT_BMODE_EMPHASIZEDMODE_DOUBLE_HEIGHTMODE_DOUBLE_WIDTHMODE_UNDERLINE
Turn underline mode on/off.
Parameters:
int $underline: 0 for no underline, 1 for underline, 2 for heavy underline. Defaults to 1 if not specified.
Initialize printer. This resets all modes back to default, and you may wish to place it at the top of your scripts if poorly-written applications leave the printer in a strange state.
Turn emphasized mode on/off.
Parameters:
boolean $on: true for emphasis, false for no emphasis.
Turn double-strike mode on/off.
Parameters:
boolean $on: true for double strike, false for no double strike.
Select character font.
Parameters:
int $font: The font to use. Must be either FONT_A, FONT_B, or FONT_C.
Select justification.
Parameters:
int $justification: Must be JUSTIFY_LEFT, JUSTIFY_CENTER, or JUSTIFY_RIGHT.
Print and reverse feed n lines.
Parameters:
int $lines: number of lines to feed. If not specified, 1 line will be fed.
Cut the paper.
Parameters:
int $mode: Cut mode, either CUT_FULL or CUT_PARTIAL. If not specified, CUT_FULL will be used.int $lines: Number of lines to feed before cutting. If not specified, 3 will be used.
Set barcode height.
Parameters:
int $height: Height in dots. If not specified, 8 will be used.
Print a barcode.
Parameters:
string $content: The information to encode.int $type: The barcode standard to output. If not specified, BARCODE_CODE39 will be used.
Currently supported barcode standards are (depending on your printer):
BARCODE_UPCABARCODE_UPCEBARCODE_JAN13BARCODE_JAN8BARCODE_CODE39BARCODE_ITFBARCODE_CODABAR
Note that some barcode standards can only encode numbers, so attempting to print non-numeric codes with them may result in strange behaviour.
Posts I've written up for people who are learning how to use receipt printers:
- What is ESC/POS, and how do I use it?, which documents the output of test.php.
- Setting up an Epson receipt printer
Epson notes that not all of its printers support all ESC/POS features, and includes a table in their documentation:
Note that many printers produced by other vendors use the same standard, and may or may not be compatible.