Electron 收据热敏打印机 [英] Receipt thermal printer in Electron

查看:48
本文介绍了Electron 收据热敏打印机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要找到一种在 Electron 的 javascript 中打印收据的方法.我已经尝试过 QZ-TRAY,但由于 Electron 无法正常工作.我也尝试过 node-thermal-printer,但它也从未对我有用.这里有人知道如何在不使用 javascript (Electron) 询问用户的情况下打印收据吗?

编辑

Qz-tray 提供了一个非常好的解决方案.

如果您遇到错误 RSVP 未定义,您需要使用此行启用原生 javascript 承诺.

qz.api.setPromiseType(resolver => new Promise(resolver));

解决方案

引用相关评论...

<块引用><块引用>

对于 QZ,我的问题是 RSVP 未定义,而对于 node-thermal-printer,打印机从未打印过."

对于 QZ,它花了 20 秒才找到这个:https://qz.io/wiki/2.0-api-override"

按照评论建议的方式发布解决方案.感谢@gilbert-gabriel 的帮助.

默认情况下启用 RSVP 承诺,但通过以下方式支持原生 JS 承诺:

qz.api.setPromiseType(resolver => new Promise(resolver));

一个更全面的例子:

//安装依赖:/*npm install qz-tray js-sha256*///提供 API 覆盖并开始与 QZ Tray 对话:从'qz-tray'导入*作为qz;从'js-sha256'导入{sha256};qz.api.setSha256Type(data => sha256(data));qz.api.setPromiseType(resolver => new Promise(resolver));qz.websocket.connect().then(qz.printers.getDefault).then(printer => console.log("默认打印机是:" + 打印机)).then(qz.websocket.disconnect).catch(err => console.error(err));

I need to find a way of printing receipts in javascript from Electron. I already tried QZ-TRAY but it doesn't work because of Electron. I also tried node-thermal-printer but it also never worked for me. Does anybody here know how you can print receipts without asking the user in javascript (Electron)?

EDIT

Qz-tray offer a solution that is pretty good and hard to beat.

If you have the error RSVP is not defined you need to enable native javascript promises with this line.

qz.api.setPromiseType(resolver => new Promise(resolver));

解决方案

Quoting the relevant comments...

"Well with QZ my problem was RSVP is not defined and with node-thermal-printer, the printer just never printed."

"for QZ it took all of 20secs to find this: https://qz.io/wiki/2.0-api-override"

Posting as asolution as the comments suggest it worked. Credits to @gilbert-gabriel for the help.

The RSVP promises are enabled by default, but native JS promises are supported via:

qz.api.setPromiseType(resolver => new Promise(resolver));

A more comprehensive example:

// Install dependencies:
/*
   npm install qz-tray js-sha256
*/

// Provide API overrides and start talking to QZ Tray:    
import * as qz from 'qz-tray';
import { sha256 } from 'js-sha256';

qz.api.setSha256Type(data => sha256(data));
qz.api.setPromiseType(resolver => new Promise(resolver));

qz.websocket.connect()
 .then(qz.printers.getDefault)
 .then(printer => console.log("The default printer is: " + printer))
 .then(qz.websocket.disconnect)
 .catch(err => console.error(err));

这篇关于Electron 收据热敏打印机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆