首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在使用JavaScript、角度或节点JS的热打印机上单击打印按钮

在使用JavaScript、角度或节点JS的热打印机上单击打印按钮
EN

Stack Overflow用户
提问于 2021-10-28 09:46:53
回答 1查看 1.5K关注 0票数 1

我已经在NodeJS API上创建了它,我将其称为角应用程序中的一个按钮。

想要打印购买收据点击按钮,通过热打印机。没有任何PDF保存或打印对话框弹出,它应该直接打印。

我尝试过这个nodeJS代码,但看起来它适用于桌面应用程序,而不是web。我在不同的代码库里有UI和BE代码。

我尝试了以下代码:

代码语言:javascript
运行
复制
'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;

它抛出一个错误:

代码语言:javascript
运行
复制
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端任何解决方案都可以,它应该静默打印)

EN

回答 1

Stack Overflow用户

发布于 2021-11-07 09:16:36

您将得到TypeError: BrowserWindow不是构造函数,因为您在非电子环境(浏览器)中使用它。

既然你提到角,我会怎么做:

我将有一个服务,将我想要打印的数据发送到呈现数据的组件(实际上是使用来自服务器的数据制作可视化文档),然后是window.print().。

该服务如下所示:

代码语言:javascript
运行
复制
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这样的组件中:

代码语言:javascript
运行
复制
ngOnInit() {
  this.invoiceDetails = this.invoiceIds
    .map(id => this.getInvoiceDetails(id));
  Promise.all(this.invoiceDetails)
    .then(() => this.printService.onDataReady());
}

  1. 如果您不想做一些花哨的事情,只需简单地列出要打印的内容,只需从组件:

中调用window.print()即可。

在HTML中

代码语言:javascript
运行
复制
<button (click)="onPrint()">Print</button>

在TS中

代码语言:javascript
运行
复制
onPrint(){
    window.print();
}

  1. 您还可以使用打开/写/关闭弹出窗口方法:

HTML

代码语言:javascript
运行
复制
<div id="printSectionId" >
.......
    <button (click)="printToCart('printSectionId')" class="button">Print</button>
...
</div>

TS

代码语言:javascript
运行
复制
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();
  }

 }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69751832

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档