electron获取打印机各位都很清楚,且官方文档描述的也比较详细,但是关于调用打印机部分,却是比较模糊的,而且过程比较繁琐,尤其采用了Vue方式对electron进行开发时,打印起来会更加麻烦。
这里推荐一个封装POS打印库,直接即可使用
安装:
$ npm install electron-pos-printer 或 $ yarn add electron-pos-printer
使用方法:
const {PosPrinter} = require("electron-pos-printer"); const path = require("path"); const options = { preview: false, // Preview in window or print width: '170px', // width of content body margin: '0 0 0 0', // margin of content body copies: 1, // Number of copies to print printerName: 'XP-80C', // printerName: string, check it at webContent.getPrinters() timeOutPerLine: 400 } const 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', // 'text' | 'barCode' | 'qrCode' | 'image' value: 'SAMPLE HEADING', style: `text-align:center;`, css: {"font-weight": "700", "font-size": "18px"} },{ type: 'text', // 'text' | 'barCode' | 'qrCode' | 'image' value: 'Secondary text', style: `text-align:left;color: red;`, css: {"text-decoration": "underline", "font-size": "10px"} },{ type: 'barCode', value: 'HB4587896', height: 12, // height of barcode, applicable only to bar and QR codes width: 1, // width of barcode, applicable only to bar and QR codes displayValue: true, // Display value below barcode fontsize: 8, },{ type: 'qrCode', value: 'https://github.com/Hubertformin/electron-pos-printer', height: 55, width: 55, style: 'margin: 10 20px 20 20px' } ] PosPrinter.print(data, options) .then(() => {}) .catch((error) => { console.error(error); });
评论