parent
c241189508
commit
bb96125bbd
|
@ -15,7 +15,8 @@ class Report {
|
||||||
*/
|
*/
|
||||||
show(path, params) {
|
show(path, params) {
|
||||||
params = Object.assign({
|
params = Object.assign({
|
||||||
access_token: this.vnToken.tokenMultimedia
|
access_token: this.vnToken.tokenMultimedia,
|
||||||
|
format: 'pdf'
|
||||||
}, params);
|
}, params);
|
||||||
const serializedParams = this.$httpParamSerializer(params);
|
const serializedParams = this.$httpParamSerializer(params);
|
||||||
const query = serializedParams ? `?${serializedParams}` : '';
|
const query = serializedParams ? `?${serializedParams}` : '';
|
||||||
|
|
|
@ -1,21 +1,25 @@
|
||||||
const {Report, Email} = require('vn-print');
|
const {Report, Email} = require('vn-print');
|
||||||
|
const TYPES = {
|
||||||
|
'html': 'text/html',
|
||||||
|
'pdf': 'application/pdf'
|
||||||
|
};
|
||||||
module.exports = Self => {
|
module.exports = Self => {
|
||||||
Self.printReport = async function(ctx, id, reportName) {
|
Self.printReport = async function(ctx, id, reportName) {
|
||||||
const args = Object.assign({}, ctx.args);
|
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;
|
delete args.ctx;
|
||||||
for (const param in args)
|
for (const param in args)
|
||||||
params[param] = args[param];
|
params[param] = args[param];
|
||||||
|
|
||||||
const report = new Report(reportName, params);
|
const report = new Report(reportName, params);
|
||||||
const stream = await report.toPdfStream();
|
const stream = await report.toStream(fileExtension);
|
||||||
|
|
||||||
let fileName = `${reportName}`;
|
let fileName = `${reportName}`;
|
||||||
if (id) fileName += `-${id}`;
|
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) {
|
Self.printEmail = async function(ctx, id, templateName) {
|
||||||
|
|
|
@ -22,7 +22,7 @@ class Report extends Component {
|
||||||
return (await this.getUserLocale())['reportName'];
|
return (await this.getUserLocale())['reportName'];
|
||||||
}
|
}
|
||||||
|
|
||||||
async toPdfStream() {
|
async toStream(type) {
|
||||||
const template = await this.render();
|
const template = await this.render();
|
||||||
const defaultOptions = Object.assign({}, config.pdf);
|
const defaultOptions = Object.assign({}, config.pdf);
|
||||||
|
|
||||||
|
@ -51,9 +51,15 @@ class Report extends Component {
|
||||||
|
|
||||||
options.headerTemplate = '\n';
|
options.headerTemplate = '\n';
|
||||||
options.footerTemplate = footer;
|
options.footerTemplate = footer;
|
||||||
|
switch (type) {
|
||||||
|
case 'html':
|
||||||
|
resolve(await page.content());
|
||||||
|
break;
|
||||||
|
|
||||||
const buffer = await page.pdf(options);
|
default:
|
||||||
resolve(buffer);
|
resolve(await page.pdf(options));
|
||||||
|
break;
|
||||||
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
reject(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
|
* Returns all the params that ends with id
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in New Issue