@ejfdelgado/ejflab-back 1.26.4 → 1.26.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/srv/MyPdf.mjs +54 -23
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ejfdelgado/ejflab-back",
3
- "version": "1.26.4",
3
+ "version": "1.26.6",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/ejfdelgado/ejflab-back.git"
package/srv/MyPdf.mjs CHANGED
@@ -6,13 +6,15 @@ import { CsvFormatterFilters } from "@ejfdelgado/ejflab-common/src/CsvFormatterF
6
6
  import { MyUtilities } from '@ejfdelgado/ejflab-common/src/MyUtilities.js';
7
7
 
8
8
  export class MyPdf {
9
- static async localRender(template, model = {}, format = "pdf", extra = []) {
9
+ static async localRender(template, model = {}, format = "pdf", extra = [], configuration, launch) {
10
10
  const source = fs.readFileSync(`./src/assets/templates/pdf/${template}`, { encoding: "utf8" });
11
+ if (!model.extra) {
12
+ model.extra = {};
13
+ }
14
+ const renderer = new MyTemplate();
15
+ renderer.registerFunction("json", CsvFormatterFilters.json);
11
16
  for (let i = 0; i < extra.length; i++) {
12
17
  const extraFile = extra[i];
13
- if (!model.extra) {
14
- model.extra = {};
15
- }
16
18
  const path = MyUtilities.removeRepeatedSlash(`./src/assets/templates/pdf/${extraFile.path}`);
17
19
  const exists = fs.existsSync(path);
18
20
  if (exists) {
@@ -21,36 +23,64 @@ export class MyPdf {
21
23
  const base64String = data.toString('base64');
22
24
  model.extra[extraFile.alias] = "base64," + base64String;
23
25
  } else {
26
+ // It is used as text
24
27
  const text = fs.readFileSync(path, { encoding: "utf8" });
25
- model.extra[extraFile.alias] = text;
28
+ model.extra[extraFile.alias] = renderer.render(text, model);
26
29
  }
27
30
  } else {
28
31
  model.extra[extraFile.alias] = "";
29
32
  }
30
33
  }
31
-
32
- const renderer = new MyTemplate();
33
- renderer.registerFunction("json", CsvFormatterFilters.json);
34
34
  const rendered = renderer.render(source, model);
35
35
  if (format == "html") {
36
36
  return rendered;
37
37
  }
38
- const browser = await puppeteer.launch({
39
- headless: 'new',
40
- executablePath: '/usr/bin/google-chrome',
41
- args: [
42
- "--no-sandbox",
43
- "--disable-gpu",
44
- ]
45
- });
38
+ if (!launch) {
39
+ launch = {
40
+ headless: 'new',
41
+ executablePath: '/usr/bin/google-chrome',
42
+ args: [
43
+ "--no-sandbox",
44
+ "--disable-gpu",
45
+ ]
46
+ };
47
+ }
48
+ const browser = await puppeteer.launch(launch);
49
+ if (!configuration) {
50
+ configuration = {
51
+ margin: { top: '100px', right: '50px', bottom: '100px', left: '50px' },
52
+ printBackground: true,
53
+ format: 'letter',
54
+ }
55
+ }
56
+
57
+ let itHasHeaderOrFooter = false;
58
+ if (!configuration.headerTemplate) {
59
+ if (model.extra.header_html) {
60
+ configuration.headerTemplate = model.extra.header_html;
61
+ itHasHeaderOrFooter = true;
62
+ }
63
+ }
64
+ if (!configuration.footerTemplate) {
65
+ if (model.extra.footer_html) {
66
+ configuration.footerTemplate = model.extra.footer_html;
67
+ itHasHeaderOrFooter = true;
68
+ }
69
+ }
70
+ configuration.displayHeaderFooter = itHasHeaderOrFooter;
71
+ if (itHasHeaderOrFooter) {
72
+ if (!configuration.footerTemplate) {
73
+ configuration.footerTemplate = '';
74
+ }
75
+ if (!configuration.headerTemplate) {
76
+ configuration.headerTemplate = '';
77
+ }
78
+ }
79
+
46
80
  const page = await browser.newPage();
47
81
  await page.setContent(rendered);
48
82
  await page.emulateMediaType('print');
49
- const pdf = await page.pdf({
50
- margin: { top: '100px', right: '50px', bottom: '100px', left: '50px' },
51
- printBackground: true,
52
- format: 'letter',
53
- });
83
+ const pdf = await page.pdf(configuration);
54
84
  await browser.close();
55
85
  return pdf;
56
86
  }
@@ -58,18 +88,19 @@ export class MyPdf {
58
88
  static async render(req, res, next) {
59
89
  const template = General.readParam(req, "template");
60
90
  const format = General.readParam(req, "format", "pdf");
91
+ const fileName = General.readParam(req, "format", "fileName");
61
92
  const body = req.body;
62
93
  const rendered = await MyPdf.localRender(template, body, format);
63
94
  if (format == "html") {
64
95
  res.writeHead(200, {
65
96
  "Content-Type": "text/html",
66
- "Content-disposition": "inline"
97
+ "Content-disposition": `inline; filename="${fileName}.html"`
67
98
  });
68
99
  res.end(rendered);
69
100
  } else {
70
101
  res.writeHead(200, {
71
102
  "Content-Type": "application/pdf",
72
- "Content-disposition": "inline"
103
+ "Content-disposition": `inline; filename="${fileName}.pdf"`
73
104
  });
74
105
  res.end(rendered);
75
106
  }