Skip to content

Instantly share code, notes, and snippets.

@stephanbogner
Last active August 11, 2021 09:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephanbogner/160f4fe24ed9ae06015eafe18331213f to your computer and use it in GitHub Desktop.
Save stephanbogner/160f4fe24ed9ae06015eafe18331213f to your computer and use it in GitHub Desktop.
Print silently (no print dialog/window opening) in electron.js with a physical printer - no external libraries needed
// Adjusted from https://stackoverflow.com/a/46020931/3763660
// This does not work on its own, but needs to be placed in the electron.js file
printSilently('Hello physical world');
function printSilently(text){
let printWin = new BrowserWindow({width: 800, height: 600, show: false });
let file = 'data:text/html;charset=UTF-8, <p>' + text + '</p>'
printWin.loadURL(file)
printWin.webContents.on('did-finish-load', () => {
printWin.webContents.print({silent: true});
printWin = null;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment