-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.chk.js
300 lines (298 loc) · 8.76 KB
/
index.chk.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// @ts-check
/**
* Copyright (c) 2018, SOW (https://www.safeonline.world). (https://github.com/RKTUXYN) All rights reserved.
* @author {SOW}
* Copyrights licensed under the New BSD License.
* See the accompanying LICENSE file for terms.
*/
//By Rajib Chy
// On 11:00 AM 12/25/2020
/**
* @typedef {import('./index').ICPdfConfig} ICPdfConfig
*/
const path = require('path');
const os = require("os");
const fs = require('fs');
const { Writable } = require('stream');
const { ServerResponse } = require('http');
/**
* Import PDF Native Module
* @returns {import('./index').html2pdf_native}
*/
function import_module() {
let binding_path;
if (process.env.LUNCH_MODE === "DEBUG") {
binding_path = "./build/Release/reqw.node";
} else {
binding_path = require('node-pre-gyp').find(path.resolve(path.join(__dirname, './package.json')));
}
return require(binding_path).reqw;
}
const cpdfy = import_module();
/*const defaultConfig = {
global_settings: {
"documentTitle": "Hello World",
"size.paperSize": "A4",
"orientation": "Portrait",
"colorMode": "Color",
"dpi": "80",
"imageDPI": "300",
"imageQuality": "92",
"margin.top": "1.27cm",
"margin.bottom": "1.27cm",
"margin.left": "1.27cm",
"margin.right": "1.27cm"
},
object_settings: {
"web.defaultEncoding": "utf-8",
"web.background": "true",
"web.loadImages": "true",
"web.enablePlugins": "false",
"web.enableJavascript": "false",
"web.enableIntelligentShrinking": "true",
"web.minimumFontSize": "12",
"web.printMediaType": "true",
"header.fontSize": "8",
"header.fontName": "Times New Roman",
"header.left": "[date]",
"header.line": "false",
"header.spacing": "0",
"footer.fontSize": "8",
"footer.right": "Page [page] of [topage]",
"footer.line": "false",
"footer.spacing": "0"
}
};*/
/**
* To `String`
* @param {any} val
* @returns {string}
*/
function toString(val) {
if (!val) return undefined;
if (typeof (val) === "string") return val;
return String(val);
}
function parseConfig(obj, outObj) {
for (let prop in obj) {
let next = obj[prop];
if (next && typeof (next) === "object") {
for (let nprop in next) {
outObj[`${prop}.${nprop}`] = toString(next[nprop]);
}
} else {
outObj[prop] = toString(next);
}
}
}
/**
* Create PDF Config
* @param {ICPdfConfig} config
*/
function prepareConfig(config) {
/** @type {ICPdfConfig} */
let outConfig = {};
if (config.global_settings) {
outConfig.global_settings = {};
parseConfig(config.global_settings, outConfig.global_settings);
}
if (config.object_settings) {
outConfig.object_settings = {};
parseConfig(config.object_settings, outConfig.object_settings);
}
outConfig.from_path = config.from_path || undefined;
outConfig.from_url = config.from_url || undefined;
outConfig.out_path = config.out_path || undefined;
for (let prop in outConfig) {
config[prop] = outConfig[prop];
}
}
/**
* Set pdf response header
* @param {ServerResponse} res
*/
function _setHeader(res) {
const header = cpdfy.get_http_header();
for (let key in header) {
res.setHeader(key, header[key]);
}
}
/**
* Write to Http Response
* @param {Writable} res
* @param {ICPdfConfig} config
* @param {string} htmlStr
* @param {(err?:Error)=>void} next
* @param {()=>boolean} onOpen
* @returns {void}
*/
function _pipeToWritableStream(res, config, htmlStr, next, onOpen) {
prepareConfig(config);
config.out_path = path.resolve(`${os.tmpdir()}/${Math.floor((0x999 + Math.random()) * 0x10000000)}.pdf`);
try {
cpdfy.generate_pdf(config, htmlStr);
} catch (e) {
return next(e);
}
const stream = fs.createReadStream(config.out_path);
let isEnded = false;
stream.on("open", (fd) => {
if (!onOpen()) {
return stream.emit("end");
}
stream.pipe(res);
}).on("error", (err) => {
return next(err);
}).on("end", () => {
if (isEnded) return;
isEnded = true;
fs.stat(config.out_path, (err, state) => {
if (state) {
fs.rm(config.out_path, (err) => {
// No need to verify this result
});
}
});
return next();
});
return void 0;
}
/**
* Write to Http Response
* @param {Writable} res
* @param {ICPdfConfig} config
* @param {string} htmlStr
* @param {(err?:Error)=>void} next
* @returns {void}
*/
function _pipeStream(res, config, htmlStr, next) {
if (typeof (config) === "string") {
if (typeof (htmlStr) === "function") {
next = htmlStr; htmlStr = undefined;
}
htmlStr = config; config = {};
}
if (typeof (htmlStr) === "function") {
next = htmlStr; htmlStr = undefined;
} else if (typeof (htmlStr) !== "string") {
htmlStr = undefined;
}
/** @type {()=>boolean} */
let onOpen;
if ("headersSent" in res || res instanceof ServerResponse) {
onOpen = () => {
if (res.headersSent) {
return next(new Error("Remote connection closed...")), false;
}
return _setHeader(res), true;
};
} else {
onOpen = () => true;
}
if (typeof (next) !== "function") {
next = (...args) => void 0;
}
return _pipeToWritableStream(res, config, htmlStr, next, onOpen);
}
class Cpdfy {
/**
* Generate PDF
* @param {ICPdfConfig} config
* @param {string|void} htmlStr
*/
static generatePdf(config, htmlStr) {
if (typeof (config) === "string") {
htmlStr = config; config = {};
}
prepareConfig(config);
return cpdfy.generate_pdf(config, htmlStr);
}
/**
*
* @param {ServerResponse} res
*/
static setHeader(res) {
return _setHeader(res);
}
static getHttpHeader() {
return cpdfy.get_http_header();
}
static destroyApp() {
return cpdfy.destroy_app();
}
/**
* Create or pip to ouput stream
* @param {ICPdfConfig|ServerResponse|fs.WriteStream} config
* @param {string} htmlStr
* @param {(err:Error, stream:fs.ReadStream)=>void} next
* @returns {void}
*/
static createStream(config, htmlStr, next) {
if (config instanceof ServerResponse || config instanceof fs.WriteStream) {
return _pipeStream.apply(this, Array.prototype.slice.call(arguments));
}
if (typeof (config) === "string") {
if (typeof (htmlStr) === "function") {
next = htmlStr; htmlStr = undefined;
}
htmlStr = config; config = {};
}
if (typeof (htmlStr) === "function") {
next = htmlStr; htmlStr = undefined;
} else if (typeof (htmlStr) !== "string") {
htmlStr = undefined;
}
if (typeof (next) !== "function") {
throw new Error("Callback should be instance of `Function`.");
}
prepareConfig(config);
config.out_path = path.resolve(`${os.tmpdir()}/${Math.floor((0x999 + Math.random()) * 0x10000000)}.pdf`);
try {
cpdfy.generate_pdf(config, htmlStr);
} catch (e) {
return next(e, null);
}
const stream = fs.createReadStream(config.out_path);
stream.on("open", (fd) => {
next(null, stream);
}).on("error", (err) => {
next(err, null);
}).on("end", () => {
// @ts-ignore
fs.stat(config.out_path, (err, state) => {
if (state) {
// @ts-ignore
fs.rm(config.out_path, (err) => {
// No need to verify this result
});
}
});
});
return void 0;
}
static createStreamAsync(config, htmlStr) {
if (typeof (config) === "string") {
htmlStr = config; config = {};
}
return new Promise((reject, reslove) => {
try {
Cpdfy.createStream(config, htmlStr, (err, stream) => {
if (err) return reject(err);
return reslove(stream);
});
} catch (e) {
return reject(e);
}
});
}
static generatePdfAsync(config, htmlStr) {
return new Promise((reject, reslove) => {
try {
return reslove(Cpdfy.generatePdf(config, htmlStr));
} catch (e) {
return reject(e);
}
});
}
};
module.exports.Cpdfy = Cpdfy;