在 php 中打印到 Zebra 打印机

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14864065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:06:12  来源:igfitidea点击:

Print to Zebra printer in php

phpprintingnetwork-printers

提问by user2070092

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

正在寻找使用 RAW 端口 9100 从 php 网页打印到 zebra IP 打印机的正确代码。有谁知道这是否可能?我需要将 ZPL 格式输出中的字符串直接发送到 ZM400 标签打印机。我搜索了高低,我找到的最接近的是:使用 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 页面点击该代码时,它什么也不做。这是我使用的代码:

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

采纳答案by jason.zissman

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.

如果您希望将 ZPL 发送到打印机,则不一定需要专用的打印库。您只需要为该打印机打开一个套接字并直接发送您的 ZPL。这更像是一个通用的 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. 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.

如果托管您的 Web 应用程序的服务器和打印机在同一网络上,那么您将能够打开套接字并发送 ZPL。但是,如果您的打印机和 Web 应用程序服务器位于不同的网络上,则在没有其他浏览器插件或附加组件的情况下,您将无法在该型号打印机上通过套接字进行打印。一般来说,通过网站访问远程打印机(或任何设备)存在安全风险。

回答by user984976

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 服务器。在本地网络上,我们有另一台仅运行 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/). 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 ( https://www.printnode.com/)运行。到目前为止,我们发现它非常好,允许我们打印各种文档而无需使用我们自己的代理,并且还提供了一个白色标签的安装程序,因此它看起来像我们自己的产品。我不隶属于 PrintNode。

回答by Spudley

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

printer_open()和相关功能不是标准的PHP语言的一部分; 它们是扩展的一部分。

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. An example might look like this:

如果您的服务器不是 Windows,您将需要使用外部程序将数据发送到打印机。一个示例可能如下所示:

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.

希望有帮助。

回答by Florin

After hours of searchings I get the solutions :

经过数小时的搜索,我得到了解决方案:

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

在所需 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

其中:打印机= Epson-Cofetarie

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

文件= /home/clara/Desktop/txt.txt

file need Apsolute path

文件需要绝对路径

回答by Ary Parker

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:
  1. 将您的 ZPL 代码放在一个文件中,例如“barcode.zpl”
  2. 共享您的 Zebra 打印机,以便可以在 Windows 资源管理器中通过键入
    例如“\192.168.1.113[您的打印机名称]”来访问它;
  3. 创建 PHP 文件,并编写代码:

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

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

Thank You

谢谢你