尝试使用 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);
});