从电子申请打印 [英] Print from electron application
问题描述
我正在尝试使用电子应用程序中的节点打印机,但是一旦我添加了使用打印机,应用程序崩溃。
I'm trying to use node printer from an electron application, but as soon I add the lines to use the printer, the app crashes down.
控制台输出:
[1] 9860分段错误(核心转储)node_modules / electron-prebuilt / dist / electron。
这是我正在运行的应用程序。我只在电子文档中提供的简单应用示例中添加了打印行:
This is the app I'm running. I only added the printing lines to the simple app example provided on electron documentation:
var app = require('app'); // Module to control application life.
var BrowserWindow = require('browser-window'); // Module to create native browser window.
var printer = require('printer');
// Report crashes to our server.
require('crash-reporter').start();
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is GCed.
var mainWindow = null;
// Quit when all windows are closed.
app.on('window-all-closed', function() {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform != 'darwin') {
app.quit();
}
});
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
app.on('ready', function() {
// Create the browser window.
mainWindow = new BrowserWindow({width: 800, height: 600});
// and load the index.html of the app.
mainWindow.loadUrl('file://' + __dirname + '/app/index.html');
// Open the devtools.
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);}
});
// Emitted when the window is closed.
mainWindow.on('closed', function() {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null;
});
});
我错过了什么吗?
我自己尝试了节点打印机,并成功打印了一些乱码文本。
Am I missing something? I tried the node printer on its own and I successfully printed some gibberish text.
推荐答案
node-printer
使用本机绑定并根据 docs :
node-printer
uses native bindings and according to the docs:
Electron支持本机Node模块,但由于Electron
正在使用与官方节点不同的V8版本,你需要
在构建
本机模块时手动指定Electron标头的位置。
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.
我想这就是你得到 seg fault
的原因。尝试根据文档中提到的电子标题构建模块:
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:
npm install --save-dev electron-rebuild
# Every time you run npm install, run this too
./node_modules/.bin/electron-rebuild
这篇关于从电子申请打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!