1

web控制打印机的优秀工具-qz.io

 2 years ago
source link: https://gargantuax.github.io/blog/2017-10/web%E6%8E%A7%E5%88%B6%E6%89%93%E5%8D%B0%E6%9C%BA%E7%9A%84%E4%BC%98%E7%A7%80%E5%B7%A5%E5%85%B7-qz.io/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

以往通过web控制打印机,一般是通过js控制Applet运行java去控制打印机,但如今主流浏览器因为安全问题,已经不允许在网页中运行java了.非要运行java只能装低版本的浏览器,这容易产生安全问题,不是一个好办法. qz.io 能很好的解决这个问题.

qz.io

qz.io的思路很巧妙,通过一个客户端程序去驱动打印机,同时在客户端建立一个websocket服务器,暴露出api给web调用.支持所有浏览器,所有操作系统.控制打印机最难的驱动部分他已经帮我们搞定了.

更重要的是,这个工具是开源的! https://github.com/qzind/tray/

如何使用qz.io

https://qz.io/wiki/2.0-getting-started

<script type="text/javascript" src="js/dependencies/rsvp-3.1.0.min.js"></script>
<script type="text/javascript" src="js/dependencies/sha-256.min.js"></script>
<script type="text/javascript" src="js/qz-tray.js"></script>

示例代码:

qz.websocket.connect().then(function() { 
   return qz.printers.find("zebra")               // Pass the printer name into the next Promise
}).then(function(printer) {
   var config = qz.configs.create(printer);       // Create a default config for the found printer
   var data = ['^XA^FO50,50^ADN,36,20^FDRAW ZPL EXAMPLE^FS^XZ'];   // Raw ZPL
   return qz.print(config, data);
}).catch(function(e) { console.error(e); });
  

我们可以看到这个工具的调用是非常简单的.

使用qz.io的问题

qz.io去除提醒(Untrusted website)

qz.io的免费版,会提示不受信的打印请求,每个请求都必须手动去点允许,非常烦人.

提醒长这样的:

而这个受信的证书是需要买的,399美元.东西确实是好东西,无奈囊中羞涩,只能动手改代码,编译一个无提醒的版本,下载链接 http://pan.baidu.com/s/1dEOLaGL

请大家谨慎使用,给这家开源的公司的留些活路


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK