Skip to content

Instantly share code, notes, and snippets.

@mgalla10
Last active February 4, 2022 16:44
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 mgalla10/1ab968669f64a7707306e876ab2ff5bc to your computer and use it in GitHub Desktop.
Save mgalla10/1ab968669f64a7707306e876ab2ff5bc to your computer and use it in GitHub Desktop.
Electron 17 Print Crash
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'">
<link href="./styles.css" rel="stylesheet">
<title>Hello World!</title>
</head>
<body>
<button id="btnPrint">Print</button>
<script src="./renderer.js"></script>
</body>
</html>
const {app, BrowserWindow, ipcMain, dialog} = require('electron')
const path = require('path')
function createWindow ()
{
const mainWindow = new BrowserWindow(
{
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})
mainWindow.loadFile('index.html')
ipcMain.on("printWindow", () =>
{
// works
// mainWindow.webContents.print()
// mainWindow.webContents.print({})
// mainWindow.webContents.print({}, null)
// crashes
mainWindow.webContents.print({}, () => {})
// mainWindow.webContents.print({}, function () {})
})
}
app.whenReady().then(() => createWindow())
app.on('window-all-closed', () => app.quit())
{
"name": "silly-legs-subtract-1dlyf",
"productName": "silly-legs-subtract-1dlyf",
"description": "My Electron application description",
"keywords": [],
"main": "./main.js",
"version": "1.0.0",
"author": "mgallagh",
"scripts": {
"start": "electron ."
},
"dependencies": {},
"devDependencies": {
"electron": "17.0.0"
}
}
const {contextBridge, ipcRenderer} = require('electron')
contextBridge.exposeInMainWorld("_print", () => ipcRenderer.send("printWindow"))
document.getElementById("btnPrint").addEventListener("click", () => window._print())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment