Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 215
leamasuero Avatar asked Aug 28 '15 15:08

leamasuero


3 Answers

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
like image 186
Yan Foto Avatar answered Oct 01 '22 09:10

Yan Foto


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})
like image 36
Rohit Goyal Avatar answered Oct 01 '22 10:10

Rohit Goyal


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.

On the other hand, Electron already has printing API's in it:

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.

like image 25
justin.m.chase Avatar answered Oct 01 '22 10:10

justin.m.chase