PHP to Zebra Printer

Nirmal picture Nirmal · Sep 22, 2010 · Viewed 22.1k times · Source

I have this Zebra ZM400 Printer connected to the network (192.168.1.50). And I am trying to push a content to this printer directly from PHP.

This is the idea and I am just stuck without any way to do this. I tried the file_put_contents('192.168.1.50', $content) but with no success.

Would appreciate if anyone could please help me in sorting out this. Thank you :-)

.................................................................................................................................

Solution:

I printed using the LPR Protocol. No need to install driver or anything. The LPR Printing Class for PHP 5 can be downloaded from here:

http://www.phpclasses.org/package/2540-PHP-Abstraction-for-printing-documents.html

Answer

Edwin picture Edwin · Apr 11, 2013

I had a similar issue where I was using a java program to print to a zebra printer without a print driver, and wanted to recreate this using PHP. It was bugging me that I couldn't find the answer. Through some packet capturing with Wireshark comparing the two, I felt that it was possible. I finally stumbled into the solution (at least for me). This will print a label to a networked Zebra printer directly from a PHP page without the need for a driver.

<?php
error_reporting(E_ALL);

/* Get the port for the service. */
$port = "9100";

/* Get the IP address for the target host. */
$host = "172.17.144.89";

/* construct the label */
$mrn = "123456";
$registration_date = "03/13/2013";
$dob = "06/06/1976";
$gender = "M";
$nursing_station = "ED";
$room = "ED01";
$bed = "07";
$lastname = "Lastname";
$firstname = "Firstname";
$visit_id = "12345678";

$label = "q424\nN\n";
$label .= "A10,16,0,3,1,1,N,\"MR# " . $mrn . " ";
$label .= $registration_date . "\"\n";
$label .= "B10,43,0,3,2,4,50,N,\"" . $mrn . "\"\n";
$label .= "A235,63,0,3,1,1,N,\" ";
$label .= $dob . " ";
$label .= $gender . "\"\n";
$label .= "A265,85,0,3,1,1,N,\" ";
$label .= $nursing_station . " ";
$label .= $room . "-";
$label .= $bed . "\"\n";
$label .= "A10,108,0,3,1,1,N,\"";
$label .= $lastname . ",";
$label .= $firstname;
$label .= "\"\n";
$label .= "A10,135,0,3,1,1,N,\" #" . $visit_id . "\"\n";
$label .= "B10,162,0,3,2,4,50,N,\"" . $visit_id . "\"\n";
$label .= "P1\n";

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
    echo "socket_create() failed: reason: " . socket_strerror(socket_last_error    ()) . "\n";
} else {
    echo "OK.\n";
}

echo "Attempting to connect to '$host' on port '$port'...";
$result = socket_connect($socket, $host, $port);
if ($result === false) {
    echo "socket_connect() failed.\nReason: ($result) " . socket_strerror    (socket_last_error($socket)) . "\n";
} else {
    echo "OK.\n";
}

socket_write($socket, $label, strlen($label));
socket_close($socket);

?>