Print to Zebra printer in php
Asked Answered
S

5

5

Looking for the proper code to print from php web page to a zebra IP printer using RAW port 9100. Does anyone know if this is possible? I need to send a string in ZPL formatted output direct to ZM400 label printer. I've searched high and low, the closest I've found is this: Print directly to network printer using php

It seems very close to what I need, but when my php page hits that code, it doesn't do anything. Here's the code I used:

<?php 
     $handle = printer_open('\\\\192.168.2.206:9100\\'); 
     printer_set_option($handle, PRINTER_MODE, "RAW");
     printer_write($handle, "TEXT To print"); 
     printer_close($handle);
?>
Schell answered 13/2, 2013 at 22:4 Comment(0)
P
3

If you're looking to send ZPL to the printer, you don't necessarily need a dedicated printing library. You just need to open up a socket to that printer and send your ZPL directly. This is more of a general PHP socket-communication question as opposed to a printer-specific question.

If the server hosting your web app and the printers are on the same network, then you will be able to open the socket and send the ZPL. If, however, your printers and web app server are on different networks, you will not be able to print over sockets, on that model printer, without additional browser plugins or add-ons. Generally speaking, accessing a remote printer (or any device) via a web-site is a security risk.

Prent answered 14/2, 2013 at 14:59 Comment(5)
Well that would be ideal! Is there a reference you'd recommend for socket printing in PHP? Thats really all I need. Its all internal, on the same network. I know my Zebra ZM400 is able to accept print over sockets. We currently do something similar from a small Access app for printing report labels. I just don't see any good examples for implementing with PHP. Seems like its doable...Schell
I found a link that explains Programming sockets in PHP. Do you think this is a step in the right direction?: devshed.com/c/a/PHP/Socket-Programming-With-PHPSchell
Resolved: binarytides.com/php-socket-programming-tutorial Copied and pasted code from section "Sending Data" Works great! Thnx for the pointer @PrentSchell
PHP ZPL builder, image conversion and a basic client for network-connected Zebra label printersRetarded
"Just need to open a socket to that printer." Right... I've tried socket_create, stream_socket_client, fopen, pfsockopen and nothing seems to be working.Decarlo
R
12

I realise this question is a little old but I recently had to perform this exact task and here is how I did it. Main server is a Cloud-Based PHP server which is not on the local network. On the local network we have another machine which is simply running WAMP and this script, the Zebra printer itself is also on the local network at IP 192.168.1.201:

<?php
/*
 * File Allows printing from web interface, simply connects to the Zebra Printer and then pumps data
 * into it which gets printed out.
 */
$print_data = $_POST['zpl_data'];

// Open a telnet connection to the printer, then push all the data into it.
try
{
    $fp=pfsockopen("192.168.1.201",9100);
    fputs($fp,$print_data);
    fclose($fp);

    echo 'Successfully Printed';
}
catch (Exception $e) 
{
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

Then, on the webpage generated by the cloud server we have some code which simply does an Ajax POST to the server on the local network, posting in the zpl_data to be printed.

Edit 2017

We've now moved things over to run through PrintNode (https://www.printnode.com/). We've found it to be really good so far, and allows us to print all kinds of documents without needing to use our own proxies, and also provide a whitelabelled installer so it looks like our own product. I'm not affiliated with PrintNode.

Rumal answered 22/5, 2013 at 10:20 Comment(2)
Code tested on a ZDesigner GK420D, connected on port 9100 on a Win2016 server - works like a charm. Only thing I needed to tweak was inserting a UTF8 encoding command into the .zpl file somewhere in the first lines: ^CI28Dunaj
Wamp server with this code is working for me on a Zebra ZD420 in 2017. Thank you for saving me a ton of frustration!Ataraxia
P
3

If you're looking to send ZPL to the printer, you don't necessarily need a dedicated printing library. You just need to open up a socket to that printer and send your ZPL directly. This is more of a general PHP socket-communication question as opposed to a printer-specific question.

If the server hosting your web app and the printers are on the same network, then you will be able to open the socket and send the ZPL. If, however, your printers and web app server are on different networks, you will not be able to print over sockets, on that model printer, without additional browser plugins or add-ons. Generally speaking, accessing a remote printer (or any device) via a web-site is a security risk.

Prent answered 14/2, 2013 at 14:59 Comment(5)
Well that would be ideal! Is there a reference you'd recommend for socket printing in PHP? Thats really all I need. Its all internal, on the same network. I know my Zebra ZM400 is able to accept print over sockets. We currently do something similar from a small Access app for printing report labels. I just don't see any good examples for implementing with PHP. Seems like its doable...Schell
I found a link that explains Programming sockets in PHP. Do you think this is a step in the right direction?: devshed.com/c/a/PHP/Socket-Programming-With-PHPSchell
Resolved: binarytides.com/php-socket-programming-tutorial Copied and pasted code from section "Sending Data" Works great! Thnx for the pointer @PrentSchell
PHP ZPL builder, image conversion and a basic client for network-connected Zebra label printersRetarded
"Just need to open a socket to that printer." Right... I've tried socket_create, stream_socket_client, fopen, pfsockopen and nothing seems to be working.Decarlo
C
2

The printer_open() and related functions are not part of the standard PHP language; they are part of an extension.

If you want to use them, you'll need to install the extension: See here for info on the printer extension.

However, please note that this extension is only available for PHP running on Windows.

If your server is not Windows, you will need to use an external program to send data to the printer. An example might look like this:

exec("lpr -P 'printer' -r 'filename.txt');

This info, and more can be found elsewhere on SO -- eg here: printing over network from PHP app

Hope that helps.

Compunction answered 13/2, 2013 at 22:29 Comment(2)
Gotcha, I'm running on Ubuntu LAMP. I took a look at the second link you posted...in your opinion, which would be better for a simple php webpage., the one you posted above, or the LPR printer class? Basically, I would like to have a print button that would print the value of a stored variable, which is a calculated Zebra ZPL string.Schell
I haven't tried the LPR class, so I can't comment on it. Using exec() to call the external lpr program should work pretty much universally though on a Linux platform.Compunction
S
0

After hours of searchings I get the solutions :

After you install the printer on the needed IP:enter image description here

exec('lp -d printer file');

In my case the command was:

exec('lp -d Epson-Cofetarie /home/clara/Desktop/txt.txt');

Where: printer = Epson-Cofetarie

file = /home/clara/Desktop/txt.txt

file need Apsolute path

Sporadic answered 29/5, 2015 at 9:45 Comment(2)
Wouldn't this run those commands in the server machine? what if the printer is plugged in the client machine?Eden
Server machine. To run on client machine you need to use ssh2. php.net/manual/ro/function.ssh2-exec.phpSporadic
C
0

I hope, this simple code will resolve the problem,

  1. Put your ZPL code in a file e.g "barcode.zpl"
  2. Share your Zebra Printer, so that it can be accessed In Windows Explorer by typing
    e.g "\192.168.1.113[YOUR PRINTER NAME]";
  3. Create PHP File, and write code:

<?php $file="barcode.zpl"; copy($file, "//192.168.1.113/ZDesigner GK420t"); ?>

Thank You

Cheeky answered 1/4, 2019 at 22:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.