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

Use electron to generate pdf

This commit is contained in:
Faris Ansari 2018-10-24 23:22:02 +05:30
parent c5beae4353
commit c8b05140e9

View File

@ -21,11 +21,37 @@ async function makePDF(html, filepath) {
}
async function getPDFForElectron(doctype, name, destination, htmlContent) {
const { shell } = require('electron');
const { remote, shell } = require('electron');
const { BrowserWindow } = remote;
const html = htmlContent || await getHTML(doctype, name);
const filepath = path.join(destination, name + '.pdf');
await makePDF(html, filepath);
const fs = require('fs')
let printWindow = new BrowserWindow({width: 600, height: 800})
printWindow.loadURL(`file://${path.join(__static, 'print.html')}`);
printWindow.on('closed', () => {
printWindow = null;
});
const code = `
document.body.innerHTML = \`${html}\`;
`;
printWindow.webContents.executeJavaScript(code);
printWindow.webContents.on('did-finish-load', () => {
printWindow.webContents.printToPDF({}, (error, data) => {
if (error) throw error
printWindow.close();
fs.writeFile(filepath, data, (error) => {
if (error) throw error
console.log('Write PDF successfully.')
shell.openItem(filepath);
})
})
})
// await makePDF(html, filepath);
}
function setupExpressRoute() {