从电子应用打印

我试图从电子应用程序使用节点打印机 ,但是一旦我添加了使用打印机的行,应用程序就会崩溃。

控制台输出: [1] 9860 segmentation fault (core dumped) node_modules/electron-prebuilt/dist/electron.

这是我正在运行的应用程序。 我只在电子文档中提供的简单应用示例中添加了打印行:

 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; }); }); 

我错过了什么吗? 我自己尝试了节点打印机,并成功地打印了一些乱码文本。

node-printer使用本地绑定,并根据文档 :

Electron支持本地节点模块,但由于Electron正在使用与官方节点不同的V8版本,因此在构build本地模块时必须手动指定Electron头的位置。

我想这就是为什么你会遇到seg fault 。 尝试按照文档中提到的方法构build电子标题模块:

 npm install --save-dev electron-rebuild # Every time you run npm install, run this too ./node_modules/.bin/electron-rebuild 

node-printer模块具有C ++代码。 这意味着您必须使用与电子版本相同的版本进行编译。 这实际上是可行的,但是相当复杂。

另一方面,Electron已经有了打印API: http : //electron.atom.io/docs/v0.34.0/api/browser-window/#win-print-options

如果这个api不够用,你还想利用node-printer模块让我知道,我将用更长的答案编辑这个响应,如何分叉和修复node-printer以便它是电子兼容的。

 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.') }) }) }) 

否则,您也可以使用以下行

 win.webContents.print({silent:true, printBackground:true})