This code presents a form with two inputs, the Ethernet POS machine IP and a value to encode. On form submition, if both IP and value to encode are provided, the code opens a TCP socket, and writes a ticket including some text and a QR code (using ESC/POS Epson® commands). If the printer doesn't support QR codes, Ethernet POS can emulate this feature.
To test this code, you need to create a .php file on a PHP web server and request the corresponding page using a web browser.
<?php // ESC/POS command defines define('ESC', "\x1b"); define('FS', "\x1c"); define('GS', "\x1d"); // TCP/IP helper functions function prn_socket_error() { $err = socket_last_error(); if ($err == 115 /* EINPROGRESS */) return socket_strerror(110 /* ETIMEDOUT */); else return socket_strerror($err); } function prn_socket_create($ip, &$error) { $timeouts = array('sec' => 15, 'usec' => 0); $socket = @socket_create(AF_INET, SOCK_STREAM, 0); if ($socket === false) { $error = prn_socket_error(); return false; } socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, $timeouts); socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, $timeouts); if (!@socket_connect($socket, $ip, 9100)) { $error = prn_socket_error(); socket_close($socket); return false; } return $socket; } function prn_socket_write($socket, $data, &$error) { if (!@socket_send($socket, $data, strlen($data), 0)) { $error = prn_socket_error(); return false; } return true; } // Form variables initialization $prn_ip = isset($_GET['ip']) ? $_GET['ip'] : ''; $qr_data = isset($_GET['data']) ? $_GET['data'] : ''; $error = ''; // Print ticket if IP and data are provided if ($prn_ip != '' && $qr_data != '') { // Initialize printer $cmd = ESC . "@"; // Add some header text $cmd .= "Ethernet POS Printing service\n"; $cmd .= "https://www.activeplus.com\n\n"; $cmd .= "This sample prints a QR code encoding\nthe value:\n"; $cmd .= "$qr_data\n\n"; // Align QR code center $cmd .= sprintf (ESC . 'a%c', 1); // Set QR code size 4 $cmd .= sprintf(GS . "(k\x03\x00\x31%c%c", 67, 4); // Set QR code data $len = strlen($qr_data) + 3; $cmd .= sprintf(GS . "(k%c%c\x31%c\x30%s", $len & 0xff, $len >> 8, 80, $qr_data); // Add QR code print command $cmd .= sprintf(GS . "(k\x03\x00\x31%c%c", 81, 48); // Add a line feed and a paper cut command. $cmd .= sprintf ("\n" . GS . "V%c%c", 65, 3); // Connect to printer, and send data on success $socket = prn_socket_create($prn_ip, $error); if ($socket !== false) { prn_socket_write($socket, $cmd, $error); socket_close($socket); } } // Encode value for HTML attributes $euri = htmlspecialchars($_SERVER['REQUEST_URI']); $eip = htmlspecialchars($prn_ip); $edata = htmlspecialchars($qr_data); ?> <!doctype html> <html> <head> <meta charset="utf-8" /> <title>Ethernet POS TCP/IP printing sample in PHP</title> </head> <body> <form action="<?= $euri ?>" method="get"> <h3>Ethernet POS direct printing sample</h3> <table> <tr> <td>Ethernet POS IP:</td> <td> <input type="text" name="ip" value="<?= $eip ?>" /> </td> </tr> <tr> <td>QR code value to print:</td> <td> <input type="text" name="data" value="<?= $edata ?>" /> </td> </tr> <tr> <td colspan="2" style="text-align: right"> <input type="submit" value="Print" /> </td> </tr> </table> <?php if ($error != '') { ?> <p style="color: red"><?= htmlspecialchars($error) ?></p> <?php } ?> </form> </body> </html>
Download tcpip.php