2018-04-14 19:14:02 +00:00
|
|
|
const frappe = require('frappejs');
|
2018-03-29 18:51:56 +00:00
|
|
|
const puppeteer = require('puppeteer');
|
2018-04-14 19:14:02 +00:00
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
const { getTmpDir } = require('frappejs/server/utils');
|
|
|
|
const { getHTML } = require('frappejs/common/print');
|
|
|
|
const { getRandomString } = require('frappejs/utils');
|
2018-03-29 18:51:56 +00:00
|
|
|
|
2018-04-14 19:14:02 +00:00
|
|
|
async function makePDF(html, filepath) {
|
2018-03-29 18:51:56 +00:00
|
|
|
const browser = await puppeteer.launch();
|
|
|
|
const page = await browser.newPage();
|
|
|
|
await page.setContent(html);
|
|
|
|
await page.pdf({
|
|
|
|
path: filepath,
|
|
|
|
format: 'A4'
|
|
|
|
});
|
|
|
|
await browser.close();
|
|
|
|
}
|
2018-04-14 19:14:02 +00:00
|
|
|
|
|
|
|
function setupExpressRoute() {
|
|
|
|
if (!frappe.app) return;
|
|
|
|
frappe.app.post('/api/method/pdf', frappe.asyncHandler(handlePDFRequest));
|
|
|
|
}
|
|
|
|
|
|
|
|
async function handlePDFRequest(req, res) {
|
|
|
|
const args = req.body;
|
|
|
|
const { doctype, name } = args;
|
|
|
|
const html = await getHTML(doctype, name);
|
|
|
|
|
|
|
|
const filepath = path.join(getTmpDir(), `frappe-pdf-${getRandomString()}.pdf`);
|
|
|
|
await makePDF(html, filepath);
|
|
|
|
|
|
|
|
const file = fs.createReadStream(filepath);
|
|
|
|
const stat = fs.statSync(filepath);
|
|
|
|
res.setHeader('Content-Length', stat.size);
|
|
|
|
res.setHeader('Content-Type', 'application/pdf');
|
|
|
|
res.setHeader('Content-Disposition', `attachment; filename=${path.basename(filepath)}`);
|
|
|
|
file.pipe(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
makePDF,
|
|
|
|
setupExpressRoute
|
|
|
|
}
|