Skip to content

Commit c8c2fdb

Browse files
committed
[reports] puppeteer CORS fix for localhost
1 parent c29945a commit c8c2fdb

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

api/utils/pdf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ exports.renderPDF = async function(html, callback, options = null, puppeteerArgs
4242
}
4343
const updatedTimeout = 240000;
4444
const page = await browser.newPage();
45-
45+
await page.setBypassCSP(true);
4646
page.on('console', (msg) => {
4747
log.d("Headless chrome page log", msg.text());
4848
});

plugins/reports/api/api.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ var common = require('../../../api/utils/common.js'),
44
moment = require('moment-timezone'),
55
log = require('../../../api/utils/log')('reports:api'),
66
ejs = require("ejs"),
7+
fs = require("fs"),
78
plugins = require('../../pluginManager.js'),
9+
pdf = require('../../../api/utils/pdf'),
810
{ validateCreate, validateRead, validateUpdate, validateDelete, getUserApps, } = require('../../../api/utils/rights.js');
911

1012
const FEATURE_NAME = 'reports';
@@ -428,6 +430,69 @@ const FEATURE_NAME = 'reports';
428430
});
429431
});
430432
break;
433+
case 'pdf':
434+
validateRead(paramsInstance, FEATURE_NAME, function() {
435+
var params = paramsInstance;
436+
var argProps = {
437+
'_id': { 'required': true, 'type': 'String'}
438+
},
439+
id = '';
440+
441+
if (!(id = common.validateArgs(params.qstring.args, argProps)._id)) {
442+
common.returnMessage(params, 200, 'Not enough args');
443+
return false;
444+
}
445+
common.db.collection('reports').findOne(recordUpdateOrDeleteQuery(params, id), function(err, result) {
446+
if (err || !result) {
447+
common.returnMessage(params, 200, 'Report not found');
448+
return false;
449+
}
450+
451+
// TODO: Handle report type check
452+
453+
reports.getReport(common.db, result, function(err2, res) {
454+
if (err2) {
455+
common.returnMessage(params, 200, err2);
456+
}
457+
else {
458+
if (params && params.res) {
459+
var html = res.message;
460+
if (result.report_type !== "core") {
461+
html = ejs.render(res.message.template, res.message.data);
462+
}
463+
const filePath = '/tmp/email_report_' + new Date().getTime() + '.pdf';
464+
const options = { "path": filePath, "width": "1028px", height: "1000px" };
465+
pdf.renderPDF(html, function() {
466+
//output created file to browser
467+
fs.readFile(filePath, function(err3, data) {
468+
if (err3) {
469+
console.log(err3);
470+
common.returnMessage(params, 500, 'Cannot read pdf file');
471+
}
472+
else {
473+
common.returnRaw(params, 200, data, {
474+
'Content-Type': 'application/pdf',
475+
'Content-Disposition': 'inline; filename="report.pdf"',
476+
'Content-Length': data.length,
477+
'Access-Control-Allow-Origin': '*'
478+
});
479+
}
480+
fs.unlink(filePath, function(unlinkErr) {
481+
if (unlinkErr) {
482+
console.log("Cannot remove temp pdf file");
483+
console.log(unlinkErr);
484+
}
485+
});
486+
});
487+
}, options, {
488+
args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-web-security'],
489+
}, true);
490+
}
491+
}
492+
});
493+
});
494+
});
495+
break;
431496
case 'status':
432497
validateUpdate(paramsInstance, FEATURE_NAME, function() {
433498
var params = paramsInstance;

plugins/reports/api/reports.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ var metricProps = {
708708
mail.sendMail(msg, deletePDFCallback);
709709
}
710710
}, options, {
711-
args: ['--no-sandbox', '--disable-setuid-sandbox'],
711+
args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-web-security'],
712712
}, true).catch(err => {
713713
log.d(err);
714714
});

0 commit comments

Comments
 (0)