Last active
August 11, 2021 09:08
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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