电子收据热敏打印机

时间:2018-11-11 22:58:37

标签: javascript html printing electron

我需要找到一种使用电子的javascript打印收据的方法。我已经尝试过 QZ-TRAY ,但是由于Electron,它不起作用。我还尝试过 node-thermal-printer (节点热敏打印机),但它也对我没有用。这里有没有人知道如何在不使用javascript(Electron)询问用户的情况下打印收据?

2 个答案:

答案 0 :(得分:1)

引用相关评论...

  
    

“ 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));

答案 1 :(得分:1)

请尝试使用电子正装打印机包装。 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);
   });
相关问题