@ejfdelgado/ejflab-back 1.26.3 → 1.26.5

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 +13 -9
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ejfdelgado/ejflab-back",
3
- "version": "1.26.3",
3
+ "version": "1.26.5",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/ejfdelgado/ejflab-back.git"
package/srv/MyPdf.mjs CHANGED
@@ -6,7 +6,7 @@ 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) {
10
10
  const source = fs.readFileSync(`./src/assets/templates/pdf/${template}`, { encoding: "utf8" });
11
11
  for (let i = 0; i < extra.length; i++) {
12
12
  const extraFile = extra[i];
@@ -19,7 +19,7 @@ export class MyPdf {
19
19
  if (extraFile.base64) {
20
20
  const data = fs.readFileSync(path);
21
21
  const base64String = data.toString('base64');
22
- model.extra[extraFile.alias] = base64String;
22
+ model.extra[extraFile.alias] = "base64," + base64String;
23
23
  } else {
24
24
  const text = fs.readFileSync(path, { encoding: "utf8" });
25
25
  model.extra[extraFile.alias] = text;
@@ -43,14 +43,17 @@ export class MyPdf {
43
43
  "--disable-gpu",
44
44
  ]
45
45
  });
46
+ if (!configuration) {
47
+ configuration = {
48
+ margin: { top: '100px', right: '50px', bottom: '100px', left: '50px' },
49
+ printBackground: true,
50
+ format: 'letter',
51
+ }
52
+ }
46
53
  const page = await browser.newPage();
47
54
  await page.setContent(rendered);
48
55
  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
- });
56
+ const pdf = await page.pdf(configuration);
54
57
  await browser.close();
55
58
  return pdf;
56
59
  }
@@ -58,18 +61,19 @@ export class MyPdf {
58
61
  static async render(req, res, next) {
59
62
  const template = General.readParam(req, "template");
60
63
  const format = General.readParam(req, "format", "pdf");
64
+ const fileName = General.readParam(req, "format", "fileName");
61
65
  const body = req.body;
62
66
  const rendered = await MyPdf.localRender(template, body, format);
63
67
  if (format == "html") {
64
68
  res.writeHead(200, {
65
69
  "Content-Type": "text/html",
66
- "Content-disposition": "inline"
70
+ "Content-disposition": `inline; filename="${fileName}.html"`
67
71
  });
68
72
  res.end(rendered);
69
73
  } else {
70
74
  res.writeHead(200, {
71
75
  "Content-Type": "application/pdf",
72
- "Content-disposition": "inline"
76
+ "Content-disposition": `inline; filename="${fileName}.pdf"`
73
77
  });
74
78
  res.end(rendered);
75
79
  }