【发布时间】:2021-10-28 09:46:53
【问题描述】:
我在 NodeJS API 上创建了,我在 Angular 应用程序中单击按钮时调用它。
想要通过热敏打印机点击按钮打印购买收据。没有任何 PDF 保存或打印对话框弹出,应该直接打印。
我尝试了这个 nodeJS 代码,但看起来它适用于桌面应用程序而不是 web。我在不同的代码库 repo 中有 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 端都可以,它应该静默打印)
【问题讨论】:
-
我怀疑是否可以在未经用户许可的情况下打印,即某种弹出窗口。
-
根据
electron-pos-printer的the README,它需要电子。如果您不熟悉 electron,它是一个用于桌面应用程序的 node.js 框架,它期望某些文件存在。您可以在github.com/fssonca/electron-printer 处查看自述文件链接到的示例电子应用程序。您可能可以通过向示例应用添加 API 服务器来获得所需的功能,但它可能有点笨重。 -
@sandip,仅使用
require("electron-pos-printer")并不意味着您将 node.js 应用程序作为电子应用程序运行。相反,它是一个期望在电子环境中运行的库。您的错误表明它无法获取electron的BrowserWindow实例。你可以看到它被添加到哪里here -
构建一个与热敏打印机接口的桌面 nodejs 服务器,并在 localhost 上公开一个 API,可以通过您的网页访问。
-
@sandip @steve 试图告诉我们的是正确的。您为此使用了错误的库。你导入了
electron-pos-printer,里面有electron这个词,所以这意味着它只能被电子应用程序使用。删除electron-pos-printer并尝试其他库,如ESCPOS或node-thermal-printer。我认为直接从 Angular 是不可能的,所以你需要专注于后端并从那里开始。
标签: javascript node.js angular thermal-printer