@ejfdelgado/ejflab-back 1.26.5 → 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 +43 -16
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ejfdelgado/ejflab-back",
3
- "version": "1.26.5",
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 = [], configuration) {
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,28 +23,29 @@ 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);
46
49
  if (!configuration) {
47
50
  configuration = {
48
51
  margin: { top: '100px', right: '50px', bottom: '100px', left: '50px' },
@@ -50,6 +53,30 @@ export class MyPdf {
50
53
  format: 'letter',
51
54
  }
52
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
+
53
80
  const page = await browser.newPage();
54
81
  await page.setContent(rendered);
55
82
  await page.emulateMediaType('print');