pim-import 4.4.0 → 4.5.2

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.
@@ -1,263 +0,0 @@
1
- import puppeteer from "puppeteer-core";
2
- import { secondBetweenTwoDate } from "../utils";
3
- import { log, serverUtils } from "./logs";
4
- import { PDFOptions } from "puppeteer-core/lib/types";
5
- import axios from "@atoms-studio/axios";
6
-
7
- interface HeaderColumns {
8
- left?: string;
9
- right?: string;
10
- }
11
- interface FooterColumns {
12
- topLeft?: string;
13
- topRight?: string;
14
- bottomLeft?: string;
15
- bottomRight?: string;
16
- mail?: string;
17
- }
18
-
19
- interface PDFHeaderAndFooter {
20
- header?: HeaderColumns;
21
- footer?: FooterColumns;
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
-
85
- /**
86
- * Generate PDF by URL
87
- *
88
- * @param url E.g. https://my-site.com/slug
89
- * @param path E.g. my-file.pdf
90
- * @param contents E.g. {header{left:'Foo', right:'Bar'}, footer{left:'Foo', right:'Bar'}}
91
- * @returns
92
- */
93
- const generatePDFByUrlWithPuppeteer = async (
94
- url: string,
95
- path: string = "",
96
- contents?: PDFHeaderAndFooter
97
- ) => {
98
- log(`generatePDFByUrl - url: ${url} path: ${path}`);
99
-
100
- if (!process.env.BROWSERLESS_TOKEN) {
101
- log(`BROWSERLESS_TOKEN environment variable not found`, "ERROR");
102
- }
103
-
104
- const timeStart = new Date();
105
- let logMsg = `Before puppeteer.connect - ${secondBetweenTwoDate(
106
- timeStart,
107
- new Date()
108
- )} seconds`;
109
- log(logMsg, "DEBUG");
110
- if (serverUtils) {
111
- serverUtils.log(logMsg);
112
- }
113
-
114
- const browser = await puppeteer.connect({
115
- browserWSEndpoint: `wss://chrome.browserless.io?token=${process.env.BROWSERLESS_TOKEN}`,
116
- });
117
-
118
- logMsg = `After puppeteer.connect - ${secondBetweenTwoDate(
119
- timeStart,
120
- new Date()
121
- )} seconds`;
122
- log(logMsg, "DEBUG");
123
- if (serverUtils) {
124
- serverUtils.log(logMsg);
125
- }
126
- const page = await browser.newPage();
127
-
128
- logMsg = `Before page.goto - ${secondBetweenTwoDate(
129
- timeStart,
130
- new Date()
131
- )} seconds`;
132
- log(logMsg, "DEBUG");
133
- if (serverUtils) {
134
- serverUtils.log(logMsg);
135
- }
136
- const res = await page.goto(url, { waitUntil: "networkidle0", timeout: 0 });
137
- logMsg = `After page.goto - ${secondBetweenTwoDate(
138
- timeStart,
139
- new Date()
140
- )} seconds`;
141
- log(logMsg, "DEBUG");
142
- if (serverUtils) {
143
- serverUtils.log(logMsg);
144
- }
145
-
146
- if (!res) {
147
- await browser.close();
148
- console.log(`Error no response`);
149
- return null;
150
- }
151
-
152
- if (res.status() !== 200) {
153
- await browser.close();
154
- console.log(`Error ${res.status()} - ${res.url()}`);
155
- return null;
156
- }
157
-
158
- try {
159
- const options: PDFOptions = getPDFOptions(contents);
160
- if (path) {
161
- options.path = path;
162
- }
163
-
164
- logMsg = `Before page.pdf - ${secondBetweenTwoDate(
165
- timeStart,
166
- new Date()
167
- )} seconds`;
168
- log(logMsg, "DEBUG");
169
- if (serverUtils) {
170
- serverUtils.log(logMsg);
171
- }
172
- const pdf = await page.pdf(options);
173
- logMsg = `After page.pdf - ${secondBetweenTwoDate(
174
- timeStart,
175
- new Date()
176
- )} seconds`;
177
- log(logMsg, "DEBUG");
178
- if (serverUtils) {
179
- serverUtils.log(logMsg);
180
- }
181
-
182
- const timeEnd = new Date();
183
- const seconds = secondBetweenTwoDate(timeStart, timeEnd);
184
- logMsg = `Request time: ${seconds} seconds - generatePDFByUrl`;
185
- log(logMsg, "DEBUG");
186
- if (serverUtils) {
187
- serverUtils.log(logMsg);
188
- }
189
-
190
- return pdf;
191
- } catch (err: any) {
192
- console.log(err);
193
- return null;
194
- } finally {
195
- await browser.close();
196
- }
197
- };
198
-
199
- /**
200
- * Generate PDF by URL
201
- *
202
- * @param url E.g. https://my-site.com/slug
203
- * @param contents E.g. {header{left:'Foo', right:'Bar'}, footer{left:'Foo', right:'Bar'}}
204
- * @returns
205
- */
206
- const generatePDFByUrlWithAxios = async (
207
- url: string,
208
- contents?: PDFHeaderAndFooter
209
- ) => {
210
- let logMsg = `generatePDFByUrl - url: ${url}`;
211
- log(logMsg, "DEBUG");
212
- if (serverUtils) {
213
- serverUtils.log(logMsg);
214
- }
215
- const timeStart = new Date();
216
-
217
- if (!process.env.BROWSERLESS_TOKEN) {
218
- logMsg = `BROWSERLESS_TOKEN environment variable not found`;
219
- if (serverUtils) {
220
- serverUtils.log(logMsg);
221
- }
222
- log(logMsg, "ERROR");
223
- return null;
224
- }
225
-
226
- logMsg = `Before axios request - ${secondBetweenTwoDate(
227
- timeStart,
228
- new Date()
229
- )} seconds`;
230
- log(logMsg, "DEBUG");
231
- if (serverUtils) {
232
- serverUtils.log(logMsg);
233
- }
234
-
235
- const options: any = {
236
- method: "POST",
237
- url: "https://chrome.browserless.io/pdf",
238
- params: { token: process.env.BROWSERLESS_TOKEN },
239
- responseType: "arraybuffer",
240
- headers: {
241
- "cache-control": "no-cache",
242
- "content-type": "application/json",
243
- },
244
- data: {
245
- url,
246
- gotoOptions: { waitUntil: "networkidle0" },
247
- options: getPDFOptions(contents),
248
- },
249
- };
250
-
251
- const response = await axios.request(options);
252
-
253
- logMsg = `after axios request - ${secondBetweenTwoDate(
254
- timeStart,
255
- new Date()
256
- )} seconds`;
257
- log(logMsg, "DEBUG");
258
- if (serverUtils) {
259
- serverUtils.log(logMsg);
260
- }
261
-
262
- return response.data;
263
- };