2
0
mirror of https://github.com/frappe/books.git synced 2024-11-10 07:40:55 +00:00

chore: Remove electron pdf generation

- Moving it to accounting repo
This commit is contained in:
Faris Ansari 2019-12-07 00:03:27 +05:30
parent 859939e763
commit 496401c47e

View File

@ -21,78 +21,6 @@ async function makePDF(html, filepath) {
await browser.close();
}
async function getPDFForElectron(doctype, name, destination, htmlContent) {
const { remote, shell } = require('electron');
const { BrowserWindow } = remote;
const html = htmlContent || (await getHTML(doctype, name));
if (!destination) {
let folder =
process.env.NODE_ENV === 'development'
? path.resolve('.')
: remote.getGlobal('documentsPath');
destination = path.join(folder, `${name}.pdf`);
}
const fs = require('fs');
let printWindow = new BrowserWindow({
width: 600,
height: 800,
show: false,
webPreferences: {
nodeIntegration: true
}
});
let url;
if (process.env.NODE_ENV === 'development') {
url = `http://localhost:${process.env.PORT}/static/print.html`;
} else {
let printPath = path.join(
remote.app.getAppPath(),
'dist',
'electron',
'static',
'print.html'
);
url = `file://${printPath}`;
}
printWindow.loadURL(url);
printWindow.on('closed', () => {
printWindow = null;
});
const code = `
let el = document.querySelector('.printTarget');
document.body.innerHTML = \`${html}\`;
`;
printWindow.webContents.executeJavaScript(code);
const printPromise = new Promise(resolve => {
printWindow.webContents.on('did-finish-load', () => {
printWindow.webContents.printToPDF(
{
marginsType: 1, // no margin
pageSize: 'A4',
printBackground: true
},
(error, data) => {
if (error) throw error;
printWindow.close();
fs.writeFile(destination, data, error => {
if (error) throw error;
resolve(shell.openItem(destination));
});
}
);
});
});
await printPromise;
}
function setupExpressRoute() {
if (!frappe.app) return;
frappe.app.post('/api/method/pdf', frappe.asyncHandler(handlePDFRequest));