PHP to Zebra Printer

可紊 提交于 2019-11-28 19:44:09

I'm pretty sure just pushing text content over a socket will not work at all.

You will need to print over lpr is my guess. Install a print server like CUPS and you can send it using that...

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);

?>

I see you've gotten solutions using LPR and FTP, but I'm almost certain the Zebra printers will accept raw ZPL text on port 9100.

Later?...

If you have access to the serial port:

LINUX:


 $fpi = fopen('/dev/ttyS0', 'r+');


$comando = "
N
Q70
A40,20,0,2,1,0,N,\"xxx\"
B40,40,0,1,2,6,100,N,\"$don\"
A40,145,0,3,1,0,N,\"N.- $don \"
A40,165,0,2,1,0,N,\"ccc\"
A0,198,7,1,1,0,N,\"$fecha\"
A19,198,7,1,1,0,N,\"fasdfas\"
P
";
fwrite($fpi, $comando);

fclose($fpi);


  • THIS WORK FINE

I have created a file in zebra language and used FTP to send the file many times in the past. just save the file and then create an ftp connection to the printer then use the put command to send the .txt file. the printer will do the rest

There is sample code on how to send ZPL directly to Zebra printers at:

https://km.zebra.com/kb/index?page=content&channel=SAMPLE_CODE&cat=ZISV_PL_ZPL

I didn't see any in PHP, but there is an example of talking directly to the printer via port 9100 using VB.

This is how to print on a Zebra Printer connected to the network: Assuming your printer is at IP: 192.168.1.50 and standard port : 9100

<?php 
if(($conn = fsockopen('192.168.1.50',9100,$errno,$errstr))===false){ 
    echo 'Connection Failed' . $errno . $errstr; 
} 

$data = ' 
    ^XA 
    ^FT50,200 
    ^A0N,200,200^FDTEST^FS 
    ^FT50,500 
    ^A0N,200,200^FDZebra Printer^FS 
    ^XZ'; 

#send request 
$fput = fputs($conn, $data, strlen($data)); 

#close the connection 
fclose($conn); 
?> 

This is working 100% on any ZPL compatible printer, not only Zebra. In this Example we print a label with a big TEST ZEBRA PRINTER in it

codaddict

Take a look at these PHP printer functions

Note that

These functions are only available under Windows 9.x, ME, NT4 and 2000.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!