2
0
mirror of https://github.com/frappe/books.git synced 2025-02-02 20:18:26 +00:00

fix pdf generation

This commit is contained in:
Ankit Singhaniya 2021-08-18 23:27:39 +05:30 committed by 18alantom
parent 11e3474e8d
commit fd8221a576
2 changed files with 25 additions and 12 deletions

View File

@ -206,6 +206,16 @@ export function handleErrorWithDialog(e, doc) {
throw e; throw e;
} }
// NOTE: a hack to find all the css from the current document and inject it to the print version
// remove this if you are able to fix and get the default css loading on the page
function injectCSS(contents) {
const styles = document.getElementsByTagName('style')
for(let style of styles) {
contents.insertCSS(style.innerHTML);
}
}
export function makePDF(html, destination) { export function makePDF(html, destination) {
const { BrowserWindow } = remote; const { BrowserWindow } = remote;
@ -214,7 +224,8 @@ export function makePDF(html, destination) {
height: 842, height: 842,
show: false, show: false,
webPreferences: { webPreferences: {
nodeIntegration: process.env.ELECTRON_NODE_INTEGRATION nodeIntegration: process.env.ELECTRON_NODE_INTEGRATION,
enableRemoteModule: true
} }
}); });
@ -237,23 +248,25 @@ export function makePDF(html, destination) {
printWindow.webContents.executeJavaScript(code); printWindow.webContents.executeJavaScript(code);
return new Promise(resolve => { const printOptions = {
printWindow.webContents.on('did-finish-load', () => {
printWindow.webContents.printToPDF(
{
marginsType: 1, // no margin marginsType: 1, // no margin
pageSize: 'A4', pageSize: 'A4',
printBackground: true printBackground: true,
}, printBackgrounds: true,
(error, data) => { printSelectionOnly: false
if (error) throw error; };
return new Promise(resolve => {
printWindow.webContents.on('did-finish-load', () => {
injectCSS(printWindow.webContents);
printWindow.webContents.printToPDF(printOptions)
.then(data => {
printWindow.close(); printWindow.close();
fs.writeFile(destination, data, error => { fs.writeFile(destination, data, error => {
if (error) throw error; if (error) throw error;
resolve(shell.openItem(destination)); resolve(shell.openItem(destination));
}); });
} })
);
}); });
}); });
} }