問題描述
我需要找到一種在 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 收據熱敏打印機的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!