Silent printing in electron

I am currently building an electron app. I have a PDF on my local file system which I need to silently print out (on the default printer). I came across the node-printer library, but it doesn't seem to work for me. Is there an easy solution to achieve this?

2,738 3 3 gold badges 27 27 silver badges 46 46 bronze badges asked Sep 2, 2017 at 9:13 Simon Schiller Simon Schiller 654 1 1 gold badge 8 8 silver badges 24 24 bronze badges

3 Answers 3

Well first of all it is near impossible to understand what you mean with "silent" print. Because once you send a print order to your system printer it will be out of your hand to be silent at all. On Windows for example once the order was given, at least the systemtray icon will indicate that something is going on. That said, there are very good described features for printing with electron even "silent" is one of them:

You need to get all system printers if you do not want to use the default printer:

contents.getPrinters() 

Which will return a PrinterInfo[] Object.

Here is an example how the object will look like from the electron PrtinerInfo Docs:

To print your file you can do it with

contents.print([options]) 

The options are descriped in the docs for contents.print():

Prints window’s web page. When silent is set to true, Electron will pick the system’s default printer if deviceName is empty and the default settings for printing.

Calling window.print() in web page is equivalent to calling webContents.print() .

Use page-break-before: always; CSS style to force to print to a new page.

So all you need is to load the PDF into a hidden window and then fire the print method implemented in electron with the flag set to silent.

// In the main process. const = require('electron'); let win = null; app.on('ready', () => < // Create window win = new BrowserWindow(); // Could be redundant, try if you need this. win.once('ready-to-show', () => win.hide()) // load PDF. win.loadURL(`file://directory/to/pdf/document.pdf`); // if pdf is loaded start printing. win.webContents.on('did-finish-load', () => < win.webContents.print(); // close window after print order. win = null; >); >); 

However let me give you a little warning: Once you start printing it can and will get frustrating because there are drivers out there which will interpret data in a slightly different way. Meaning that margins could be ignored and much more. Since you already have a PDF this problem will most likely not happen. But keep this in mind if you ever want to use this method for example contents.printToPDF(options, callback) . That beeing said there are plently of options to avoid getting frustrated like using a predefined stylesheet like descriped in this question: Print: How to stick footer on every page to the bottom?