2
0
mirror of https://github.com/frappe/books.git synced 2025-01-22 14:48:25 +00:00

Pdf for electron

This commit is contained in:
Faris Ansari 2018-04-09 17:59:13 +05:30
parent f7db1206b0
commit a30fb50405

View File

@ -15,6 +15,14 @@ module.exports = class PrintPage extends Page {
this.addButton(frappe._('Edit'), 'primary', () => {
frappe.router.setRoute('edit', this.doctype, this.name)
});
this.addButton(frappe._('Print'), 'secondary', async () => {
const pdf = require('frappejs/server/pdf');
const savePath = '/Users/farisansari/frappe.pdf';
pdf(await this.getHTML(true), savePath);
const { shell } = require('electron');
shell.openItem(savePath);
});
}
async show(params) {
@ -29,18 +37,28 @@ module.exports = class PrintPage extends Page {
}
async renderTemplate() {
this.printFormat = await frappe.getDoc('PrintFormat', this.meta.print.printFormat);
let doc = await frappe.getDoc(this.doctype, this.name);
let context = {doc: doc, frappe: frappe};
frappe.desk.setActiveDoc(doc);
try {
this.body.innerHTML = `<div class="print-page">${nunjucks.renderString(this.printFormat.template, context)}</div>`;
this.body.innerHTML = await this.getHTML();
// this.setTitle(doc.name);
} catch (e) {
this.renderError('Template Error', e);
throw e;
}
}
async getHTML(pdf = false) {
this.printFormat = await frappe.getDoc('PrintFormat', this.meta.print.printFormat);
let doc = await frappe.getDoc(this.doctype, this.name);
let context = {doc: doc, frappe: frappe};
frappe.desk.setActiveDoc(doc);
return `
${pdf ? `
<style>
${require('fs').readFileSync('./www/dist/css/style.css').toString()}
</style>
` : ''}
<div class="print-page">
${nunjucks.renderString(this.printFormat.template, context)}
</div>`;
}
}