mike42/escpos-php
PHP library for ESC/POS receipt printers. Print text, images, barcodes, QR codes and cut paper over USB, network, serial or Windows share. Includes connectors and utilities for common thermal POS printers and cash drawers.
Install via Composer:
composer require mike42/escpos-php
Basic Usage Example (Network Printer):
use Mike42\Escpos\PrintConnectors\NetworkPrintConnector;
use Mike42\Escpos\Printer;
$connector = new NetworkPrintConnector("192.168.1.100", 9100);
$printer = new Printer($connector);
$printer->text("Hello World!\n");
$printer->cut();
$printer->close();
First Use Case:
src/Mike42/Escpos/ (Core classes: Printer, PrintConnectors, Graphics)tests/ (Real-world examples for barcodes, QR codes, and formatting)Receipt Generation Pipeline:
$printer = new Printer($connector);
$printer->selectPrintMode(Printer::MODE_DOUBLE_HEIGHT);
$printer->setJustification(Printer::JUSTIFY_CENTER);
$printer->text("ORDER #12345\n");
$printer->text("Item 1: $10.00\n");
$printer->text("Item 2: $5.00\n");
$printer->text("Total: $15.00\n", 1, Printer::FONT_B);
$printer->cut(Printer::CUT_PARTIAL);
$printer->close();
Dynamic Data Binding (Laravel Blade Example):
// In a controller:
$order = Order::find($id);
$printer = new Printer($connector);
$printer->text("Order #{$order->id}\n");
$printer->text("Customer: {$order->customer_name}\n");
$printer->qr("https://example.com/order/{$order->id}", 50);
Connection Management:
SerialPrintConnector (requires php-serial extension).
$connector = new SerialPrintConnector("/dev/usb/lp0");
9100 is standard).$connector = new FilePrintConnector("output.escpos");
Barcodes & QR Codes:
$printer->barcode("123456789", Printer::TYPE_CODE128);
$printer->qr("https://example.com", 30);
Cash Drawer Integration:
$printer->pulse(255, 100); // Open drawer (adjust pulse duration as needed)
AppServiceProvider for dependency injection:
$this->app->singleton('printer', function ($app) {
return new Printer(new NetworkPrintConnector(config('printer.ip'), config('printer.port')));
});
class PrintReceiptJob implements ShouldQueue
{
public function handle()
{
$printer = app('printer');
// ... print logic ...
}
}
ReceiptBuilder class to encapsulate common layouts:
class ReceiptBuilder {
public function build(Order $order) {
$printer = new Printer($connector);
$printer->text("=== RECEIPT ===");
foreach ($order->items as $item) {
$printer->text("{$item->name}: ${item->price}");
}
$printer->cut();
return $printer;
}
}
Connection Issues:
avahi://).9100 (default) may be blocked. Test with telnet <IP> 9100.lp group or adjust /etc/udev/rules.d/.Encoding Problems:
utf8 encoding explicitly:
$printer->setCharacterCode('UTF-8');
$printer->text("Café"); // Works with UTF-8
Printer::CHARCODE_ISO for non-UTF8 printers.Paper Feed Quirks:
cut() may not work on all printers. Use feed(50) (50 dots) as a fallback.CUT_PARTIAL) may tear paper; test with CUT_FULL.Memory Limits:
memory_limit. Stream output for big receipts:
$printer->open();
$printer->text("Large Data...");
$printer->close(); // Flushes immediately
Driver Compatibility:
$printer->raw(Printer::ESC_ALIGN_CENTER);
Printer to log output:
$printer = new Printer($connector);
$printer->setDebug(true); // Logs to stderr
FilePrintConnector:
Save output to a file and open it in a hex editor to verify commands.Connection refused: Printer offline or wrong IP/port.Invalid argument: Check barcode/Qr code size limits (max ~200px width).No data: Ensure close() is called to flush buffers.PrintConnector for proprietary protocols:
class CustomPrintConnector extends PrintConnector {
public function write($data) {
// Implement custom logic (e.g., HTTP API calls)
}
}
PrinterConfig class to manage device-specific settings:
class PrinterConfig {
public static function getForModel(string $model) {
return match ($model) {
'pos58' => ['width' => 202, 'timeout' => 5000],
default => ['width' => 384],
};
}
}
Notification::route('printer', $printer)
->notify(new PrintReceipt($order));
Printer::IMAGE_MODE_DOUBLE_HEIGHT or scale down:
$printer->graphics(file_get_contents('logo.png'), 200, 50);
$connector = new NetworkPrintConnector("192.168.1.100", 9100, 5000); // 5s timeout
setLineSpacing() for multi-line items:
$printer->setLineSpacing(30); // 30 dots = ~1.5 lines
FONT_B (bold) may not be supported on all printers. Test with FONT_A (normal).How can I help you explore Laravel packages today?