@ejfdelgado/ejflab-back 1.26.5 → 1.26.7
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.
- package/package.json +1 -1
- package/srv/MyPdf.mjs +47 -16
package/package.json
CHANGED
package/srv/MyPdf.mjs
CHANGED
@@ -4,15 +4,21 @@ import { General } from "./common/General.mjs";
|
|
4
4
|
import { MyTemplate } from "@ejfdelgado/ejflab-common/src/MyTemplate.js";
|
5
5
|
import { CsvFormatterFilters } from "@ejfdelgado/ejflab-common/src/CsvFormatterFilters.js";
|
6
6
|
import { MyUtilities } from '@ejfdelgado/ejflab-common/src/MyUtilities.js';
|
7
|
+
import { MyDatesBack } from '@ejfdelgado/ejflab-common/src/MyDatesBack.mjs';
|
7
8
|
|
8
9
|
export class MyPdf {
|
9
|
-
static async localRender(template, model = {}, format = "pdf", extra = [], configuration) {
|
10
|
+
static async localRender(template, model = {}, format = "pdf", extra = [], configuration, launch) {
|
10
11
|
const source = fs.readFileSync(`./src/assets/templates/pdf/${template}`, { encoding: "utf8" });
|
12
|
+
if (!model.extra) {
|
13
|
+
model.extra = {};
|
14
|
+
}
|
15
|
+
const renderer = new MyTemplate();
|
16
|
+
renderer.registerFunction("json", CsvFormatterFilters.json);
|
17
|
+
renderer.registerFunction("epoch2date", (value, ...args) => {
|
18
|
+
return MyDatesBack.formatDateCompleto(new Date(value), ...args);
|
19
|
+
});
|
11
20
|
for (let i = 0; i < extra.length; i++) {
|
12
21
|
const extraFile = extra[i];
|
13
|
-
if (!model.extra) {
|
14
|
-
model.extra = {};
|
15
|
-
}
|
16
22
|
const path = MyUtilities.removeRepeatedSlash(`./src/assets/templates/pdf/${extraFile.path}`);
|
17
23
|
const exists = fs.existsSync(path);
|
18
24
|
if (exists) {
|
@@ -21,28 +27,29 @@ export class MyPdf {
|
|
21
27
|
const base64String = data.toString('base64');
|
22
28
|
model.extra[extraFile.alias] = "base64," + base64String;
|
23
29
|
} else {
|
30
|
+
// It is used as text
|
24
31
|
const text = fs.readFileSync(path, { encoding: "utf8" });
|
25
|
-
model.extra[extraFile.alias] = text;
|
32
|
+
model.extra[extraFile.alias] = renderer.render(text, model);
|
26
33
|
}
|
27
34
|
} else {
|
28
35
|
model.extra[extraFile.alias] = "";
|
29
36
|
}
|
30
37
|
}
|
31
|
-
|
32
|
-
const renderer = new MyTemplate();
|
33
|
-
renderer.registerFunction("json", CsvFormatterFilters.json);
|
34
38
|
const rendered = renderer.render(source, model);
|
35
39
|
if (format == "html") {
|
36
40
|
return rendered;
|
37
41
|
}
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
42
|
+
if (!launch) {
|
43
|
+
launch = {
|
44
|
+
headless: 'new',
|
45
|
+
executablePath: '/usr/bin/google-chrome',
|
46
|
+
args: [
|
47
|
+
"--no-sandbox",
|
48
|
+
"--disable-gpu",
|
49
|
+
]
|
50
|
+
};
|
51
|
+
}
|
52
|
+
const browser = await puppeteer.launch(launch);
|
46
53
|
if (!configuration) {
|
47
54
|
configuration = {
|
48
55
|
margin: { top: '100px', right: '50px', bottom: '100px', left: '50px' },
|
@@ -50,6 +57,30 @@ export class MyPdf {
|
|
50
57
|
format: 'letter',
|
51
58
|
}
|
52
59
|
}
|
60
|
+
|
61
|
+
let itHasHeaderOrFooter = false;
|
62
|
+
if (!configuration.headerTemplate) {
|
63
|
+
if (model.extra.header_html) {
|
64
|
+
configuration.headerTemplate = model.extra.header_html;
|
65
|
+
itHasHeaderOrFooter = true;
|
66
|
+
}
|
67
|
+
}
|
68
|
+
if (!configuration.footerTemplate) {
|
69
|
+
if (model.extra.footer_html) {
|
70
|
+
configuration.footerTemplate = model.extra.footer_html;
|
71
|
+
itHasHeaderOrFooter = true;
|
72
|
+
}
|
73
|
+
}
|
74
|
+
configuration.displayHeaderFooter = itHasHeaderOrFooter;
|
75
|
+
if (itHasHeaderOrFooter) {
|
76
|
+
if (!configuration.footerTemplate) {
|
77
|
+
configuration.footerTemplate = '';
|
78
|
+
}
|
79
|
+
if (!configuration.headerTemplate) {
|
80
|
+
configuration.headerTemplate = '';
|
81
|
+
}
|
82
|
+
}
|
83
|
+
|
53
84
|
const page = await browser.newPage();
|
54
85
|
await page.setContent(rendered);
|
55
86
|
await page.emulateMediaType('print');
|