2018-04-14 19:14:02 +00:00
|
|
|
const frappe = require('frappejs');
|
2019-08-14 09:39:55 +00:00
|
|
|
const puppeteer = require('puppeteer-core');
|
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) {
|
2019-08-14 09:39:55 +00:00
|
|
|
const browser = await puppeteer.launch();
|
|
|
|
const page = await browser.newPage();
|
|
|
|
await page.setContent(html);
|
|
|
|
await page.addStyleTag({
|
|
|
|
url:
|
|
|
|
'https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css'
|
|
|
|
});
|
|
|
|
await page.pdf({
|
|
|
|
path: filepath,
|
|
|
|
format: 'A4'
|
|
|
|
});
|
|
|
|
await browser.close();
|
2018-03-29 18:51:56 +00:00
|
|
|
}
|
2018-04-14 19:14:02 +00:00
|
|
|
|
2018-10-22 20:51:19 +00:00
|
|
|
async function getPDFForElectron(doctype, name, destination, htmlContent) {
|
2019-08-14 09:39:55 +00:00
|
|
|
const { remote, shell } = require('electron');
|
|
|
|
const { BrowserWindow } = remote;
|
|
|
|
const html = htmlContent || (await getHTML(doctype, name));
|
2019-09-03 07:22:14 +00:00
|
|
|
if (!destination) {
|
2019-11-28 17:38:37 +00:00
|
|
|
let folder =
|
2019-09-03 07:22:14 +00:00
|
|
|
process.env.NODE_ENV === 'development'
|
|
|
|
? path.resolve('.')
|
|
|
|
: remote.getGlobal('documentsPath');
|
2019-11-28 17:38:37 +00:00
|
|
|
destination = path.join(folder, `${name}.pdf`);
|
2019-09-03 07:22:14 +00:00
|
|
|
}
|
|
|
|
|
2019-08-14 09:39:55 +00:00
|
|
|
const fs = require('fs');
|
|
|
|
let printWindow = new BrowserWindow({
|
|
|
|
width: 600,
|
|
|
|
height: 800,
|
2019-11-28 17:38:37 +00:00
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
nodeIntegration: true
|
|
|
|
}
|
2019-08-14 09:39:55 +00:00
|
|
|
});
|
2019-09-03 07:22:14 +00:00
|
|
|
|
2019-11-28 17:38:37 +00:00
|
|
|
let url;
|
|
|
|
if (process.env.NODE_ENV === 'development') {
|
|
|
|
url = `http://localhost:${process.env.PORT}/static/print.html`;
|
|
|
|
} else {
|
|
|
|
url = `file://${__dirname}/static/print.html`;
|
|
|
|
}
|
2019-09-03 07:22:14 +00:00
|
|
|
|
2019-11-28 17:38:37 +00:00
|
|
|
printWindow.loadURL(url);
|
2018-10-24 17:52:02 +00:00
|
|
|
|
2019-08-14 09:39:55 +00:00
|
|
|
printWindow.on('closed', () => {
|
|
|
|
printWindow = null;
|
|
|
|
});
|
2018-10-24 17:52:02 +00:00
|
|
|
|
2019-08-14 09:39:55 +00:00
|
|
|
const code = `
|
2019-11-28 17:38:37 +00:00
|
|
|
let el = document.querySelector('.printTarget');
|
|
|
|
document.body.innerHTML = \`${html}\`;
|
|
|
|
`;
|
2018-10-24 17:52:02 +00:00
|
|
|
|
2019-08-14 09:39:55 +00:00
|
|
|
printWindow.webContents.executeJavaScript(code);
|
2018-10-24 17:52:02 +00:00
|
|
|
|
2019-08-14 09:39:55 +00:00
|
|
|
const printPromise = new Promise(resolve => {
|
|
|
|
printWindow.webContents.on('did-finish-load', () => {
|
|
|
|
printWindow.webContents.printToPDF(
|
|
|
|
{
|
2019-01-12 10:42:30 +00:00
|
|
|
marginsType: 1, // no margin
|
|
|
|
pageSize: 'A4',
|
|
|
|
printBackground: true
|
2019-08-14 09:39:55 +00:00
|
|
|
},
|
|
|
|
(error, data) => {
|
|
|
|
if (error) throw error;
|
2018-10-24 18:12:40 +00:00
|
|
|
printWindow.close();
|
2019-11-28 17:38:37 +00:00
|
|
|
fs.writeFile(destination, data, error => {
|
2019-08-14 09:39:55 +00:00
|
|
|
if (error) throw error;
|
2019-11-28 17:38:37 +00:00
|
|
|
resolve(shell.openItem(destination));
|
2019-08-14 09:39:55 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2018-10-24 18:12:40 +00:00
|
|
|
|
2019-08-14 09:39:55 +00:00
|
|
|
await printPromise;
|
2018-04-16 09:32:31 +00:00
|
|
|
}
|
|
|
|
|
2018-04-14 19:14:02 +00:00
|
|
|
function setupExpressRoute() {
|
2019-08-14 09:39:55 +00:00
|
|
|
if (!frappe.app) return;
|
|
|
|
frappe.app.post('/api/method/pdf', frappe.asyncHandler(handlePDFRequest));
|
2018-04-14 19:14:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function handlePDFRequest(req, res) {
|
2019-08-14 09:39:55 +00:00
|
|
|
const args = req.body;
|
|
|
|
const { doctype, name } = args;
|
|
|
|
const html = await getHTML(doctype, name);
|
2018-04-14 19:14:02 +00:00
|
|
|
|
2019-08-14 09:39:55 +00:00
|
|
|
const filepath = path.join(
|
|
|
|
getTmpDir(),
|
|
|
|
`frappe-pdf-${getRandomString()}.pdf`
|
|
|
|
);
|
|
|
|
await makePDF(html, filepath);
|
2018-04-14 19:14:02 +00:00
|
|
|
|
2019-08-14 09:39:55 +00:00
|
|
|
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);
|
2018-04-14 19:14:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2019-08-14 09:39:55 +00:00
|
|
|
makePDF,
|
|
|
|
setupExpressRoute,
|
|
|
|
getPDFForElectron
|
|
|
|
};
|