Javascript 从 Electron 应用程序打印
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32274265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Print from an Electron application
提问by leamasuero
I'm trying to use node printerfrom an Electron application, but as soon I add the lines to use the printer, the app crashes.
我正在尝试从 Electron 应用程序使用节点打印机,但是一旦我添加了使用打印机的行,应用程序就会崩溃。
The console outputs this:
控制台输出这个:
[1] 9860 segmentation fault (core dumped) node_modules/electron-prebuilt/dist/electron.
This is the app I'm running:
这是我正在运行的应用程序:
var app = require('app');
var BrowserWindow = require('browser-window');
var printer = require('printer');
require('crash-reporter').start();
app.on('ready', function() {
var mainWindow = new BrowserWindow({width: 800, height: 600});
mainWindow.loadUrl('file://' + __dirname + '/app/index.html');
mainWindow.openDevTools();
printer.printDirect({data:"print from Node.JS buffer" // or simple String: "some text"
, printer:'HP-Deskjet-F4400-series' // printer name, if missing then will print to default printer
, type: 'TEXT' // type: RAW, TEXT, PDF, JPEG, .. depends on platform
, success:function(jobID){
console.log("sent to printer with ID: "+jobID);
}
, error:function(err){console.log(err);}
});
});
Am I missing something?
我错过了什么吗?
I tried the node printer on its own, and I successfully printed some gibberish text.
我自己尝试了节点打印机,我成功打印了一些乱码。
回答by Yan Foto
node-printer
uses native bindings and according to the docs:
node-printer
使用本机绑定并根据文档:
The native Node modules are supported by Electron, but since Electron is using a different V8 version from official Node, you have to manually specify the location of Electron's headers when building native modules.
Electron 支持原生 Node 模块,但由于 Electron 使用的是与官方 Node 不同的 V8 版本,因此您必须在构建原生模块时手动指定 Electron 头文件的位置。
I suppose that is why you are getting the seg fault
. Try to build the module against the electron headers as mentioned in the docs:
我想这就是为什么你得到seg fault
. 尝试针对文档中提到的电子标题构建模块:
npm install --save-dev electron-rebuild
# Every time you run npm install, run this too
./node_modules/.bin/electron-rebuild
回答by Rohit Goyal
app.on('ready', () => {
let win = new BrowserWindow({width: 800, height: 600, resizable: false})
win.loadURL('file://' + __dirname + '/index.html')
win.webContents.on('did-finish-load', () => {
win.webContents.printToPDF({marginsType: 2, pageSize: "A3", landscape: false}, (error, data) => {
if (error) throw error
fs.writeFile('output.pdf', data, (error) => {
//getTitle of Window
console.log(win.webContents.getTitle())
//Silent Print
if (error) throw error
console.log('Write PDF successfully.')
})
})
})
})
Otherwise You can also use the following line
否则您也可以使用以下行
win.webContents.print({silent:true, printBackground:true})
回答by justin.m.chase
The node-printer
module has C++ code in it. Which means that you have to compile it using the same version of node that electron is using. This is doable actually, but it is pretty complicated.
该node-printer
模块中有 C++ 代码。这意味着您必须使用与电子使用的节点版本相同的节点来编译它。这实际上是可行的,但它非常复杂。
On the other hand, Electron already has printing API's in it:
另一方面,Electron 已经有打印 API 了:
https://electronjs.org/docs/api/web-contents#contentsprintoptions-callback
https://electronjs.org/docs/api/web-contents#contentsprintoptions-callback
If this api isn't sufficient and you still want to leverage the node-printer
module let me know and I will edit this response with a longer answer about how to fork and fix node-printer
so that it is electron compatible.
如果这个 api 不够用,而你仍然想利用该node-printer
模块,请告诉我,我将编辑此响应,并提供有关如何分叉和修复node-printer
以使其与电子兼容的更长答案。