Skip to content

Instantly share code, notes, and snippets.

@dgmid
Last active September 9, 2020 21:31
Show Gist options
  • Save dgmid/c5de74c2973633e178803eef65b827df to your computer and use it in GitHub Desktop.
Save dgmid/c5de74c2973633e178803eef65b827df to your computer and use it in GitHub Desktop.
Electron Fiddle Gist
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Printing test</title>
</head>
<body>
<h1>Printing test</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<button id="print">Print me</button>
<script>
require('./renderer.js')
</script>
</body>
</html>
// Modules to control application life and create native browser window
const {app, BrowserWindow, ipcMain} = require('electron')
let mainWindow
function createWindow () {
// Create the browser window.
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true
}
})
// and load the index.html of the app.
mainWindow.loadFile('index.html')
// Open the DevTools.
// mainWindow.webContents.openDevTools()
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})
ipcMain.on('print', (event, message) => {
console.log('PRINT')
const printOptions = { printBackground: true }
mainWindow.webContents.print(printOptions, (success, error) => {
if( !success ) {
console.log( error )
}
})
})
const {ipcRenderer} = require( 'electron' )
document.getElementById("print").onclick = printWindow
function printWindow() {
ipcRenderer.send( 'print', 'print' )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment