renderer.js
const ipc = require('electron').ipcRenderer
const printPDFBtn = document.getElementById('pdfME')
printPDFBtn.addEventListener('click', function (event) {
ipc.send('print-to-pdf')
})
main.js
const electron = require('electron')
const fs = require('fs')
const app = electron.app
const BrowserWindow = electron.BrowserWindow
const Menu = electron.Menu
const Tray = electron.Tray
const ipc = electron.ipcMain
const path = require('path')
const url = require('url')
const shell = electron.shell
let mainWindow
ipc.on('print-to-pdf', function (event) {
const pdfPath = path.join(__dirname, '/reports/print.pdf')
const win = BrowserWindow.fromWebContents(event.sender)
win.webContents.printToPDF({printBackground: true, landscape: true}, function (error, data) {
if (error) throw error
fs.writeFile(pdfPath, data, function (error) {
if (error) {
throw error
}
shell.openExternal('file://' + pdfPath)
event.sender.send('wrote-pdf', pdfPath)
})
})
})