2
0
mirror of https://github.com/frappe/books.git synced 2024-12-24 11:55:46 +00:00

Load printwindow in background

This commit is contained in:
Faris Ansari 2018-10-24 23:42:40 +05:30
parent c8b05140e9
commit cb731ba45f

View File

@ -27,7 +27,11 @@ async function getPDFForElectron(doctype, name, destination, htmlContent) {
const filepath = path.join(destination, name + '.pdf'); const filepath = path.join(destination, name + '.pdf');
const fs = require('fs') const fs = require('fs')
let printWindow = new BrowserWindow({width: 600, height: 800}) let printWindow = new BrowserWindow({
width: 600,
height: 800,
show: false
})
printWindow.loadURL(`file://${path.join(__static, 'print.html')}`); printWindow.loadURL(`file://${path.join(__static, 'print.html')}`);
printWindow.on('closed', () => { printWindow.on('closed', () => {
@ -40,17 +44,20 @@ async function getPDFForElectron(doctype, name, destination, htmlContent) {
printWindow.webContents.executeJavaScript(code); printWindow.webContents.executeJavaScript(code);
printWindow.webContents.on('did-finish-load', () => { const printPromise = new Promise(resolve => {
printWindow.webContents.printToPDF({}, (error, data) => { printWindow.webContents.on('did-finish-load', () => {
if (error) throw error printWindow.webContents.printToPDF({}, (error, data) => {
printWindow.close();
fs.writeFile(filepath, data, (error) => {
if (error) throw error if (error) throw error
console.log('Write PDF successfully.') printWindow.close();
shell.openItem(filepath); fs.writeFile(filepath, data, (error) => {
if (error) throw error
resolve(shell.openItem(filepath));
})
}) })
}) })
}) })
await printPromise;
// await makePDF(html, filepath); // await makePDF(html, filepath);
} }