Documentation Ethernet POS - Exemple PHP d'impression TCP/IP

Notes

Ce code présente un formulaire avec deux champs, l'adresse IP de la machine exécutant Ethernet POS, et une valeur à encoder. Lors de la soumission du formulaire, si les deux champs sont renseignés, le code ouvre une connexion TCP, et y écrit un ticket incluant du texte et un code QR (en utilisant des commandes Epson® ESC/POS). Si l'imprimante ne supporte pas l'impression de code QR, Ethernet POS peut émuler cette fonctionnalité.

Pour tester ce code, vous devez créer un fichier .php sur un serveur web PHP, et effectuer une requête sur la page correspondante via un navigateur web.

Code <?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>

Télécharger tcpip.php