6

我需要从Electron找到一种用 javascript 打印收据的方法。我已经尝试过QZ-TRAY,但由于 Electron,它不起作用。我也尝试过node-thermal-printer,但它也从未对我有用。这里有人知道如何打印收据而不用 javascript (Electron) 询问用户吗?

编辑

Qz-tray 提供了一个非常好的解决方案,而且很难被击败。

如果您遇到错误,则RSVP is not defined需要使用此行启用本机 javascript 承诺。

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

4

2 回答 2

4

尝试使用 package electron-pos-printer。npm i electron-pos-printer . 查看文档

演示

// In the main process
const {PosPrinter} = require("electron-pos-printer");
// or in render process
const {PosPrinter} = require('electron').remote.require("electron-pos-printer");

// each object in the data array accounts for a row or line
const print_data = [
    {
      type: 'image',                                       
      path: path.join(__dirname, 'assets/banner.png'),     // file path
      position: 'center',                                  // position of image: 'left' | 'center' | 'right'
      width: 60,                                           // width of image in px; default: auto
      height: 60,                                          // width of image in px; default: 50 or '50px'
   },
   {type: 'text', value: 'Sample text', style: 'text-align:center;font-weight: bold'},
   {type: 'text', value: 'Another text', style: 'color: #fff'},
   {type 'barCode', value: 'HB4587896', height: 12, width: 1, fontsize: 9},
   {type 'qrCode', value: 'https://google.com', height: 55, width: 55, style: 'margin: 10 20px 20 20px'}
];

// returns promise<any>
 PosPrinter.print(print_data, {
    printerName: 'XP-80C',
    preview: false,
    width: '170px',               //  width of content body
    margin: '0 0 0 0',            // margin of content body
    copies: 1,                   // The number of copies to print
  })
  .then(() => {
    // some code ...
  })
  .catch((error) => {
    console.error(error);
   });
于 2019-10-31T02:55:59.720 回答
3

引用相关评论...

“好吧,QZ 我的问题是RSVP is not defined节点热敏打印机,打印机从来没有打印过。”

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

正如评论所暗示的那样发布解决方案。感谢@gilbert-gabriel 的帮助。

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

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

一个更全面的例子:

// 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));
于 2019-01-17T17:37:26.107 回答