Electron print without dialog (silent print)

Tags: , , ,



I just need to use electron js to build my desktop app, I use simple BrowserWindow to load my website in the application.

I added some functionality to reload the window when the connection issues so when the internet turn-on again the app will reload the page so it will not show “Page not found”.

In my web page it received an orders and print it to receipt printer, I don’t want the print dialog show out, is there any solution to print the receipt silently?

I know the way how to print it silent with the firefox but I need to use it now in my electron app.

my code:

const electron = require('electron')
const app = electron.app
const BrowserWindow = electron.BrowserWindow

const path = require('path')
const url = require('url')

let mainWindow

function createWindow () {
  mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    minWidth: 800,
    minHeight: 600,
    icon: __dirname + '/icon.ico'
  })

  mainWindow.loadURL(url.format({
    pathname: path.join(__dirname, 'index.html'),
    protocol: 'file:',
    slashes: true
  }))

  mainWindow.on('closed', function () {
    mainWindow = null
  })

}

app.on('ready', createWindow)

app.on('window-all-closed', function () {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', function () {
  if (mainWindow === null) {
    createWindow()
  }
})

Answer

There is the silent option of BrowserWindow.webContents.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({silent: false, printBackground: false, deviceName: ''}).

let win = new BrowserWindow(params);

win.webContents.print({silent: true});


Source: stackoverflow