【问题标题】:Receipt thermal printer in ElectronElectron 收据热敏打印机
【发布时间】:2019-04-14 16:44:22
【问题描述】:

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

编辑

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

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

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

【问题讨论】:

  • 收据是什么文件格式?
  • 暂时什么都没有,只想打印一行Hello World
  • 好吧,定义“不起作用”。
  • 好吧,QZ 我的问题是RSVP is not defined 和node-thermal-printer,打印机从来没有打印过。
  • QZ 花了 20 秒才找到这个:qz.io/wiki/2.0-api-overrides

标签: javascript html printing electron qz-tray


【解决方案1】:

尝试使用包电子-pos-printer。 npm i electron-pos-printer .查看documentation

演示

// 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);
   });

【讨论】:

  • 如何在不安装驱动的情况下使用串口打印?
【解决方案2】:

引用相关的cmets...

“对于 QZ,我的问题是 RSVP is not defined,而对于 node-thermal-printer,打印机从未打印过。”

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

按照 cmets 的建议发布解决方案。感谢@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));

【讨论】:

  • 感谢您将所有内容放在一个示例中,我需要重做此操作,这确实很有帮助!
猜你喜欢
  • 1970-01-01
  • 2017-03-12
  • 1970-01-01
  • 2013-07-04
  • 2015-10-25
  • 1970-01-01
  • 1970-01-01
  • 2012-10-05
相关资源
最近更新 更多