2018-02-21 09:38:56 +00:00
|
|
|
const Page = require('frappejs/client/view/page');
|
|
|
|
const frappe = require('frappejs');
|
|
|
|
const nunjucks = require('nunjucks/browser/nunjucks');
|
|
|
|
|
|
|
|
nunjucks.configure({ autoescape: false });
|
|
|
|
|
|
|
|
module.exports = class PrintPage extends Page {
|
|
|
|
constructor(doctype) {
|
|
|
|
let meta = frappe.getMeta(doctype);
|
2018-02-22 07:36:28 +00:00
|
|
|
super({title: `${meta.name}`, hasRoute: true});
|
2018-02-21 09:38:56 +00:00
|
|
|
this.meta = meta;
|
|
|
|
this.doctype = doctype;
|
2018-03-08 13:31:22 +00:00
|
|
|
this.titleElement.classList.add('hide');
|
2018-02-21 09:38:56 +00:00
|
|
|
|
|
|
|
this.addButton(frappe._('Edit'), 'primary', () => {
|
|
|
|
frappe.router.setRoute('edit', this.doctype, this.name)
|
|
|
|
});
|
2018-04-09 12:29:13 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
2018-02-21 09:38:56 +00:00
|
|
|
}
|
|
|
|
|
2018-03-05 16:45:21 +00:00
|
|
|
async show(params) {
|
|
|
|
super.show();
|
|
|
|
this.name = params.name;
|
|
|
|
if (this.meta.print) {
|
|
|
|
// render
|
|
|
|
this.renderTemplate();
|
|
|
|
} else {
|
|
|
|
this.renderError('No Print Settings');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-21 09:38:56 +00:00
|
|
|
async renderTemplate() {
|
|
|
|
try {
|
2018-04-09 12:29:13 +00:00
|
|
|
this.body.innerHTML = await this.getHTML();
|
2018-03-08 13:31:22 +00:00
|
|
|
// this.setTitle(doc.name);
|
2018-02-21 09:38:56 +00:00
|
|
|
} catch (e) {
|
|
|
|
this.renderError('Template Error', e);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
2018-04-09 12:29:13 +00:00
|
|
|
|
|
|
|
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>`;
|
|
|
|
}
|
2018-02-21 09:38:56 +00:00
|
|
|
}
|