我已经在NodeJS API上创建了它,我将其称为角应用程序中的一个按钮。
想要打印购买收据点击按钮,通过热打印机。没有任何PDF保存或打印对话框弹出,它应该直接打印。
我尝试过这个nodeJS代码,但看起来它适用于桌面应用程序,而不是web。我在不同的代码库里有UI和BE代码。
我尝试了以下代码:
'use strict';
const { PosPrinter } = require("electron-pos-printer");
class SeriesPrinter {
constructor() { }
printSr() {
console.log("Inside print function");
const print_data = [
{ type: 'text', value: 'Sample text', style: 'text-align:center;font-weight: bold' },
{ type: 'text', value: 'Another text', style: 'color: #fff' },
];
// returns promise<any>
PosPrinter.print(print_data, {
printerName: 'POS-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);
});
}
}
module.exports = SeriesPrinter;
它抛出一个错误:
Inside print function
TypeError: BrowserWindow is not a constructor
\node_modules\electron-pos-printer\dist\post-printer.js:87:30
at new Promise (<anonymous>)
\node_modules\electron-pos-p
任何修复此解决方案或任何其他解决方案的想法(UI端或nodeJS端任何解决方案都可以,它应该静默打印)
发布于 2021-11-07 09:16:36
您将得到TypeError: BrowserWindow不是构造函数,因为您在非电子环境(浏览器)中使用它。
既然你提到角,我会怎么做:
我将有一个服务,将我想要打印的数据发送到呈现数据的组件(实际上是使用来自服务器的数据制作可视化文档),然后是window.print().。
该服务如下所示:
export class PrintService {
isPrinting = false;
constructor(private router: Router) { }
printDocument(documentName: string, documentData: string[]) {
this.isPrinting = true;
this.router.navigate(['/',
{ outlets: {
'print': ['print', documentName, documentData.join()]
}}]);
}
onDataReady() {
setTimeout(() => {
window.print();
this.isPrinting = false;
this.router.navigate([{ outlets: { print: null }}]);
});
}
}
您可以将它用于像InvoiceComponent这样的组件中:
ngOnInit() {
this.invoiceDetails = this.invoiceIds
.map(id => this.getInvoiceDetails(id));
Promise.all(this.invoiceDetails)
.then(() => this.printService.onDataReady());
}
中调用window.print()即可。
在HTML中
<button (click)="onPrint()">Print</button>
在TS中
onPrint(){
window.print();
}
HTML
<div id="printSectionId" >
.......
<button (click)="printToCart('printSectionId')" class="button">Print</button>
...
</div>
TS
export class Component{
constructor(){}
printToCart(printSectionId: string){
let popupWinindow
let innerContents = document.getElementById(printSectionId).innerHTML;
popupWinindow = window.open('', '_blank', 'width=600,height=700,scrollbars=no,menubar=no,toolbar=no,location=no,status=no,titlebar=no');
popupWinindow.document.open();
popupWinindow.document.write('<html><head><link rel="stylesheet" type="text/css" href="style.css" /></head><body onload="window.print()">' + innerContents + '</html>');
popupWinindow.document.close();
}
}
https://stackoverflow.com/questions/69751832
复制相似问题