gpt4 book ai didi

Electron:如何将 API 响应的 ZPLCode 值直接打印到标签打印机?

转载 作者:行者123 更新时间:2023-12-03 12:38:45 28 4
gpt4 key购买 nike

我有一个 Electron 应用程序,我正在尝试打印到标签打印机。我向外部 API 发出请求,然后得到响应:

ResponsePackage:[
0: {
AirportCode: "",
BarCode: "111111111111111111111111111111",
PackageID: "xxxDEV1057-A",
ShippingCharge: 0,
TrackingNumber: "xxx000000380",
Unicode: "40",
ZPLCode: "~DGR:PCLMINI,825,25,000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000070FFC0FFC00000000000000000000000000000000000000000F1FFC1FFF80000000000000000000000000000000000000001E1FF81FFFC0000000000000007C000001F0000000000000003E3FF83FFFE000000000000000F8000003F0000000000000003E3FF03F1FE000000000000001F8000007E0000000000000007C7FF07E07E000000000000003F0000007E0000000000000007C7FE07E07E000000000000003F000000FC000000000000000F8FFE0FC07E7E07E1F1C03F807F03FE07FF803F807C3800000F8FFC0FC0FC7E0FC3F7C1FFC07E1FFF07FF81FFC07CF800000F0FFC1F80FCFC0FC3EFC7FFE07E1FFF8FFF07FFE0FDF000001F1FF81F81F8FC1F87FF8FFFE0FC3FFF8FFF0FFFE0FFF000001E1FF83F03F9F81F87FF9FCFE0FC303F8FFE1FCFE0FFE000003E3FF03F1FF1F83F87FC3F07E1F8001F87E03F07E1FF8000003C3FF07FFFE3F03F0FE07E07E1F8003F07E07E07E1FC0000007C7FF07FFFC3F03F0FC0FE07E3F007FF07C0FE07E3F8007FFC787FE07FFF03E07E1F80FC07E3F03FFF0FC0FC07E3F0007FF8F8FFE0FFFC07E07E1F81F80FC7E0FFFE0FC1F80FC7E0003FF8F8FFC0FC0007E0FC3F01F80FC7E1FFFE1F81F80FC7E0001FF1F1FFC1F8000FC0FC3F03F81F8FC3F0FC1F83F81F8FC0000FF1F1FF81F8000FC1F87E03F01F8FC7E0FC3F03F01F8FC00007E3E3FF83F0001F81F87E03F03F1FCFC1F83F03F03F1F800003E3E3FF03F0001F83F0FC03F07E1F8FC1F87F03F07E1F800001C3C7FF07E0001FCFF0FC03F0FC3F9FC3F07F03F0FC3F800000C7C7FE07E0003FFFE1F803FFF83F1FFFF07FC3FFF83F0000000787FE0FC0003FFFE1F803FFF03F1FFFE07FC3FFF03F0000000F8FFC0FC0001FF7E3F001FFE07E1FFFE07F81FFC07E0000000F0FF81F00001FC7C3E000FF007C0FE7E03F80FF007C000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 ... the rest of the response code ...
},
1: { .. another entry with similar format}
]
有这个响应的窗口中的相关代码有如下代码:

const options = {
// name: 'ZDesigner GC420d (EPL)',
silent: false,
printBackground: false,
color: false,
margin: {
marginType: 'printableArea'
},
landscape: false,
pagesPerSheet: 1,
collate: false,
copies: 1
}

if (orderCreated) {
responsePackage.map(puroPackage => ipcRenderer.send('print-job', [options, puroPackage.ZPLCode]) ) // ZLPCode is the code from the response object above
}
在我的 main.js 文件中,我有对事件使用react的 icpMain:
 ipcMain.on('print-job', (event, args) => {
console.log('args: ', args); // shows the proper ZPLCode passed in

// how do I print the ZPLCode passed in args?
mainWindow.webContents.print(args, (success, failureReason) => {
if (!success) console.log(failureReason);

console.log('Print initiated')
})
})
我不想打印 mainWindow 的内容,因为这个 ZPLCode 数据不会在窗口中,而只会在内存中发送到标签打印机。如果 ZPLCode 的内容不在窗口中,我如何打印它?

最佳答案

无需通过ipcRenderer 交流和 ipcMain并且无需使用webContents.print()因为我没有打印窗口的内容。
为了让它工作,我必须安装 node-printer来自 https://github.com/tojocky/node-printer使用 yarn .
然后我导入了printer通过import printer from 'printer';进入我的模块
使用console.log('printer.getPrinters: ', printer.getPrinters());查看操作系统上安装了哪些打印机。我要使用的名为“ZDesigner GC420d (EPL)”
然后,当我使用正确的 ZPLCode 从外部 API 得到响应时:

 // ResponsePackage is an array
ResponsePackage.forEach(package => printer.printDirect({
printer: 'ZDesigner GC420d (EPL)',
data: package.ZPLCode,
type: 'RAW',
success: (jobID) => {
console.log("sent to printer with ID: " + jobID);
},
error: (error) => console.log('error: ', error)
}) )

}

关于Electron:如何将 API 响应的 ZPLCode 值直接打印到标签打印机?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64794660/

28 4 0