-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
227 lines (201 loc) · 5.96 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
const html_to_pdf = require("html-pdf-node");
const fs = require("fs");
/**
* This function generates a PDF file from a html template and returns it as a blob.
* @description
* For more information about the html-pdf-node package including additional
* pdf generation options, visit https://www.npmjs.com/package/html-pdf-node.
* @param {string} path - Path to the html file.
* @param {object | null} context - Options for the PDF generation.
* @param {object} opts - Options for the PDF generation. Visit https://www.npmjs.com/package/html-pdf-node for more information.
* @returns {Promise<Buffer>} The generated PDF file as a blob.
* @example const buffer = await generatePDF('path/to/file.html', { name: 'John Doe' });
*/
async function generatePDF(path, context = null, opts = {}) {
const file = {
content: await dynamicLoadPDF(path, context),
};
return await new Promise((resolve, reject) => {
html_to_pdf
.generatePdf(file, opts)
.then((pdfBuffer) => {
resolve(pdfBuffer);
})
.catch((error) => {
reject(error);
});
});
}
/**
* Loads a html file and returns it as a string.
* @param {string} path - Path to the html file.
* @param {object | null} context - Options for the PDF generation.
* @returns {Promise<string>} The html file as a string.
* @example const html = await dynamicLoadPDF('path/to/file.html', { name: 'John Doe' });
*/
async function dynamicLoadPDF(path, context = null) {
return await new Promise((resolve, reject) => {
fs.readFile(path, "utf8", (err, data) => {
if (err) reject(err);
resolve(
replaceBracesWithValues(evaluateConditions(data, context), context)
);
});
});
}
/**
* Navigates a string hierarchy and returns the last value.
* @param {string} str - The string to navigate.
* @param {object} context - The context object to navigate.
* @returns {string} The value from the context object.
* @example
* const str = navigateStringHierarchy("employee.salary", { employee: { salary: "$90000" } }});
* console.log(str); // Returns "$90000"
*/
function navigateStringHierarchy(str, context) {
if (!str.includes(".")) {
try {
return context[str];
} catch {
return null;
}
}
const hierarchy = str.split(".");
context = context[hierarchy[0]];
let newStr = "";
for (let i = 0; i < hierarchy.length; i++) {
if (i > 1) {
newStr += ".";
}
if (i > 0) {
newStr += hierarchy[i];
}
}
return navigateStringHierarchy(newStr, context);
}
/**
* Replaces all braces with the corresponding value from the context object.
* @param {string} text - The text to replace the braces in.
* @param {object | null} context - Options for the PDF generation.
* @returns {string} The text with the replaced braces.
* @example replaceBracesWithValues("Hello {name}!", { name: "World" });
*/
function replaceBracesWithValues(text, context) {
if (context === null) return text;
let isField = false;
let currentField = "";
let startIndex, endIndex;
for (let i = 0; i < text.length; i++) {
if (text[i] === "{") {
isField = true;
currentField = "";
startIndex = i;
continue;
}
if (text[i] === "}" && isField) {
isField = false;
endIndex = i;
if (navigateStringHierarchy(currentField, context)) {
text =
text.substring(0, startIndex) +
navigateStringHierarchy(currentField, context) +
text.substring(endIndex + 1);
return replaceBracesWithValues(text, context);
}
continue;
}
if (isField && text[i] !== " ") {
currentField += text[i];
}
}
return text;
}
/**
* Hides or shows content based on existing keys in the context object.
* @param text - The text to evaluate the conditions in.
* @param context - The context object.
* @returns {string} The text with the evaluated conditions.
* @example evaluateConditions("Hello ?{name World}?!", { name: "World" });
*/
function evaluateConditions(text, context) {
if (context === null) return text;
let startIndex = null;
let endIndex = null;
let currentField = "";
let content = "";
let nestCount = 0;
for (let i = 0; i < text.length; i++) {
if (
i - 1 > -1 &&
text[i] === "{" &&
text[i - 1] === "?" &&
startIndex === null
) {
startIndex = i - 1;
continue;
} else if (
i - 1 > -1 &&
text[i] === "{" &&
text[i - 1] === "?" &&
startIndex !== null
) {
nestCount++;
}
if (
i + 1 < text.length &&
text[i] === "}" &&
text[i + 1] === "?" &&
startIndex !== null &&
nestCount === 0
) {
endIndex = i + 1;
const replaceText = () => {
text =
text.substring(0, startIndex) +
content +
text.substring(endIndex + 1);
};
const replaceEmptyText = () => {
text = text.substring(0, startIndex) + text.substring(endIndex + 1);
};
if (currentField.trim()[0] !== "!") {
if (navigateStringHierarchy(currentField.trim(), context)) {
replaceText();
} else {
replaceEmptyText();
}
} else {
currentField = currentField.trim().substring(1);
if (!navigateStringHierarchy(currentField.trim(), context)) {
replaceText();
} else {
replaceEmptyText();
}
}
return evaluateConditions(text, context);
} else if (
i + 1 < text.length &&
text[i] === "}" &&
text[i + 1] === "?" &&
startIndex !== null &&
nestCount > 0
) {
nestCount--;
}
if (startIndex !== null) {
if (text[i] !== " " && !content.length) {
currentField += text[i];
}
if (currentField.length && !content.length && text[i] === " ") {
content = text[i];
continue;
}
if (content.length) {
if (content[0] === " ") content = "";
content += text[i];
}
}
}
return text;
}
module.exports = generatePDF;