Electron 收据热敏打印机

Receipt thermal printer in Electron(Electron 收据热敏打印机)
本文介绍了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 收据热敏打印机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
Ordinals in words javascript(javascript中的序数)