@ejfdelgado/ejflab-back 1.25.2 → 1.26.1
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 +34 -8
package/package.json
CHANGED
package/srv/MyPdf.mjs
CHANGED
@@ -1,10 +1,26 @@
|
|
1
1
|
import fs from "fs";
|
2
2
|
import puppeteer from 'puppeteer';
|
3
3
|
import { General } from "./common/General.mjs";
|
4
|
+
import { MyTemplate } from "@ejfdelgado/ejflab-common/src/MyTemplate.js";
|
4
5
|
|
5
6
|
export class MyPdf {
|
6
|
-
static async localRender(template) {
|
7
|
+
static async localRender(template, model = {}, format = "pdf", extra = []) {
|
7
8
|
const source = fs.readFileSync(`./src/assets/templates/pdf/${template}`, { encoding: "utf8" });
|
9
|
+
|
10
|
+
for (let i = 0; i < extra.length; i++) {
|
11
|
+
const extraFile = extra[i];
|
12
|
+
const text = fs.readFileSync(`./src/assets/templates/pdf/${extraFile.path}`, { encoding: "utf8" });
|
13
|
+
if (!model.extra) {
|
14
|
+
model.extra = {};
|
15
|
+
model.extra[extraFile.alias];
|
16
|
+
}
|
17
|
+
}
|
18
|
+
|
19
|
+
const renderer = new MyTemplate();
|
20
|
+
const rendered = renderer.render(source, model);
|
21
|
+
if (format == "html") {
|
22
|
+
return rendered;
|
23
|
+
}
|
8
24
|
const browser = await puppeteer.launch({
|
9
25
|
headless: 'new',
|
10
26
|
executablePath: '/usr/bin/google-chrome',
|
@@ -14,7 +30,7 @@ export class MyPdf {
|
|
14
30
|
]
|
15
31
|
});
|
16
32
|
const page = await browser.newPage();
|
17
|
-
await page.setContent(
|
33
|
+
await page.setContent(rendered);
|
18
34
|
await page.emulateMediaType('print');
|
19
35
|
const pdf = await page.pdf({
|
20
36
|
margin: { top: '100px', right: '50px', bottom: '100px', left: '50px' },
|
@@ -27,11 +43,21 @@ export class MyPdf {
|
|
27
43
|
|
28
44
|
static async render(req, res, next) {
|
29
45
|
const template = General.readParam(req, "template");
|
30
|
-
const
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
46
|
+
const format = General.readParam(req, "format", "pdf");
|
47
|
+
const body = req.body;
|
48
|
+
const rendered = await MyPdf.localRender(template, body, format);
|
49
|
+
if (format == "html") {
|
50
|
+
res.writeHead(200, {
|
51
|
+
"Content-Type": "text/html",
|
52
|
+
"Content-disposition": "inline"
|
53
|
+
});
|
54
|
+
res.end(rendered);
|
55
|
+
} else {
|
56
|
+
res.writeHead(200, {
|
57
|
+
"Content-Type": "application/pdf",
|
58
|
+
"Content-disposition": "inline"
|
59
|
+
});
|
60
|
+
res.end(rendered);
|
61
|
+
}
|
36
62
|
}
|
37
63
|
}
|