Network Delivery

EscposPrinter::end()/dump() return a plain string of ESC/POS bytes. What to do with that string — save it to a file, return it from an API, or send it to a real printer — is up to you, using whatever your application already uses for that (file writes, HTTP responses, etc.). This package does not wrap trivial I/O like file_put_contents().

Delivering bytes to a network thermal printer over TCP is a different matter: it needs connection timeout handling and clear error reporting, which is easy to get subtly wrong by hand (and it is the standard delivery mechanism for ESC/POS printers, not a generic file/API concern). Derafu\Escpos\Transport\NetworkTransport covers exactly that, with plain PHP streams — it has no dependency on mike42/escpos-php or any other ESC/POS library.

use Derafu\Escpos\Transport\NetworkTransport;

$escposData = $printer->end();

(new NetworkTransport())->send($escposData, '172.16.1.5');
// Port defaults to NetworkTransport::DEFAULT_PORT (9100, the de facto
// standard raw ESC/POS printing port). Pass a third argument to override
// it, and a fourth to change the connection timeout (default 5 seconds).

If the connection fails, or not all bytes could be delivered, it throws Derafu\Escpos\Exception\NetworkDeliveryException with the real connection error — never a silently swallowed failure.

use Derafu\Escpos\Exception\NetworkDeliveryException;
use Derafu\Escpos\Transport\NetworkTransport;

try {
    (new NetworkTransport())->send($escposData, '172.16.1.5');
} catch (NetworkDeliveryException $e) {
    // $e->getMessage() includes the host, port, and the underlying error.
}

bin/print.php

The repository also has bin/print.php, a thin CLI wrapper around NetworkTransport used to quickly validate ESC/POS output against a real printer during development:

bin/print.php 172.16.1.5:9100 ticket.escpos
cat ticket.escpos | bin/print.php 172.16.1.5
php examples/full-example.php | bin/print.php 172.16.1.5

This is an internal development tool, not part of the package’s public API: it is not registered as a Composer binary, so it will not appear in vendor/bin/ when you require this package. It still physically exists inside the installed package, at vendor/derafu/escpos/bin/print.php, so it can be run from there if you know the path — but it is not documented or supported as something to build on. Use NetworkTransport directly in your own code instead.

On this page

Last updated on 04/08/2026 by Anonymous