feat(salix): refs #7563 #7563 add new paramt with format value

This commit is contained in:
Javier Segarra 2024-06-11 13:06:09 +02:00
parent c241189508
commit bb96125bbd
3 changed files with 24 additions and 9 deletions

View File

@ -15,7 +15,8 @@ class Report {
*/
show(path, params) {
params = Object.assign({
access_token: this.vnToken.tokenMultimedia
access_token: this.vnToken.tokenMultimedia,
format: 'pdf'
}, params);
const serializedParams = this.$httpParamSerializer(params);
const query = serializedParams ? `?${serializedParams}` : '';

View File

@ -1,21 +1,25 @@
const {Report, Email} = require('vn-print');
const TYPES = {
'html': 'text/html',
'pdf': 'application/pdf'
};
module.exports = Self => {
Self.printReport = async function(ctx, id, reportName) {
const args = Object.assign({}, ctx.args);
const params = {lang: ctx.req.getLocale()};
const params = {lang: ctx.req.getLocale(), format: ctx.req.headers.accept.split(',')[0]};
const fileExtension = ctx.req.query.format ?? params?.format.split('/')[1];
delete args.ctx;
for (const param in args)
params[param] = args[param];
const report = new Report(reportName, params);
const stream = await report.toPdfStream();
const stream = await report.toStream(fileExtension);
let fileName = `${reportName}`;
if (id) fileName += `-${id}`;
return [stream, 'application/pdf', `filename="${fileName}.pdf"`];
const type = TYPES[fileExtension];
return [stream, type, `filename="${fileName}.${fileExtension}"`];
};
Self.printEmail = async function(ctx, id, templateName) {

View File

@ -22,7 +22,7 @@ class Report extends Component {
return (await this.getUserLocale())['reportName'];
}
async toPdfStream() {
async toStream(type) {
const template = await this.render();
const defaultOptions = Object.assign({}, config.pdf);
@ -51,9 +51,15 @@ class Report extends Component {
options.headerTemplate = '\n';
options.footerTemplate = footer;
switch (type) {
case 'html':
resolve(await page.content());
break;
const buffer = await page.pdf(options);
resolve(buffer);
default:
resolve(await page.pdf(options));
break;
}
} catch (err) {
reject(err);
}
@ -61,6 +67,10 @@ class Report extends Component {
});
}
async toPdfStream() {
await this.toStream('pdf');
}
/**
* Returns all the params that ends with id
*