Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
261 views
in Technique[技术] by (71.8m points)

javascript - Print from an 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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

node-printer uses native bindings and according to the docs:

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.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...