Skip to content

Commit 28a98fb

Browse files
committed
removed jsreport from reportsV2
1 parent bc1c1a0 commit 28a98fb

File tree

2 files changed

+11
-62
lines changed

2 files changed

+11
-62
lines changed

src/reports/report.generator.ts

Lines changed: 9 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
import * as nunjucks from "nunjucks"
2-
import puppeteer from "puppeteer"
3-
import * as XLSX from 'xlsx';
4-
import {JSDOM} from "jsdom"
5-
import * as numeral from "numeral";
6-
7-
1+
import axios from "axios";
82

93
export class ReportRenderParams{
104
template: string
@@ -14,61 +8,16 @@ export class ReportRenderParams{
148
}
159

1610
export class ReportGenerator {
17-
constructor() {
18-
this.nj = nunjucks.configure({ autoescape: true });
19-
20-
this.nj.addFilter('numberFormat', function(val, format) {
21-
return numeral(val).format(format ? format : `0.00`);
22-
});
23-
24-
this.nj.addFilter('shorten', function(str, count) {
25-
return str.slice(0, count || 5);
26-
});
27-
}
28-
29-
private browser = null
30-
private nj = null
11+
constructor() {}
3112

3213
async render(params: ReportRenderParams) {
33-
console.log(params.template)
34-
let html = await this.nj.renderString(params.template, params.data);
35-
36-
37-
38-
if (params.output === 'pdf') {
39-
return await this.toPdf(html, params.settings)
40-
} if (params.output === 'xlsx') {
41-
return await this.toXlsx(html)
42-
} else {
43-
throw `The output type ${params.output} is not implemented yet!`
44-
}
45-
}
46-
47-
private async toPdf(html: string, settings: any) {
48-
if(!this.browser) {
49-
this.browser = await puppeteer.launch();
14+
try {
15+
let res = await axios.post(process.env.REPORTS_SERVICE_URL + '/v1/render', params)
16+
return res.data
17+
} catch (e) {
18+
console.error(e)
19+
let err = e.response?.data?.message || 'unknown'
20+
throw "Error while rendering report: " + err
5021
}
51-
52-
const page = await this.browser.newPage();
53-
54-
await page.setContent(html, {
55-
waitUntil: ["load","networkidle0"]
56-
});
57-
58-
let data = await page.pdf(settings);
59-
60-
await page.close()
61-
62-
return Buffer.from(data)
63-
}
64-
65-
private async toXlsx(html) : Promise<ArrayBuffer> {
66-
const doc = new JSDOM(html).window.document.querySelector("table");
67-
const workbook = XLSX.utils.table_to_book(doc);
68-
69-
return XLSX.write(workbook, {
70-
type: 'buffer',
71-
bookType: 'xlsx',
72-
})
7322
}
7423
}

src/reports/reports.contreller.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ export class ReportsController {
195195
return {
196196
statusCode: 200,
197197
contentType: rep.contentType,
198-
report: rep.data.toString('base64'),
198+
report: rep.data,
199199
filename: rep.filename,
200200
renderingTime: rep.rendering,
201201
preparingTime: rep.preparing
@@ -226,7 +226,7 @@ export class ReportsController {
226226
return {
227227
statusCode: 200,
228228
contentType: rep.contentType,
229-
report: rep.data.toString('base64'),
229+
report: rep.data,
230230
filename: rep.filename,
231231
renderingTime: rep.rendering,
232232
preparingTime: rep.preparing

0 commit comments

Comments
 (0)