Cara menggunakan php raw print

Bagi kita developer berbasis web seperti php, ruby, python, dot net, dll terkadang terpikirkan bagaimana melakukan direct printing hanya dengan menekan tombol di browser dan otomatis akan mengeluarkan hasil printer. umumnya yang kita kenal adalah dengan cara menekan shift + P untuk melakukan print dan muncul window dialog preview.

untuk Direct printing yang pernah kami coba adalah dalam kasus develop sebuah aplikasi kasir berbasis web dimana diminta langsung print ke printer POS.
untuk lebih detail bisa menggunakan software QZ Tray di website https://qz.io/d

Demo dapat dicoba langsung di : https://demo.qz.io/

Karena ini memerlukan instalasi QZ Tray, sehingga untuk sementara hanya mendukung OS linux, windows dan MAC, sedangkan Android dan iOS belum didukung.
Mengingat QZ Tray menggunakan teknologi front-end (client side) untuk melakukan printing, maka untuk setiap PC client diharapkan menginstall aplikasi QZ Tray ini.

License pun tidak gratis, untuk trial disediakan tetapi ada batasan waktu.
JIka tertarik untuk license yang lebih panjang jangka waktunya dapat menghubungi kami.

Here's a PHP version of print_r which can be tailored to your needs. Shows protected and private properties of objects and detects recursion (for objects only!). Usage:

void u_print_r ( mixed $expression [, array $ignore] )

Use the $ignore parameter to provide an array of property names that shouldn't be followed recursively.

function u_print_r($subject, $ignore = array(), $depth = 1, $refChain = array())
{
    if ($depth > 20) return;
    if (is_object($subject)) {
        foreach ($refChain as $refVal)
            if ($refVal === $subject) {
                echo "*RECURSION*\n";
                return;
            }
        array_push($refChain, $subject);
        echo get_class($subject) . " Object ( \n";
        $subject = (array) $subject;
        foreach ($subject as $key => $val)
            if (is_array($ignore) && !in_array($key, $ignore, 1)) {
                echo str_repeat(" ", $depth * 4) . '[';
                if ($key{0} == "\0") {
                    $keyParts = explode("\0", $key);
                    echo $keyParts[2] . (($keyParts[1] == '*')  ? ':protected' : ':private');
                } else
                    echo $key;
                echo '] => ';
                u_print_r($val, $ignore, $depth + 1, $refChain);
            }
        echo str_repeat(" ", ($depth - 1) * 4) . ")\n";
        array_pop($refChain);
    } elseif (is_array($subject)) {
        echo "Array ( \n";
        foreach ($subject as $key => $val)
            if (is_array($ignore) && !in_array($key, $ignore, 1)) {
                echo str_repeat(" ", $depth * 4) . '[' . $key . '] => ';
                u_print_r($val, $ignore, $depth + 1, $refChain);
            }
        echo str_repeat(" ", ($depth - 1) * 4) . ")\n";
    } else
        echo $subject . "\n";
}

?>

Example:

class test {

    public $var1 = 'a';
    protected $var2 = 'b';
    private $var3 = 'c';
    protected $array = array('x', 'y', 'z');

}

$test = new test();
$test->recursiveRef = $test;
$test->anotherRecursiveRef->recursiveRef = $test;
$test->dont->follow = 'me';

void u_print_r ( mixed $expression [, array $ignore] )0

?>

void u_print_r ( mixed $expression [, array $ignore] )2

void u_print_r ( mixed $expression [, array $ignore] )3

This project implements a subset of Epson's ESC/POS protocol for thermal receipt printers. It allows you to generate and print receipts with basic formatting, cutting, and barcodes on a compatible printer.

The library was developed to add drop-in support for receipt printing to any PHP app, including web-based point-of-sale (POS) applications.

Compatibility

Interfaces and operating systems

This driver is known to work with the following OS/interface combinations:

 LinuxMacWindowsEthernetYesYesYesUSBYesNot testedYesUSB-serialYesYesYesSerialYesYesYesParallelYesNot testedYesSMB sharedYesNoYesCUPS hostedYesYesNo

Printers

Many thermal receipt printers support ESC/POS to some degree. This driver has been known to work with:

  • 3nStar RPT-008
  • Approx APPPOS80AM
  • AURES ODP-333
  • AURES ODP-500
  • Bematech-4200-TH
  • Bematech LR2000E
  • Birch PRP-085III
  • Bixolon SRP-350III
  • Bixolon SRP-350Plus
  • Black Copper BC-85AC
  • CHD TH-305N
  • Citizen CBM1000-II
  • Citizen CT-S310II
  • Dapper-Geyi Q583P
  • Daruma DR800
  • DR-MP200 (manufacturer unknown)
  • EPOS TEP 220M
  • Elgin i9
  • Epson EU-T332C
  • Epson FX-890 (requires
    <?php
    /* Call this file 'hello-world.php' */
    require __DIR__ . '/vendor/autoload.php';
    use Mike42\Escpos\PrintConnectors\FilePrintConnector;
    use Mike42\Escpos\Printer;
    $connector = new FilePrintConnector("php://stdout");
    $printer = new Printer($connector);
    $printer -> text("Hello World!\n");
    $printer -> cut();
    $printer -> close();
    5 to release paper).
  • Epson TM-T20
  • Epson TM-T20II
  • Epson TM-T70
  • Epson TM-T70II
  • Epson TM-T81
  • Epson TM-T82II
  • Epson TM-T88II
  • Epson TM-T88III
  • Epson TM-T88IV
  • Epson TM-T88V
  • Epson TM-U220
  • Epson TM-U295 (requires
    <?php
    /* Call this file 'hello-world.php' */
    require __DIR__ . '/vendor/autoload.php';
    use Mike42\Escpos\PrintConnectors\FilePrintConnector;
    use Mike42\Escpos\Printer;
    $connector = new FilePrintConnector("php://stdout");
    $printer = new Printer($connector);
    $printer -> text("Hello World!\n");
    $printer -> cut();
    $printer -> close();
    6 to release slip).
  • Epson TM-U590 and TM-U590P
  • Equal (EQ-IT-001) POS-58
  • Everycom EC-58
  • Excelvan HOP-E200
  • Excelvan HOP-E58
  • Excelvan HOP-E801
  • Gainscha GP-2120TF
  • Gainscha GP-5890x (Also marketed as EC Line 5890x)
  • Gainscha GP-U80300I (Also marketed as gprinter GP-U80300I)
  • gprinter GP-U80160I
  • HOIN HOP-H58
  • Ithaca iTherm 28
  • Hasar HTP 250
  • Metapace T-1
  • Metapace T-25
  • Nexa PX700
  • Nyear NP100
  • OKI RT322
  • OKI 80 Plus III
  • Orient BTP-R580
  • P-822D
  • P85A-401 (make unknown)
  • Partner Tech RP320
  • POSLIGNE ODP200H-III-G
  • QPOS Q58M
  • Rongta RP326US
  • Rongta RP58-U
  • Rongta RP80USE
  • SAM4S GIANT-100DB
  • Senor TP-100
  • Sewoo SLK-TS400
  • SEYPOS PRP-96
  • SEYPOS PRP-300 (Also marketed as TYSSO PRP-300)
  • SNBC BTP-R880NPIII
  • Solux SX-TP-88300
  • Sicar POS-80
  • Silicon SP-201 / RP80USE
  • SPRT SP-POS88V
  • Star BSC10
  • Star TSP100 ECO
  • Star TSP100III FuturePRNT
  • Star TSP-650
  • Star TUP-592
  • TVS RP45 Shoppe
  • Venus V248T
  • Xeumior SM-8330
  • Xprinter F-900
  • Xprinter XP-365B
  • Xprinter XP-58 Series
  • Xprinter XP-80C
  • Xprinter XP-90
  • XPrinter XP-Q20011
  • Xprinter XP-Q800
  • Zjiang NT-58H
  • Zjiang ZJ-5870
  • Zjiang ZJ-5890 (Also sold as POS-5890 by many vendors; ZJ-5890K, ZJ-5890T also work).
  • Zjiang ZJ-8220 (Also marketed as Excelvan ZJ-8220)
  • Zjiang ZJ-8250

If you use any other printer with this code, please let us know so that it can be added to the list.

Basic usage

Include the library

Composer

This library is designed for use with the

<?php
/* Call this file 'hello-world.php' */
require __DIR__ . '/vendor/autoload.php';
use Mike42\Escpos\PrintConnectors\FilePrintConnector;
use Mike42\Escpos\Printer;
$connector = new FilePrintConnector("php://stdout");
$printer = new Printer($connector);
$printer -> text("Hello World!\n");
$printer -> cut();
$printer -> close();
7 PHP dependency manager. Simply add the
<?php
/* Call this file 'hello-world.php' */
require __DIR__ . '/vendor/autoload.php';
use Mike42\Escpos\PrintConnectors\FilePrintConnector;
use Mike42\Escpos\Printer;
$connector = new FilePrintConnector("php://stdout");
$printer = new Printer($connector);
$printer -> text("Hello World!\n");
$printer -> cut();
$printer -> close();
8 package to get started:

composer require mike42/escpos-php

If you haven't used

<?php
/* Call this file 'hello-world.php' */
require __DIR__ . '/vendor/autoload.php';
use Mike42\Escpos\PrintConnectors\FilePrintConnector;
use Mike42\Escpos\Printer;
$connector = new FilePrintConnector("php://stdout");
$printer = new Printer($connector);
$printer -> text("Hello World!\n");
$printer -> cut();
$printer -> close();
7 before, you can read about it at getcomposer.org.

Requirements

This project has few hard dependencies:

  • PHP 7.3 or newer.
  • php hello-world.php | nc 10.x.x.x. 9100
    0 extension, used to load bundled printer definitions (see documentation)
  • php hello-world.php | nc 10.x.x.x. 9100
    1 extension, used for character encoding (see documentation)
  • php hello-world.php | nc 10.x.x.x. 9100
    2 extension, used for de-compressing bundled resources (see documentation).

It is also suggested that you install either

php hello-world.php | nc 10.x.x.x. 9100
3 or
php hello-world.php | nc 10.x.x.x. 9100
4, as these can be used to speed up image processing.

A number of optional extensions can be added to enable more specific features. These are described in the "suggest" section of composer.json.

The 'Hello World' receipt

To make use of this driver, your server (where PHP is installed) must be able to communicate with your printer. Start by generating a simple receipt and sending it to your printer using the command-line.

<?php
/* Call this file 'hello-world.php' */
require __DIR__ . '/vendor/autoload.php';
use Mike42\Escpos\PrintConnectors\FilePrintConnector;
use Mike42\Escpos\Printer;
$connector = new FilePrintConnector("php://stdout");
$printer = new Printer($connector);
$printer -> text("Hello World!\n");
$printer -> cut();
$printer -> close();

Some examples are below for common interfaces.

Communicate with a printer with an Ethernet interface using

php hello-world.php | nc 10.x.x.x. 9100
5:

php hello-world.php | nc 10.x.x.x. 9100

A USB local printer connected with

php hello-world.php | nc 10.x.x.x. 9100
6 on Linux has a device file (Includes USB-parallel interfaces):

php hello-world.php > /dev/usb/lp0

A computer installed into the local

php hello-world.php | nc 10.x.x.x. 9100
7 server is accessed through
php hello-world.php | nc 10.x.x.x. 9100
8 or
php hello-world.php | nc 10.x.x.x. 9100
9:

php hello-world.php > foo.txt
lpr -o raw -H localhost -P printer foo.txt

A local or networked printer on a Windows computer is mapped in to a file, and generally requires you to share the printer first:

php hello-world.php > foo.txt
net use LPT1 \\server\printer
copy foo.txt LPT1
del foo.txt

If you have troubles at this point, then you should consult your OS and printer system documentation to try to find a working print command.

Using a PrintConnector

To print receipts from PHP, use the most applicable PrintConnector for your setup. The connector simply provides the plumbing to get data to the printer.

For example, a

php hello-world.php > /dev/usb/lp0
0 accepts an IP address and port:

use Mike42\Escpos\PrintConnectors\NetworkPrintConnector;
use Mike42\Escpos\Printer;
$connector = new NetworkPrintConnector("10.x.x.x", 9100);
$printer = new Printer($connector);
try {
    // ... Print stuff
} finally {
    $printer -> close();
}

While a serial printer might use:

use Mike42\Escpos\PrintConnectors\FilePrintConnector;
use Mike42\Escpos\Printer;
$connector = new FilePrintConnector("/dev/ttyS0");
$printer = new Printer($connector);

For each OS/interface combination that's supported, there are examples in the compatibility section of how a

php hello-world.php > /dev/usb/lp0
1 would be constructed. If you can't get a
php hello-world.php > /dev/usb/lp0
1 to work, then be sure to include the working print command in your issue.

Using a CapabilityProfile

Support for commands and code pages varies between printer vendors and models. By default, the driver will accept UTF-8, and output commands that are suitable for Epson TM-series printers.

When trying out a new brand of printer, it's a good idea to use the "simple"

php hello-world.php > /dev/usb/lp0
3, which instructs the driver to avoid the use of advanced features (generally simpler image handling, ASCII-only text).

use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
use Mike42\Escpos\CapabilityProfile;
$profile = CapabilityProfile::load("simple");
$connector = new WindowsPrintConnector("smb://computer/printer");
$printer = new Printer($connector, $profile);

As another example, Star-branded printers use different commands:

use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
use Mike42\Escpos\CapabilityProfile;
$profile = CapabilityProfile::load("SP2000")
$connector = new WindowsPrintConnector("smb://computer/printer");
$printer = new Printer($connector, $profile);

For a list of available profiles, or to have support for your printer improved, please see the upstream receipt-print-hq/escpos-printer-db project.

Tips & examples

On Linux, your printer device file will be somewhere like

php hello-world.php > /dev/usb/lp0
4 (parallel),
php hello-world.php > /dev/usb/lp0
5 (USB),
php hello-world.php > /dev/usb/lp0
6 (USB-Serial),
php hello-world.php > /dev/usb/lp0
7 (serial).

On Windows, the device files will be along the lines of

php hello-world.php > /dev/usb/lp0
8 (parallel) or
php hello-world.php > /dev/usb/lp0
9 (serial). Use the
php hello-world.php > foo.txt
lpr -o raw -H localhost -P printer foo.txt
0 to tap into system printing on Windows (eg. Windows USB, SMB or Windows LPT) - this submits print jobs via a queue rather than communicating directly with the printer.

A complete real-world receipt can be found in the code of Auth in ReceiptPrinter.php. It includes justification, boldness, and a barcode.

Other examples are located in the example/ directory.

Available methods

__construct(PrintConnector $connector, CapabilityProfile $profile)

Construct new print object.

Parameters:

  • php hello-world.php > foo.txt
    lpr -o raw -H localhost -P printer foo.txt
    1: The PrintConnector to send data to.
  • php hello-world.php > foo.txt
    lpr -o raw -H localhost -P printer foo.txt
    2 Supported features of this printer. If not set, the "default" CapabilityProfile will be used, which is suitable for Epson printers.

See example/interface/ for ways to open connections for different platforms and interfaces.

barcode($content, $type)

Print a barcode.

Parameters:

  • php hello-world.php > foo.txt
    lpr -o raw -H localhost -P printer foo.txt
    3: The information to encode.
  • php hello-world.php > foo.txt
    lpr -o raw -H localhost -P printer foo.txt
    4: The barcode standard to output. If not specified,
    php hello-world.php > foo.txt
    lpr -o raw -H localhost -P printer foo.txt
    5 will be used.

Currently supported barcode standards are (depending on your printer):

  • php hello-world.php > foo.txt
    lpr -o raw -H localhost -P printer foo.txt
    6
  • php hello-world.php > foo.txt
    lpr -o raw -H localhost -P printer foo.txt
    7
  • php hello-world.php > foo.txt
    lpr -o raw -H localhost -P printer foo.txt
    8
  • php hello-world.php > foo.txt
    lpr -o raw -H localhost -P printer foo.txt
    9
  • php hello-world.php > foo.txt
    net use LPT1 \\server\printer
    copy foo.txt LPT1
    del foo.txt
    
    0
  • php hello-world.php > foo.txt
    net use LPT1 \\server\printer
    copy foo.txt LPT1
    del foo.txt
    
    1
  • php hello-world.php > foo.txt
    net use LPT1 \\server\printer
    copy foo.txt LPT1
    del foo.txt
    
    2

Note that some barcode standards can only encode numbers, so attempting to print non-numeric codes with them may result in strange behaviour.

bitImage(EscposImage $image, $size)

See below.

cut($mode, $lines)

Cut the paper.

Parameters:

  • php hello-world.php > foo.txt
    net use LPT1 \\server\printer
    copy foo.txt LPT1
    del foo.txt
    
    3: Cut mode, either
    php hello-world.php > foo.txt
    net use LPT1 \\server\printer
    copy foo.txt LPT1
    del foo.txt
    
    4 or
    php hello-world.php > foo.txt
    net use LPT1 \\server\printer
    copy foo.txt LPT1
    del foo.txt
    
    5. If not specified,
    php hello-world.php > foo.txt
    net use LPT1 \\server\printer
    copy foo.txt LPT1
    del foo.txt
    
    4 will be used.
  • php hello-world.php > foo.txt
    net use LPT1 \\server\printer
    copy foo.txt LPT1
    del foo.txt
    
    7: Number of lines to feed before cutting. If not specified, 3 will be used.

feed($lines)

Print and feed line / Print and feed n lines.

Parameters:

  • php hello-world.php > foo.txt
    net use LPT1 \\server\printer
    copy foo.txt LPT1
    del foo.txt
    
    7: Number of lines to feed

feedForm()

Some printers require a form feed to release the paper. On most printers, this command is only useful in page mode, which is not implemented in this driver.

feedReverse($lines)

Print and reverse feed n lines.

Parameters:

  • php hello-world.php > foo.txt
    net use LPT1 \\server\printer
    copy foo.txt LPT1
    del foo.txt
    
    7: number of lines to feed. If not specified, 1 line will be fed.

graphics(EscposImage $image, $size)

Print an image to the printer.

Parameters:

  • use Mike42\Escpos\PrintConnectors\NetworkPrintConnector;
    use Mike42\Escpos\Printer;
    $connector = new NetworkPrintConnector("10.x.x.x", 9100);
    $printer = new Printer($connector);
    try {
        // ... Print stuff
    } finally {
        $printer -> close();
    }
    0: The image to print.
  • use Mike42\Escpos\PrintConnectors\NetworkPrintConnector;
    use Mike42\Escpos\Printer;
    $connector = new NetworkPrintConnector("10.x.x.x", 9100);
    $printer = new Printer($connector);
    try {
        // ... Print stuff
    } finally {
        $printer -> close();
    }
    1: Output size modifier for the image.

Size modifiers are:

  • use Mike42\Escpos\PrintConnectors\NetworkPrintConnector;
    use Mike42\Escpos\Printer;
    $connector = new NetworkPrintConnector("10.x.x.x", 9100);
    $printer = new Printer($connector);
    try {
        // ... Print stuff
    } finally {
        $printer -> close();
    }
    2 (leave image at original size)
  • use Mike42\Escpos\PrintConnectors\NetworkPrintConnector;
    use Mike42\Escpos\Printer;
    $connector = new NetworkPrintConnector("10.x.x.x", 9100);
    $printer = new Printer($connector);
    try {
        // ... Print stuff
    } finally {
        $printer -> close();
    }
    3
  • use Mike42\Escpos\PrintConnectors\NetworkPrintConnector;
    use Mike42\Escpos\Printer;
    $connector = new NetworkPrintConnector("10.x.x.x", 9100);
    $printer = new Printer($connector);
    try {
        // ... Print stuff
    } finally {
        $printer -> close();
    }
    4

A minimal example:

<?php
/* Call this file 'hello-world.php' */
require __DIR__ . '/vendor/autoload.php';
use Mike42\Escpos\PrintConnectors\FilePrintConnector;
use Mike42\Escpos\Printer;
$connector = new FilePrintConnector("php://stdout");
$printer = new Printer($connector);
$printer -> text("Hello World!\n");
$printer -> cut();
$printer -> close();
0

See the example/ folder for detailed examples.

The function takes the same parameters, and can be used if your printer doesn't support the newer graphics commands. As an additional fallback, the

use Mike42\Escpos\PrintConnectors\NetworkPrintConnector;
use Mike42\Escpos\Printer;
$connector = new NetworkPrintConnector("10.x.x.x", 9100);
$printer = new Printer($connector);
try {
    // ... Print stuff
} finally {
    $printer -> close();
}
5 function is also provided.

initialize()

Initialize printer. This resets formatting back to the defaults.

pdf417Code($content, $width, $heightMultiplier, $dataColumnCount, $ec, $options)

Print a two-dimensional data code using the PDF417 standard.

Parameters:

  • php hello-world.php > foo.txt
    lpr -o raw -H localhost -P printer foo.txt
    3: Text or numbers to store in the code
  • use Mike42\Escpos\PrintConnectors\NetworkPrintConnector;
    use Mike42\Escpos\Printer;
    $connector = new NetworkPrintConnector("10.x.x.x", 9100);
    $printer = new Printer($connector);
    try {
        // ... Print stuff
    } finally {
        $printer -> close();
    }
    7: Width of a module (pixel) in the printed code. Default is 3 dots.
  • use Mike42\Escpos\PrintConnectors\NetworkPrintConnector;
    use Mike42\Escpos\Printer;
    $connector = new NetworkPrintConnector("10.x.x.x", 9100);
    $printer = new Printer($connector);
    try {
        // ... Print stuff
    } finally {
        $printer -> close();
    }
    8: Multiplier for height of a module. Default is 3 times the width.
  • use Mike42\Escpos\PrintConnectors\NetworkPrintConnector;
    use Mike42\Escpos\Printer;
    $connector = new NetworkPrintConnector("10.x.x.x", 9100);
    $printer = new Printer($connector);
    try {
        // ... Print stuff
    } finally {
        $printer -> close();
    }
    9: Number of data columns to use. 0 (default) is to auto-calculate. Smaller numbers will result in a narrower code, making larger pixel sizes possible. Larger numbers require smaller pixel sizes.
  • use Mike42\Escpos\PrintConnectors\FilePrintConnector;
    use Mike42\Escpos\Printer;
    $connector = new FilePrintConnector("/dev/ttyS0");
    $printer = new Printer($connector);
    0: Error correction ratio, from 0.01 to 4.00. Default is 0.10 (10%).
  • use Mike42\Escpos\PrintConnectors\FilePrintConnector;
    use Mike42\Escpos\Printer;
    $connector = new FilePrintConnector("/dev/ttyS0");
    $printer = new Printer($connector);
    1: Standard code
    use Mike42\Escpos\PrintConnectors\FilePrintConnector;
    use Mike42\Escpos\Printer;
    $connector = new FilePrintConnector("/dev/ttyS0");
    $printer = new Printer($connector);
    2 with start/end bars, or truncated code
    use Mike42\Escpos\PrintConnectors\FilePrintConnector;
    use Mike42\Escpos\Printer;
    $connector = new FilePrintConnector("/dev/ttyS0");
    $printer = new Printer($connector);
    3 with start bars only.

pulse($pin, $on_ms, $off_ms)

Generate a pulse, for opening a cash drawer if one is connected. The default settings (0, 120, 240) should open an Epson drawer.

Parameters:

  • use Mike42\Escpos\PrintConnectors\FilePrintConnector;
    use Mike42\Escpos\Printer;
    $connector = new FilePrintConnector("/dev/ttyS0");
    $printer = new Printer($connector);
    4: 0 or 1, for pin 2 or pin 5 kick-out connector respectively.
  • use Mike42\Escpos\PrintConnectors\FilePrintConnector;
    use Mike42\Escpos\Printer;
    $connector = new FilePrintConnector("/dev/ttyS0");
    $printer = new Printer($connector);
    5: pulse ON time, in milliseconds.
  • use Mike42\Escpos\PrintConnectors\FilePrintConnector;
    use Mike42\Escpos\Printer;
    $connector = new FilePrintConnector("/dev/ttyS0");
    $printer = new Printer($connector);
    6: pulse OFF time, in milliseconds.

qrCode($content, $ec, $size, $model)

Print the given data as a QR code on the printer.

  • php hello-world.php > foo.txt
    lpr -o raw -H localhost -P printer foo.txt
    3: The content of the code. Numeric data will be more efficiently compacted.
  • use Mike42\Escpos\PrintConnectors\FilePrintConnector;
    use Mike42\Escpos\Printer;
    $connector = new FilePrintConnector("/dev/ttyS0");
    $printer = new Printer($connector);
    8 Error-correction level to use. One of
    use Mike42\Escpos\PrintConnectors\FilePrintConnector;
    use Mike42\Escpos\Printer;
    $connector = new FilePrintConnector("/dev/ttyS0");
    $printer = new Printer($connector);
    9 (default),
    use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
    use Mike42\Escpos\CapabilityProfile;
    $profile = CapabilityProfile::load("simple");
    $connector = new WindowsPrintConnector("smb://computer/printer");
    $printer = new Printer($connector, $profile);
    0,
    use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
    use Mike42\Escpos\CapabilityProfile;
    $profile = CapabilityProfile::load("simple");
    $connector = new WindowsPrintConnector("smb://computer/printer");
    $printer = new Printer($connector, $profile);
    1 or
    use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
    use Mike42\Escpos\CapabilityProfile;
    $profile = CapabilityProfile::load("simple");
    $connector = new WindowsPrintConnector("smb://computer/printer");
    $printer = new Printer($connector, $profile);
    2. Higher error correction results in a less compact code.
  • use Mike42\Escpos\PrintConnectors\NetworkPrintConnector;
    use Mike42\Escpos\Printer;
    $connector = new NetworkPrintConnector("10.x.x.x", 9100);
    $printer = new Printer($connector);
    try {
        // ... Print stuff
    } finally {
        $printer -> close();
    }
    1: Pixel size to use. Must be 1-16 (default 3)
  • use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
    use Mike42\Escpos\CapabilityProfile;
    $profile = CapabilityProfile::load("simple");
    $connector = new WindowsPrintConnector("smb://computer/printer");
    $printer = new Printer($connector, $profile);
    4: QR code model to use. Must be one of
    use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
    use Mike42\Escpos\CapabilityProfile;
    $profile = CapabilityProfile::load("simple");
    $connector = new WindowsPrintConnector("smb://computer/printer");
    $printer = new Printer($connector, $profile);
    5,
    use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
    use Mike42\Escpos\CapabilityProfile;
    $profile = CapabilityProfile::load("simple");
    $connector = new WindowsPrintConnector("smb://computer/printer");
    $printer = new Printer($connector, $profile);
    6 (default) or
    use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
    use Mike42\Escpos\CapabilityProfile;
    $profile = CapabilityProfile::load("simple");
    $connector = new WindowsPrintConnector("smb://computer/printer");
    $printer = new Printer($connector, $profile);
    7 (not supported by all printers).

selectPrintMode($mode)

Select print mode(s).

Parameters:

  • php hello-world.php > foo.txt
    net use LPT1 \\server\printer
    copy foo.txt LPT1
    del foo.txt
    
    3: The mode to use. Default is
    use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
    use Mike42\Escpos\CapabilityProfile;
    $profile = CapabilityProfile::load("simple");
    $connector = new WindowsPrintConnector("smb://computer/printer");
    $printer = new Printer($connector, $profile);
    9, with no special formatting. This has a similar effect to running
    use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
    use Mike42\Escpos\CapabilityProfile;
    $profile = CapabilityProfile::load("SP2000")
    $connector = new WindowsPrintConnector("smb://computer/printer");
    $printer = new Printer($connector, $profile);
    0.

Several MODE_* constants can be OR'd together passed to this function's

use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
use Mike42\Escpos\CapabilityProfile;
$profile = CapabilityProfile::load("SP2000")
$connector = new WindowsPrintConnector("smb://computer/printer");
$printer = new Printer($connector, $profile);
1 argument. The valid modes are:

  • use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
    use Mike42\Escpos\CapabilityProfile;
    $profile = CapabilityProfile::load("SP2000")
    $connector = new WindowsPrintConnector("smb://computer/printer");
    $printer = new Printer($connector, $profile);
    2
  • use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
    use Mike42\Escpos\CapabilityProfile;
    $profile = CapabilityProfile::load("SP2000")
    $connector = new WindowsPrintConnector("smb://computer/printer");
    $printer = new Printer($connector, $profile);
    3
  • use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
    use Mike42\Escpos\CapabilityProfile;
    $profile = CapabilityProfile::load("SP2000")
    $connector = new WindowsPrintConnector("smb://computer/printer");
    $printer = new Printer($connector, $profile);
    4
  • use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
    use Mike42\Escpos\CapabilityProfile;
    $profile = CapabilityProfile::load("SP2000")
    $connector = new WindowsPrintConnector("smb://computer/printer");
    $printer = new Printer($connector, $profile);
    5
  • use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
    use Mike42\Escpos\CapabilityProfile;
    $profile = CapabilityProfile::load("SP2000")
    $connector = new WindowsPrintConnector("smb://computer/printer");
    $printer = new Printer($connector, $profile);
    6
  • use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
    use Mike42\Escpos\CapabilityProfile;
    $profile = CapabilityProfile::load("SP2000")
    $connector = new WindowsPrintConnector("smb://computer/printer");
    $printer = new Printer($connector, $profile);
    7

setBarcodeHeight($height)

Set barcode height.

Parameters:

  • use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
    use Mike42\Escpos\CapabilityProfile;
    $profile = CapabilityProfile::load("SP2000")
    $connector = new WindowsPrintConnector("smb://computer/printer");
    $printer = new Printer($connector, $profile);
    8: Height in dots. If not specified, 8 will be used.

setBarcodeWidth($width)

Set barcode bar width.

Parameters:

  • use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
    use Mike42\Escpos\CapabilityProfile;
    $profile = CapabilityProfile::load("SP2000")
    $connector = new WindowsPrintConnector("smb://computer/printer");
    $printer = new Printer($connector, $profile);
    9: Bar width in dots. If not specified, 3 will be used. Values above 6 appear to have no effect.

setColor($color)

Select print color - on printers that support multiple colors.

Parameters:

  • <?php
    /* Call this file 'hello-world.php' */
    require __DIR__ . '/vendor/autoload.php';
    use Mike42\Escpos\PrintConnectors\FilePrintConnector;
    use Mike42\Escpos\Printer;
    $connector = new FilePrintConnector("php://stdout");
    $printer = new Printer($connector);
    $printer -> text("Hello World!\n");
    $printer -> cut();
    $printer -> close();
    00: Color to use. Must be either
    <?php
    /* Call this file 'hello-world.php' */
    require __DIR__ . '/vendor/autoload.php';
    use Mike42\Escpos\PrintConnectors\FilePrintConnector;
    use Mike42\Escpos\Printer;
    $connector = new FilePrintConnector("php://stdout");
    $printer = new Printer($connector);
    $printer -> text("Hello World!\n");
    $printer -> cut();
    $printer -> close();
    01 (default), or
    <?php
    /* Call this file 'hello-world.php' */
    require __DIR__ . '/vendor/autoload.php';
    use Mike42\Escpos\PrintConnectors\FilePrintConnector;
    use Mike42\Escpos\Printer;
    $connector = new FilePrintConnector("php://stdout");
    $printer = new Printer($connector);
    $printer -> text("Hello World!\n");
    $printer -> cut();
    $printer -> close();
    02

setDoubleStrike($on)

Turn double-strike mode on/off.

Parameters:

  • <?php
    /* Call this file 'hello-world.php' */
    require __DIR__ . '/vendor/autoload.php';
    use Mike42\Escpos\PrintConnectors\FilePrintConnector;
    use Mike42\Escpos\Printer;
    $connector = new FilePrintConnector("php://stdout");
    $printer = new Printer($connector);
    $printer -> text("Hello World!\n");
    $printer -> cut();
    $printer -> close();
    03: true for double strike, false for no double strike.

setEmphasis($on)

Turn emphasized mode on/off.

Parameters:

  • <?php
    /* Call this file 'hello-world.php' */
    require __DIR__ . '/vendor/autoload.php';
    use Mike42\Escpos\PrintConnectors\FilePrintConnector;
    use Mike42\Escpos\Printer;
    $connector = new FilePrintConnector("php://stdout");
    $printer = new Printer($connector);
    $printer -> text("Hello World!\n");
    $printer -> cut();
    $printer -> close();
    03: true for emphasis, false for no emphasis.

setFont($font)

Select font. Most printers have two fonts (Fonts A and B), and some have a third (Font C).

Parameters:

  • <?php
    /* Call this file 'hello-world.php' */
    require __DIR__ . '/vendor/autoload.php';
    use Mike42\Escpos\PrintConnectors\FilePrintConnector;
    use Mike42\Escpos\Printer;
    $connector = new FilePrintConnector("php://stdout");
    $printer = new Printer($connector);
    $printer -> text("Hello World!\n");
    $printer -> cut();
    $printer -> close();
    05: The font to use. Must be either
    <?php
    /* Call this file 'hello-world.php' */
    require __DIR__ . '/vendor/autoload.php';
    use Mike42\Escpos\PrintConnectors\FilePrintConnector;
    use Mike42\Escpos\Printer;
    $connector = new FilePrintConnector("php://stdout");
    $printer = new Printer($connector);
    $printer -> text("Hello World!\n");
    $printer -> cut();
    $printer -> close();
    06,
    <?php
    /* Call this file 'hello-world.php' */
    require __DIR__ . '/vendor/autoload.php';
    use Mike42\Escpos\PrintConnectors\FilePrintConnector;
    use Mike42\Escpos\Printer;
    $connector = new FilePrintConnector("php://stdout");
    $printer = new Printer($connector);
    $printer -> text("Hello World!\n");
    $printer -> cut();
    $printer -> close();
    07, or
    <?php
    /* Call this file 'hello-world.php' */
    require __DIR__ . '/vendor/autoload.php';
    use Mike42\Escpos\PrintConnectors\FilePrintConnector;
    use Mike42\Escpos\Printer;
    $connector = new FilePrintConnector("php://stdout");
    $printer = new Printer($connector);
    $printer -> text("Hello World!\n");
    $printer -> cut();
    $printer -> close();
    08.

setJustification($justification)

Select justification.

Parameters:

  • <?php
    /* Call this file 'hello-world.php' */
    require __DIR__ . '/vendor/autoload.php';
    use Mike42\Escpos\PrintConnectors\FilePrintConnector;
    use Mike42\Escpos\Printer;
    $connector = new FilePrintConnector("php://stdout");
    $printer = new Printer($connector);
    $printer -> text("Hello World!\n");
    $printer -> cut();
    $printer -> close();
    09: One of
    <?php
    /* Call this file 'hello-world.php' */
    require __DIR__ . '/vendor/autoload.php';
    use Mike42\Escpos\PrintConnectors\FilePrintConnector;
    use Mike42\Escpos\Printer;
    $connector = new FilePrintConnector("php://stdout");
    $printer = new Printer($connector);
    $printer -> text("Hello World!\n");
    $printer -> cut();
    $printer -> close();
    10,
    <?php
    /* Call this file 'hello-world.php' */
    require __DIR__ . '/vendor/autoload.php';
    use Mike42\Escpos\PrintConnectors\FilePrintConnector;
    use Mike42\Escpos\Printer;
    $connector = new FilePrintConnector("php://stdout");
    $printer = new Printer($connector);
    $printer -> text("Hello World!\n");
    $printer -> cut();
    $printer -> close();
    11, or
    <?php
    /* Call this file 'hello-world.php' */
    require __DIR__ . '/vendor/autoload.php';
    use Mike42\Escpos\PrintConnectors\FilePrintConnector;
    use Mike42\Escpos\Printer;
    $connector = new FilePrintConnector("php://stdout");
    $printer = new Printer($connector);
    $printer -> text("Hello World!\n");
    $printer -> cut();
    $printer -> close();
    12.

setLineSpacing($height)

Set the height of the line.

Some printers will allow you to overlap lines with a smaller line feed.

Parameters:

  • <?php
    /* Call this file 'hello-world.php' */
    require __DIR__ . '/vendor/autoload.php';
    use Mike42\Escpos\PrintConnectors\FilePrintConnector;
    use Mike42\Escpos\Printer;
    $connector = new FilePrintConnector("php://stdout");
    $printer = new Printer($connector);
    $printer -> text("Hello World!\n");
    $printer -> cut();
    $printer -> close();
    13: The height of each line, in dots. If not set, the printer will reset to its default line spacing.

setPrintLeftMargin($margin)

Set print area left margin. Reset to default with

<?php
/* Call this file 'hello-world.php' */
require __DIR__ . '/vendor/autoload.php';
use Mike42\Escpos\PrintConnectors\FilePrintConnector;
use Mike42\Escpos\Printer;
$connector = new FilePrintConnector("php://stdout");
$printer = new Printer($connector);
$printer -> text("Hello World!\n");
$printer -> cut();
$printer -> close();
14.

Parameters:

  • <?php
    /* Call this file 'hello-world.php' */
    require __DIR__ . '/vendor/autoload.php';
    use Mike42\Escpos\PrintConnectors\FilePrintConnector;
    use Mike42\Escpos\Printer;
    $connector = new FilePrintConnector("php://stdout");
    $printer = new Printer($connector);
    $printer -> text("Hello World!\n");
    $printer -> cut();
    $printer -> close();
    15: The left margin to set on to the print area, in dots.

setPrintWidth($width)

Set print area width. This can be used to add a right margin to the print area. Reset to default with

<?php
/* Call this file 'hello-world.php' */
require __DIR__ . '/vendor/autoload.php';
use Mike42\Escpos\PrintConnectors\FilePrintConnector;
use Mike42\Escpos\Printer;
$connector = new FilePrintConnector("php://stdout");
$printer = new Printer($connector);
$printer -> text("Hello World!\n");
$printer -> cut();
$printer -> close();
14.

Parameters:

  • use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
    use Mike42\Escpos\CapabilityProfile;
    $profile = CapabilityProfile::load("SP2000")
    $connector = new WindowsPrintConnector("smb://computer/printer");
    $printer = new Printer($connector, $profile);
    9: The width of the page print area, in dots.

setReverseColors($on)

Set black/white reverse mode on or off. In this mode, text is printed white on a black background.

Parameters:

  • <?php
    /* Call this file 'hello-world.php' */
    require __DIR__ . '/vendor/autoload.php';
    use Mike42\Escpos\PrintConnectors\FilePrintConnector;
    use Mike42\Escpos\Printer;
    $connector = new FilePrintConnector("php://stdout");
    $printer = new Printer($connector);
    $printer -> text("Hello World!\n");
    $printer -> cut();
    $printer -> close();
    03: True to enable, false to disable.

setTextSize($widthMultiplier, $heightMultiplier)

Set the size of text, as a multiple of the normal size.

Parameters:

  • <?php
    /* Call this file 'hello-world.php' */
    require __DIR__ . '/vendor/autoload.php';
    use Mike42\Escpos\PrintConnectors\FilePrintConnector;
    use Mike42\Escpos\Printer;
    $connector = new FilePrintConnector("php://stdout");
    $printer = new Printer($connector);
    $printer -> text("Hello World!\n");
    $printer -> cut();
    $printer -> close();
    19: Multiple of the regular height to use (range 1 - 8).
  • <?php
    /* Call this file 'hello-world.php' */
    require __DIR__ . '/vendor/autoload.php';
    use Mike42\Escpos\PrintConnectors\FilePrintConnector;
    use Mike42\Escpos\Printer;
    $connector = new FilePrintConnector("php://stdout");
    $printer = new Printer($connector);
    $printer -> text("Hello World!\n");
    $printer -> cut();
    $printer -> close();
    20: Multiple of the regular height to use (range 1 - 8).

setUnderline($underline)

Set underline for printed text.

Parameters:

  • <?php
    /* Call this file 'hello-world.php' */
    require __DIR__ . '/vendor/autoload.php';
    use Mike42\Escpos\PrintConnectors\FilePrintConnector;
    use Mike42\Escpos\Printer;
    $connector = new FilePrintConnector("php://stdout");
    $printer = new Printer($connector);
    $printer -> text("Hello World!\n");
    $printer -> cut();
    $printer -> close();
    21: Either
    <?php
    /* Call this file 'hello-world.php' */
    require __DIR__ . '/vendor/autoload.php';
    use Mike42\Escpos\PrintConnectors\FilePrintConnector;
    use Mike42\Escpos\Printer;
    $connector = new FilePrintConnector("php://stdout");
    $printer = new Printer($connector);
    $printer -> text("Hello World!\n");
    $printer -> cut();
    $printer -> close();
    22/
    <?php
    /* Call this file 'hello-world.php' */
    require __DIR__ . '/vendor/autoload.php';
    use Mike42\Escpos\PrintConnectors\FilePrintConnector;
    use Mike42\Escpos\Printer;
    $connector = new FilePrintConnector("php://stdout");
    $printer = new Printer($connector);
    $printer -> text("Hello World!\n");
    $printer -> cut();
    $printer -> close();
    23, or one of
    <?php
    /* Call this file 'hello-world.php' */
    require __DIR__ . '/vendor/autoload.php';
    use Mike42\Escpos\PrintConnectors\FilePrintConnector;
    use Mike42\Escpos\Printer;
    $connector = new FilePrintConnector("php://stdout");
    $printer = new Printer($connector);
    $printer -> text("Hello World!\n");
    $printer -> cut();
    $printer -> close();
    24,
    <?php
    /* Call this file 'hello-world.php' */
    require __DIR__ . '/vendor/autoload.php';
    use Mike42\Escpos\PrintConnectors\FilePrintConnector;
    use Mike42\Escpos\Printer;
    $connector = new FilePrintConnector("php://stdout");
    $printer = new Printer($connector);
    $printer -> text("Hello World!\n");
    $printer -> cut();
    $printer -> close();
    25 or
    <?php
    /* Call this file 'hello-world.php' */
    require __DIR__ . '/vendor/autoload.php';
    use Mike42\Escpos\PrintConnectors\FilePrintConnector;
    use Mike42\Escpos\Printer;
    $connector = new FilePrintConnector("php://stdout");
    $printer = new Printer($connector);
    $printer -> text("Hello World!\n");
    $printer -> cut();
    $printer -> close();
    26. Defaults to
    <?php
    /* Call this file 'hello-world.php' */
    require __DIR__ . '/vendor/autoload.php';
    use Mike42\Escpos\PrintConnectors\FilePrintConnector;
    use Mike42\Escpos\Printer;
    $connector = new FilePrintConnector("php://stdout");
    $printer = new Printer($connector);
    $printer -> text("Hello World!\n");
    $printer -> cut();
    $printer -> close();
    25.

text($str)

Add text to the buffer. Text should either be followed by a line-break, or

<?php
/* Call this file 'hello-world.php' */
require __DIR__ . '/vendor/autoload.php';
use Mike42\Escpos\PrintConnectors\FilePrintConnector;
use Mike42\Escpos\Printer;
$connector = new FilePrintConnector("php://stdout");
$printer = new Printer($connector);
$printer -> text("Hello World!\n");
$printer -> cut();
$printer -> close();
28 should be called after this.

Parameters:

  • <?php
    /* Call this file 'hello-world.php' */
    require __DIR__ . '/vendor/autoload.php';
    use Mike42\Escpos\PrintConnectors\FilePrintConnector;
    use Mike42\Escpos\Printer;
    $connector = new FilePrintConnector("php://stdout");
    $printer = new Printer($connector);
    $printer -> text("Hello World!\n");
    $printer -> cut();
    $printer -> close();
    29: The string to print.
Further notes

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
    <?php
    /* Call this file 'hello-world.php' */
    require __DIR__ . '/vendor/autoload.php';
    use Mike42\Escpos\PrintConnectors\FilePrintConnector;
    use Mike42\Escpos\Printer;
    $connector = new FilePrintConnector("php://stdout");
    $printer = new Printer($connector);
    $printer -> text("Hello World!\n");
    $printer -> cut();
    $printer -> close();
    30.
  • Setting up an Epson receipt printer
  • Getting a USB receipt printer working on Linux
Development

This code is MIT licensed, and you are encouraged to contribute any modifications back to the project.

For development, it's suggested that you load

php hello-world.php | nc 10.x.x.x. 9100
3,
php hello-world.php | nc 10.x.x.x. 9100
4 and
<?php
/* Call this file 'hello-world.php' */
require __DIR__ . '/vendor/autoload.php';
use Mike42\Escpos\PrintConnectors\FilePrintConnector;
use Mike42\Escpos\Printer;
$connector = new FilePrintConnector("php://stdout");
$printer = new Printer($connector);
$printer -> text("Hello World!\n");
$printer -> cut();
$printer -> close();
33 PHP extensions.

The tests are executed on Travis CI over PHP 7.3, 7.4 and 8.0. Older versions of PHP are not supported in the current release, nor is HHVM.