pim-import 4.1.0 → 4.2.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.
@@ -7,7 +7,51 @@ exports.generatePDFByUrl = void 0;
7
7
  const puppeteer_core_1 = __importDefault(require("puppeteer-core"));
8
8
  const utils_1 = require("../utils");
9
9
  const logs_1 = require("./logs");
10
+ const axios_1 = __importDefault(require("@atoms-studio/axios"));
10
11
  const generatePDFByUrl = async (url, path = "", contents) => {
12
+ logs_1.log(`generatePDFByUrl - url: ${url} path: ${path}`);
13
+ if (!process.env.BROWSERLESS_TOKEN) {
14
+ logs_1.log(`BROWSERLESS_TOKEN environment variable not found`, "ERROR");
15
+ }
16
+ let pdf;
17
+ if (path) {
18
+ pdf = await generatePDFByUrlWithPuppeteer(url, path, contents);
19
+ }
20
+ else {
21
+ pdf = await generatePDFByUrlWithAxios(url, contents);
22
+ }
23
+ return pdf;
24
+ };
25
+ exports.generatePDFByUrl = generatePDFByUrl;
26
+ const getPDFOptions = (contents) => {
27
+ const pageMargin = "33px";
28
+ const headerTemplate = ` `;
29
+ const footerTopLeft = contents?.footer?.topLeft || ``;
30
+ const footerTopRight = contents?.footer?.topRight || ``;
31
+ const date = new Date(new Date().toLocaleString("en-US", { timeZone: "Europe/Rome" }));
32
+ const footerBottomLeft = contents?.footer?.bottomLeft ||
33
+ `©2022 Flos - P.IVA 00290820174 - ${date.getMonth() + 1}/${date.getDate()}/${date.getFullYear()}`;
34
+ const footerMail = contents?.footer?.mail || "info@flos.com";
35
+ const footerBottomRight = contents?.footer?.bottomRight ||
36
+ `<a style="text-decoration: none; color:#000" href="https://professional.flos.com/en/global/">professional.flos.com</a> | <a style="text-decoration: none; color:#000" href="mailto:${footerMail}">${footerMail}</a> <span class="pageNumber" style="margin-left:4px"></span>/<span class="totalPages"></span>`;
37
+ const footerBottom = "In our constantly evolving world and business, technical upgrades happen every day. This means all product specifications and data are subject to change without warning in order t improve reliability, function, performance or otherwise. We make every effort to ensure the accuracy of our product images, however due to different lighting and screens used for viewing, the colours may vary. Images are indicative of the quality and style of the product but may not represent the precise details of the product you receive. This is because we are constantly working to make improvements. For aesthetic reasons, cables and/or electrical elements are often not shown. Refer to technical data sheets for all technical data. Images and colours are not part of any contract or warranty in any way.";
38
+ const footerStyle = `<style>.pdf-footer { font-family: Arial, Helvetica, sans-serif; font-weight: 300; }</style>`;
39
+ const footerTemplate = `${footerStyle}<div class="pdf-footer" style="margin: 0 ${pageMargin}; padding: 10px 0; width: 100%; font-size: 5pt; font-weight:300;"> <div class="pdf-row" style="display: flex; flex-wrap: wrap; border-bottom: 1px solid rgb(168, 168, 168); border-bottom-style:solid; padding-bottom: 6px; justify-content: center; align-items: center;"> <span class="left" style="margin:0; padding: 0; width: 80%; white-space: normal; overflow: hidden;">${footerTopLeft}</span> <span class="right" style="margin:0; padding: 0; width: 20%; text-align: right;">${footerTopRight}</span> </div> <div class="pdf-row" style="display: flex; flex-wrap: wrap; padding-top: 6px; justify-content: center; align-items: center;"> <span style="width: 50%;">${footerBottomLeft}</span> <span style="width: 50%; text-align: right;"> ${footerBottomRight} </span> </div><div class="pdf-row" style="font-size: 4pt; margin-top:5px;">${footerBottom}</div> </div>`;
40
+ return {
41
+ format: "A4",
42
+ printBackground: true,
43
+ displayHeaderFooter: true,
44
+ headerTemplate,
45
+ footerTemplate,
46
+ margin: {
47
+ top: pageMargin,
48
+ left: pageMargin,
49
+ right: pageMargin,
50
+ bottom: "114px",
51
+ },
52
+ };
53
+ };
54
+ const generatePDFByUrlWithPuppeteer = async (url, path = "", contents) => {
11
55
  logs_1.log(`generatePDFByUrl - url: ${url} path: ${path}`);
12
56
  if (!process.env.BROWSERLESS_TOKEN) {
13
57
  logs_1.log(`BROWSERLESS_TOKEN environment variable not found`, "ERROR");
@@ -42,38 +86,18 @@ const generatePDFByUrl = async (url, path = "", contents) => {
42
86
  logs_1.serverUtils.log(logMsg);
43
87
  logs_1.serverUtils.updateProgress(40);
44
88
  }
89
+ if (!res) {
90
+ await browser.close();
91
+ console.log(`Error no response`);
92
+ return null;
93
+ }
45
94
  if (res.status() !== 200) {
46
95
  await browser.close();
47
96
  console.log(`Error ${res.status()} - ${res.url()}`);
48
97
  return null;
49
98
  }
50
99
  try {
51
- const pageMargin = "33px";
52
- const headerTemplate = ` `;
53
- const footerTopLeft = contents?.footer?.topLeft || ``;
54
- const footerTopRight = contents?.footer?.topRight || ``;
55
- const date = new Date(new Date().toLocaleString("en-US", { timeZone: "Europe/Rome" }));
56
- const footerBottomLeft = contents?.footer?.bottomLeft ||
57
- `©2022 Flos - P.IVA 00290820174 - ${date.getMonth() + 1}/${date.getDate()}/${date.getFullYear()}`;
58
- const footerMail = contents?.footer?.mail || "info@flos.com";
59
- const footerBottomRight = contents?.footer?.bottomRight ||
60
- `<a style="text-decoration: none; color:#000" href="https://professional.flos.com/en/global/">professional.flos.com</a> | <a style="text-decoration: none; color:#000" href="mailto:${footerMail}">${footerMail}</a> <span class="pageNumber" style="margin-left:4px"></span>/<span class="totalPages"></span>`;
61
- const footerBottom = "In our constantly evolving world and business, technical upgrades happen every day. This means all product specifications and data are subject to change without warning in order t improve reliability, function, performance or otherwise. We make every effort to ensure the accuracy of our product images, however due to different lighting and screens used for viewing, the colours may vary. Images are indicative of the quality and style of the product but may not represent the precise details of the product you receive. This is because we are constantly working to make improvements. For aesthetic reasons, cables and/or electrical elements are often not shown. Refer to technical data sheets for all technical data. Images and colours are not part of any contract or warranty in any way.";
62
- const footerStyle = `<style>.pdf-footer { font-family: Arial, Helvetica, sans-serif; font-weight: 300; }</style>`;
63
- const footerTemplate = `${footerStyle}<div class="pdf-footer" style="margin: 0 ${pageMargin}; padding: 10px 0; width: 100%; font-size: 5pt; font-weight:300;"> <div class="pdf-row" style="display: flex; flex-wrap: wrap; border-bottom: 1px solid rgb(168, 168, 168); border-bottom-style:solid; padding-bottom: 6px; justify-content: center; align-items: center;"> <span class="left" style="margin:0; padding: 0; width: 80%; white-space: normal; overflow: hidden;">${footerTopLeft}</span> <span class="right" style="margin:0; padding: 0; width: 20%; text-align: right;">${footerTopRight}</span> </div> <div class="pdf-row" style="display: flex; flex-wrap: wrap; padding-top: 6px; justify-content: center; align-items: center;"> <span style="width: 50%;">${footerBottomLeft}</span> <span style="width: 50%; text-align: right;"> ${footerBottomRight} </span> </div><div class="pdf-row" style="font-size: 4pt; margin-top:5px;">${footerBottom}</div> </div>`;
64
- const options = {
65
- format: "a4",
66
- printBackground: true,
67
- displayHeaderFooter: true,
68
- headerTemplate,
69
- footerTemplate,
70
- margin: {
71
- top: pageMargin,
72
- left: pageMargin,
73
- right: pageMargin,
74
- bottom: "114px",
75
- },
76
- };
100
+ const options = getPDFOptions(contents);
77
101
  if (path) {
78
102
  options.path = path;
79
103
  }
@@ -108,5 +132,33 @@ const generatePDFByUrl = async (url, path = "", contents) => {
108
132
  await browser.close();
109
133
  }
110
134
  };
111
- exports.generatePDFByUrl = generatePDFByUrl;
135
+ const generatePDFByUrlWithAxios = async (url, contents) => {
136
+ logs_1.log(`generatePDFByUrl - url: ${url}`);
137
+ const timeStart = new Date();
138
+ let logMsg;
139
+ if (!process.env.BROWSERLESS_TOKEN) {
140
+ logs_1.log(`BROWSERLESS_TOKEN environment variable not found`, "ERROR");
141
+ }
142
+ logMsg = `Before axios request - ${utils_1.secondBetweenTwoDate(timeStart, new Date())} seconds`;
143
+ logs_1.log(logMsg, "DEBUG");
144
+ const options = {
145
+ method: "POST",
146
+ url: "https://chrome.browserless.io/pdf",
147
+ params: { token: process.env.BROWSERLESS_TOKEN },
148
+ responseType: "arraybuffer",
149
+ headers: {
150
+ "cache-control": "no-cache",
151
+ "content-type": "application/json",
152
+ },
153
+ data: {
154
+ url,
155
+ gotoOptions: { waitUntil: "networkidle0" },
156
+ options: getPDFOptions(contents),
157
+ },
158
+ };
159
+ const response = await axios_1.default.request(options);
160
+ logMsg = `after axios request - ${utils_1.secondBetweenTwoDate(timeStart, new Date())} seconds`;
161
+ logs_1.log(logMsg, "DEBUG");
162
+ return response.data;
163
+ };
112
164
  //# sourceMappingURL=puppeteer.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"puppeteer.js","sourceRoot":"","sources":["../../src/libs/puppeteer.ts"],"names":[],"mappings":";;;;;;AAAA,oEAAuC;AACvC,oCAAgD;AAChD,iCAA0C;AA4BnC,MAAM,gBAAgB,GAAG,KAAK,EACnC,GAAW,EACX,OAAe,EAAE,EACjB,QAA6B,EAC7B,EAAE;IACF,UAAG,CAAC,2BAA2B,GAAG,UAAU,IAAI,EAAE,CAAC,CAAC;IAEpD,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,iBAAiB,EAAE;QAClC,UAAG,CAAC,kDAAkD,EAAE,OAAO,CAAC,CAAC;KAClE;IAED,MAAM,SAAS,GAAG,IAAI,IAAI,EAAE,CAAC;IAC7B,IAAI,MAAM,GAAG,8BAA8B,4BAAoB,CAC7D,SAAS,EACT,IAAI,IAAI,EAAE,CACX,UAAU,CAAC;IACZ,UAAG,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACrB,IAAI,kBAAW,EAAE;QACf,kBAAW,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QACxB,kBAAW,CAAC,cAAc,CAAC,EAAE,CAAC,CAAC;KAChC;IAED,MAAM,OAAO,GAAG,MAAM,wBAAS,CAAC,OAAO,CAAC;QACtC,iBAAiB,EAAE,qCAAqC,OAAO,CAAC,GAAG,CAAC,iBAAiB,EAAE;KACxF,CAAC,CAAC;IAEH,MAAM,GAAG,6BAA6B,4BAAoB,CACxD,SAAS,EACT,IAAI,IAAI,EAAE,CACX,UAAU,CAAC;IACZ,UAAG,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACrB,IAAI,kBAAW,EAAE;QACf,kBAAW,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QACxB,kBAAW,CAAC,cAAc,CAAC,EAAE,CAAC,CAAC;KAChC;IACD,MAAM,IAAI,GAAG,MAAM,OAAO,CAAC,OAA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"description": "",
5
5
  "main": "./dist/index.js",
6
6
  "types": "types/index.d.ts",
@@ -41,7 +41,7 @@
41
41
  "imgix-management-js": "^1.2.1",
42
42
  "mime-types": "^2.1.33",
43
43
  "node-gzip": "^1.1.2",
44
- "puppeteer-core": "^11.0.0",
44
+ "puppeteer-core": "^15.4.0",
45
45
  "remove-markdown": "^0.3.0",
46
46
  "s3-streamlogger": "^1.7.0",
47
47
  "winston": "^3.7.2"
@@ -2,6 +2,7 @@ import puppeteer from "puppeteer-core";
2
2
  import { secondBetweenTwoDate } from "../utils";
3
3
  import { log, serverUtils } from "./logs";
4
4
  import { PDFOptions } from "puppeteer-core/lib/types";
5
+ import axios from "@atoms-studio/axios";
5
6
 
6
7
  interface HeaderColumns {
7
8
  left?: string;
@@ -20,6 +21,67 @@ interface PDFHeaderAndFooter {
20
21
  footer?: FooterColumns;
21
22
  }
22
23
 
24
+ export const generatePDFByUrl = async (
25
+ url: string,
26
+ path: string = "",
27
+ contents?: PDFHeaderAndFooter
28
+ ) => {
29
+ log(`generatePDFByUrl - url: ${url} path: ${path}`);
30
+
31
+ if (!process.env.BROWSERLESS_TOKEN) {
32
+ log(`BROWSERLESS_TOKEN environment variable not found`, "ERROR");
33
+ }
34
+ let pdf;
35
+ if (path) {
36
+ pdf = await generatePDFByUrlWithPuppeteer(url, path, contents);
37
+ } else {
38
+ pdf = await generatePDFByUrlWithAxios(url, contents);
39
+ }
40
+
41
+ return pdf;
42
+ };
43
+
44
+ // See: https://github.com/puppeteer/puppeteer/blob/v11.0.0/docs/api.md#pagepdfoptions
45
+ const getPDFOptions = (
46
+ contents: PDFHeaderAndFooter | undefined
47
+ ): PDFOptions => {
48
+ const pageMargin = "33px";
49
+ const headerTemplate = ` `;
50
+ const footerTopLeft = contents?.footer?.topLeft || ``;
51
+ const footerTopRight = contents?.footer?.topRight || ``;
52
+ const date = new Date(
53
+ new Date().toLocaleString("en-US", { timeZone: "Europe/Rome" })
54
+ );
55
+ const footerBottomLeft =
56
+ contents?.footer?.bottomLeft ||
57
+ `©2022 Flos - P.IVA 00290820174 - ${
58
+ date.getMonth() + 1
59
+ }/${date.getDate()}/${date.getFullYear()}`;
60
+ const footerMail = contents?.footer?.mail || "info@flos.com";
61
+ const footerBottomRight =
62
+ contents?.footer?.bottomRight ||
63
+ `<a style="text-decoration: none; color:#000" href="https://professional.flos.com/en/global/">professional.flos.com</a> | <a style="text-decoration: none; color:#000" href="mailto:${footerMail}">${footerMail}</a> <span class="pageNumber" style="margin-left:4px"></span>/<span class="totalPages"></span>`;
64
+ const footerBottom =
65
+ "In our constantly evolving world and business, technical upgrades happen every day. This means all product specifications and data are subject to change without warning in order t improve reliability, function, performance or otherwise. We make every effort to ensure the accuracy of our product images, however due to different lighting and screens used for viewing, the colours may vary. Images are indicative of the quality and style of the product but may not represent the precise details of the product you receive. This is because we are constantly working to make improvements. For aesthetic reasons, cables and/or electrical elements are often not shown. Refer to technical data sheets for all technical data. Images and colours are not part of any contract or warranty in any way.";
66
+ const footerStyle = `<style>.pdf-footer { font-family: Arial, Helvetica, sans-serif; font-weight: 300; }</style>`;
67
+ const footerTemplate = `${footerStyle}<div class="pdf-footer" style="margin: 0 ${pageMargin}; padding: 10px 0; width: 100%; font-size: 5pt; font-weight:300;"> <div class="pdf-row" style="display: flex; flex-wrap: wrap; border-bottom: 1px solid rgb(168, 168, 168); border-bottom-style:solid; padding-bottom: 6px; justify-content: center; align-items: center;"> <span class="left" style="margin:0; padding: 0; width: 80%; white-space: normal; overflow: hidden;">${footerTopLeft}</span> <span class="right" style="margin:0; padding: 0; width: 20%; text-align: right;">${footerTopRight}</span> </div> <div class="pdf-row" style="display: flex; flex-wrap: wrap; padding-top: 6px; justify-content: center; align-items: center;"> <span style="width: 50%;">${footerBottomLeft}</span> <span style="width: 50%; text-align: right;"> ${footerBottomRight} </span> </div><div class="pdf-row" style="font-size: 4pt; margin-top:5px;">${footerBottom}</div> </div>`;
68
+
69
+ // See: https://github.com/puppeteer/puppeteer/blob/v11.0.0/docs/api.md#pagepdfoptions
70
+ return {
71
+ format: "A4",
72
+ printBackground: true,
73
+ displayHeaderFooter: true,
74
+ headerTemplate,
75
+ footerTemplate,
76
+ margin: {
77
+ top: pageMargin,
78
+ left: pageMargin,
79
+ right: pageMargin,
80
+ bottom: "114px",
81
+ },
82
+ };
83
+ };
84
+
23
85
  /**
24
86
  * Generate PDF by URL
25
87
  *
@@ -28,7 +90,7 @@ interface PDFHeaderAndFooter {
28
90
  * @param contents E.g. {header{left:'Foo', right:'Bar'}, footer{left:'Foo', right:'Bar'}}
29
91
  * @returns
30
92
  */
31
- export const generatePDFByUrl = async (
93
+ const generatePDFByUrlWithPuppeteer = async (
32
94
  url: string,
33
95
  path: string = "",
34
96
  contents?: PDFHeaderAndFooter
@@ -85,6 +147,12 @@ export const generatePDFByUrl = async (
85
147
  serverUtils.updateProgress(40);
86
148
  }
87
149
 
150
+ if (!res) {
151
+ await browser.close();
152
+ console.log(`Error no response`);
153
+ return null;
154
+ }
155
+
88
156
  if (res.status() !== 200) {
89
157
  await browser.close();
90
158
  console.log(`Error ${res.status()} - ${res.url()}`);
@@ -92,45 +160,11 @@ export const generatePDFByUrl = async (
92
160
  }
93
161
 
94
162
  try {
95
- const pageMargin = "33px";
96
- const headerTemplate = ` `;
97
- const footerTopLeft = contents?.footer?.topLeft || ``;
98
- const footerTopRight = contents?.footer?.topRight || ``;
99
- const date = new Date(
100
- new Date().toLocaleString("en-US", { timeZone: "Europe/Rome" })
101
- );
102
- const footerBottomLeft =
103
- contents?.footer?.bottomLeft ||
104
- `©2022 Flos - P.IVA 00290820174 - ${
105
- date.getMonth() + 1
106
- }/${date.getDate()}/${date.getFullYear()}`;
107
- const footerMail = contents?.footer?.mail || "info@flos.com";
108
- const footerBottomRight =
109
- contents?.footer?.bottomRight ||
110
- `<a style="text-decoration: none; color:#000" href="https://professional.flos.com/en/global/">professional.flos.com</a> | <a style="text-decoration: none; color:#000" href="mailto:${footerMail}">${footerMail}</a> <span class="pageNumber" style="margin-left:4px"></span>/<span class="totalPages"></span>`;
111
- const footerBottom =
112
- "In our constantly evolving world and business, technical upgrades happen every day. This means all product specifications and data are subject to change without warning in order t improve reliability, function, performance or otherwise. We make every effort to ensure the accuracy of our product images, however due to different lighting and screens used for viewing, the colours may vary. Images are indicative of the quality and style of the product but may not represent the precise details of the product you receive. This is because we are constantly working to make improvements. For aesthetic reasons, cables and/or electrical elements are often not shown. Refer to technical data sheets for all technical data. Images and colours are not part of any contract or warranty in any way.";
113
- const footerStyle = `<style>.pdf-footer { font-family: Arial, Helvetica, sans-serif; font-weight: 300; }</style>`;
114
- const footerTemplate = `${footerStyle}<div class="pdf-footer" style="margin: 0 ${pageMargin}; padding: 10px 0; width: 100%; font-size: 5pt; font-weight:300;"> <div class="pdf-row" style="display: flex; flex-wrap: wrap; border-bottom: 1px solid rgb(168, 168, 168); border-bottom-style:solid; padding-bottom: 6px; justify-content: center; align-items: center;"> <span class="left" style="margin:0; padding: 0; width: 80%; white-space: normal; overflow: hidden;">${footerTopLeft}</span> <span class="right" style="margin:0; padding: 0; width: 20%; text-align: right;">${footerTopRight}</span> </div> <div class="pdf-row" style="display: flex; flex-wrap: wrap; padding-top: 6px; justify-content: center; align-items: center;"> <span style="width: 50%;">${footerBottomLeft}</span> <span style="width: 50%; text-align: right;"> ${footerBottomRight} </span> </div><div class="pdf-row" style="font-size: 4pt; margin-top:5px;">${footerBottom}</div> </div>`;
115
-
116
- // See: https://github.com/puppeteer/puppeteer/blob/v11.0.0/docs/api.md#pagepdfoptions
117
- const options: PDFOptions = {
118
- format: "a4",
119
- printBackground: true,
120
- displayHeaderFooter: true,
121
- headerTemplate,
122
- footerTemplate,
123
- margin: {
124
- top: pageMargin,
125
- left: pageMargin,
126
- right: pageMargin,
127
- bottom: "114px",
128
- },
129
- };
130
-
163
+ const options: PDFOptions = getPDFOptions(contents);
131
164
  if (path) {
132
165
  options.path = path;
133
166
  }
167
+
134
168
  logMsg = `Before page.pdf - ${secondBetweenTwoDate(
135
169
  timeStart,
136
170
  new Date()
@@ -168,3 +202,55 @@ export const generatePDFByUrl = async (
168
202
  await browser.close();
169
203
  }
170
204
  };
205
+
206
+ /**
207
+ * Generate PDF by URL
208
+ *
209
+ * @param url E.g. https://my-site.com/slug
210
+ * @param contents E.g. {header{left:'Foo', right:'Bar'}, footer{left:'Foo', right:'Bar'}}
211
+ * @returns
212
+ */
213
+ const generatePDFByUrlWithAxios = async (
214
+ url: string,
215
+ contents?: PDFHeaderAndFooter
216
+ ) => {
217
+ log(`generatePDFByUrl - url: ${url}`);
218
+ const timeStart = new Date();
219
+ let logMsg;
220
+
221
+ if (!process.env.BROWSERLESS_TOKEN) {
222
+ log(`BROWSERLESS_TOKEN environment variable not found`, "ERROR");
223
+ }
224
+
225
+ logMsg = `Before axios request - ${secondBetweenTwoDate(
226
+ timeStart,
227
+ new Date()
228
+ )} seconds`;
229
+ log(logMsg, "DEBUG");
230
+
231
+ const options: any = {
232
+ method: "POST",
233
+ url: "https://chrome.browserless.io/pdf",
234
+ params: { token: process.env.BROWSERLESS_TOKEN },
235
+ responseType: "arraybuffer",
236
+ headers: {
237
+ "cache-control": "no-cache",
238
+ "content-type": "application/json",
239
+ },
240
+ data: {
241
+ url,
242
+ gotoOptions: { waitUntil: "networkidle0" },
243
+ options: getPDFOptions(contents),
244
+ },
245
+ };
246
+
247
+ const response = await axios.request(options);
248
+
249
+ logMsg = `after axios request - ${secondBetweenTwoDate(
250
+ timeStart,
251
+ new Date()
252
+ )} seconds`;
253
+ log(logMsg, "DEBUG");
254
+
255
+ return response.data;
256
+ };
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  interface HeaderColumns {
3
2
  left?: string;
4
3
  right?: string;
@@ -14,5 +13,5 @@ interface PDFHeaderAndFooter {
14
13
  header?: HeaderColumns;
15
14
  footer?: FooterColumns;
16
15
  }
17
- export declare const generatePDFByUrl: (url: string, path?: string, contents?: PDFHeaderAndFooter | undefined) => Promise<Buffer | null>;
16
+ export declare const generatePDFByUrl: (url: string, path?: string, contents?: PDFHeaderAndFooter | undefined) => Promise<any>;
18
17
  export {};