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 the package:
composer require mike42/escpos-php
Verify PHP extensions (required):
php -m | grep json,intl,zlib
Install missing extensions (e.g., sudo apt-get install php-json php-intl php-zlib on Ubuntu).
First use case: Print a simple receipt via Ethernet (replace 192.168.1.100 with your printer’s IP):
use Mike42\Escpos\PrintConnectors\NetworkPrintConnector;
use Mike42\Escpos\Printer;
$connector = new NetworkPrintConnector("192.168.1.100", 9100);
$printer = new Printer($connector);
$printer->text("Hello, ESC/POS!\n");
$printer->cut();
$printer->close();
example/ for OS/interface-specific setups (e.g., ethernet.php, linux-usb.php).CapabilityProfile::load("simple") for unknown printers or SP2000 for Star-branded devices.php script.php > output.esc) to verify commands before printing.$printer = new Printer(
new NetworkPrintConnector("192.168.1.100", 9100),
CapabilityProfile::load("default") // or "simple" for basic features
);
$printer->selectPrintMode(Printer::MODE_DOUBLE_HEIGHT);
$printer->text("STORE NAME\n");
$printer->setJustification(Printer::JUSTIFY_CENTER);
$printer->text("RECEIPT #12345\n");
$printer->setEmphasis(true); // Bold text
$printer->text("ITEM: Coffee\n");
$printer->setEmphasis(false);
$printer->text("Price: $2.50\n");
// Logo (requires GD/Imagick)
$logo = EscposImage::load("logo.png");
$printer->graphics($logo, Printer::IMG_DOUBLE_HEIGHT);
// Barcode (UPC-A)
$printer->barcode("123456789012", Printer::BARCODE_UPCA);
$printer->cut(Printer::CUT_FULL);
$printer->close();
dispatch(new PrintReceiptJob($orderId, $printerIp));
class ReceiptTemplate {
public function __invoke(Printer $printer, array $data) {
$printer->text($data['store_name']);
// ... render items, totals, etc.
}
}
try-catch:
try {
$printer->text("Error test");
} catch (Exception $e) {
Log::error("Print failed: " . $e->getMessage());
}
| Use Case | Implementation | Notes |
|---|---|---|
| Multi-language | Set code page: $printer->setCodePage(Printer::CODEPAGE_CP1252); |
Use CP1252 for Western European chars. |
| Partial Cuts | $printer->cut(Printer::CUT_PARTIAL, 2); |
Feeds 2 lines before cutting. |
| Images | $printer->graphics($image, Printer::IMG_DOUBLE_WIDTH); |
Optimize images to 300 DPI for clarity. |
| QR Codes | Use pdf417Code() for 2D codes. |
Limited to PDF417 standard. |
Printer-Specific Quirks:
feedForm() to release paper (not implemented in this driver; use cut() instead).CapabilityProfile::load("SP2000") for correct commands./dev/usb/lp*:
sudo usermod -a -G lp $USER
\\server\printer) and use WindowsPrintConnector.Encoding Issues:
$printer->setCodePage(Printer::CODEPAGE_ISO);
mb_convert_encoding() before printing.Image Problems:
Failed to load image. Ensure:
php -m | grep gd,imagick).imagefilter($img, IMG_FILTER_GRAYSCALE)) for better compatibility.Network Timeouts:
$connector = new NetworkPrintConnector("192.168.1.100", 9100, 2.0); // 2-second timeout
Partial Prints:
cut() lines parameter or check printer paper sensors.$connector = new FilePrintConnector("php://temp");
// ... print commands ...
file_put_contents("debug.esc", file_get_contents("php://temp"));
escpos-php's CLI Tool:
composer require mike42/escpos-php-cli
escpos-php --help
$printer->text("\n\n"); // Force feed
Custom PrintConnectors:
PrintConnector for proprietary protocols (e.g., Bluetooth):
class BluetoothPrintConnector extends PrintConnector {
public function write($data) {
// Implement Bluetooth socket logic
}
}
composer.json:
"autoload": {
"psr-4": {
"App\\PrintConnectors\\": "src/PrintConnectors"
}
}
Dynamic Profiles:
$profile = CapabilityProfile::load($this->getPrinterModel());
$profile = new CapabilityProfile(json_decode($dbProfile));
Fallbacks:
bitImage() for printers without graphics() support:
$printer->bitImage($image, Printer::IMG_DOUBLE_WIDTH);
FallbackPrintConnector that retries with a different interface if the primary fails.Testing:
PrintConnector in unit tests:
$mockConnector = $this->createMock(PrintConnector::class);
$mockConnector->method('write')->willReturn(true);
$printer = new Printer($mockConnector);
FilePrintConnector to test without a real printer.CODEPAGE_CP1252 (Western European)CODEPAGE_ISO (ISO-8859-1)CODEPAGE_GB18030 (Chinese)$height = min($printer->getMaxBarcodeHeight(),
How can I help you explore Laravel packages today?