@ejfdelgado/ejflab-back 1.25.2 → 1.26.0

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 +24 -8
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ejfdelgado/ejflab-back",
3
- "version": "1.25.2",
3
+ "version": "1.26.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/ejfdelgado/ejflab-back.git"
package/srv/MyPdf.mjs CHANGED
@@ -1,10 +1,16 @@
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") {
7
8
  const source = fs.readFileSync(`./src/assets/templates/pdf/${template}`, { encoding: "utf8" });
9
+ const renderer = new MyTemplate();
10
+ const rendered = renderer.render(source, model);
11
+ if (format == "html") {
12
+ return rendered;
13
+ }
8
14
  const browser = await puppeteer.launch({
9
15
  headless: 'new',
10
16
  executablePath: '/usr/bin/google-chrome',
@@ -14,7 +20,7 @@ export class MyPdf {
14
20
  ]
15
21
  });
16
22
  const page = await browser.newPage();
17
- await page.setContent(source);
23
+ await page.setContent(rendered);
18
24
  await page.emulateMediaType('print');
19
25
  const pdf = await page.pdf({
20
26
  margin: { top: '100px', right: '50px', bottom: '100px', left: '50px' },
@@ -27,11 +33,21 @@ export class MyPdf {
27
33
 
28
34
  static async render(req, res, next) {
29
35
  const template = General.readParam(req, "template");
30
- const pdf = await MyPdf.localRender(template);
31
- res.writeHead(200, {
32
- "Content-Type": "application/pdf",
33
- "Content-disposition": "inline"
34
- });
35
- res.end(pdf);
36
+ const format = General.readParam(req, "format", "pdf");
37
+ const body = req.body;
38
+ const rendered = await MyPdf.localRender(template, body, format);
39
+ if (format == "html") {
40
+ res.writeHead(200, {
41
+ "Content-Type": "text/html",
42
+ "Content-disposition": "inline"
43
+ });
44
+ res.end(rendered);
45
+ } else {
46
+ res.writeHead(200, {
47
+ "Content-Type": "application/pdf",
48
+ "Content-disposition": "inline"
49
+ });
50
+ res.end(rendered);
51
+ }
36
52
  }
37
53
  }