简体   繁体   English

在php中打印到Zebra打印机

[英]Print to Zebra printer in php

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? 寻找使用RAW端口9100从php网页打印到斑马IP打印机的正确代码。有谁知道这是否可行? I need to send a string in ZPL formatted output direct to ZM400 label printer. 我需要将ZPL格式的输出中的字符串直接发送到ZM400标签打印机。 I've searched high and low, the closest I've found is this: Print directly to network printer using php 我搜索了高低,我发现最接近的是: 使用php直接打印到网络打印机

It seems very close to what I need, but when my php page hits that code, it doesn't do anything. 它看起来非常接近我需要的东西,但是当我的php页面遇到该代码时,它什么也没做。 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);
?>

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. 主服务器是基于云的PHP服务器,不在本地网络上。 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: 在本地网络上,我们有另一台机器只运行WAMP和这个脚本,Zebra打印机本身也在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. 然后,在云服务器生成的网页上,我们有一些代码,它只是对本地网络上的服务器执行Ajax POST,在zpl_data中发布要打印的内容。

Edit 2017 编辑2017

We've now moved things over to run through PrintNode ( https://www.printnode.com/ ). 我们现在已将事情转移到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. 我不隶属于PrintNode。

If you're looking to send ZPL to the printer, you don't necessarily need a dedicated printing library. 如果您要将ZPL发送到打印机,则不一定需要专用的打印库。 You just need to open up a socket to that printer and send your ZPL directly. 您只需要打开该打印机的套接字并直接发送您的ZPL。 This is more of a general PHP socket-communication question as opposed to a printer-specific question. 这是一个普通的PHP套接字通信问题,而不是特定于打印机的问题。

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. 如果托管您的Web应用程序的服务器和打印机在同一网络上,那么您将能够打开套接字并发送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. 但是,如果您的打印机和Web应用程序服务器位于不同的网络上,则无法在该型号打印机上打印套接字,而无需其他浏览器插件或附件。 Generally speaking, accessing a remote printer (or any device) via a web-site is a security risk. 一般而言,通过网站访问远程打印机(或任何设备)存在安全风险。

The printer_open() and related functions are not part of the standard PHP language; printer_open()和相关函数不是标准PHP语言的一部分; 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. 但请注意,此扩展仅适用于在Windows上运行的PHP。

If your server is not Windows, you will need to use an external program to send data to the printer. 如果您的服务器不是Windows,则需要使用外部程序将数据发送到打印机。 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 这个信息,以及其他更多内容可以在SO上找到 - 例如: 在PHP应用程序中通过网络打印

Hope that helps. 希望有所帮助。

After hours of searchings I get the solutions : 经过几个小时的搜索,我得到了解决方案:

After you install the printer on the needed IP: 在所需的IP上安装打印机后: 在此输入图像描述

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 地点: printer = Epson-Cofetarie

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

file need Apsolute path 文件需要Apsolute路径

I hope, this simple code will resolve the problem, 我希望,这个简单的代码将解决问题,

  1. Put your ZPL code in a file eg "barcode.zpl" 将您的ZPL代码放在一个文件中,例如“barcode.zpl”
  2. Share your Zebra Printer, so that it can be accessed In Windows Explorer by typing 共享您的Zebra打印机,以便在Windows资源管理器中通过键入来访问它
    eg "\\192.168.1.113[YOUR PRINTER NAME]"; 例如“\\ 192.168.1.113 [您的打印机名称]”;
  3. Create PHP File, and write code: 创建PHP文件,并编写代码:

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

Thank You 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM