Compare commits
1 Commits
dev
...
7563_repor
Author | SHA1 | Date |
---|---|---|
Javier Segarra | bb96125bbd |
|
@ -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}` : '';
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue