forked from Szpadel/chrome-headless-render-pdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
311 lines (274 loc) · 10.2 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
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
301
302
303
304
305
306
307
308
309
310
311
const CDP = require('chrome-remote-interface');
const fs = require('fs');
const cp = require('child_process');
const net = require('net');
const commandExists = require('command-exists');
class RenderPDF {
constructor(options) {
this.setOptions(options || {});
this.chrome = null;
this.port = Math.floor(Math.random() * 10000 + 1000);
}
setOptions(options) {
this.options = {
printLogs: def('printLogs', false),
printErrors: def('printErrors', true),
chromeBinary: def('chromeBinary', null),
noMargins: def('noMargins', false),
landscape: def('landscape', undefined),
paperWidth: def('paperWidth', undefined),
paperHeight: def('paperHeight', undefined),
includeBackground: def('includeBackground', undefined)
};
this.commandLineOptions = {
windowSize: def('windowSize', undefined),
};
function def(key, defaultValue) {
return options[key] === undefined ? defaultValue : options[key];
}
}
static async generateSinglePdf(url, filename, options) {
const renderer = new RenderPDF(options);
await renderer.spawnChrome();
await renderer.waitForDebugPort();
try {
const buff = await renderer.renderPdf(url, renderer.generatePdfOptions());
fs.writeFileSync(filename, buff);
renderer.log(`Saved ${filename}`);
} catch (e) {
renderer.error('error:', e);
}
renderer.killChrome();
}
static async generatePdfBuffer(url, options) {
const renderer = new RenderPDF(options);
await renderer.spawnChrome();
await renderer.waitForDebugPort();
try {
return renderer.renderPdf(url, renderer.generatePdfOptions());
} catch (e) {
renderer.error('error:', e);
} finally {
renderer.killChrome();
}
}
static async generateMultiplePdf(pairs, options) {
const renderer = new RenderPDF(options);
await renderer.spawnChrome();
await renderer.waitForDebugPort();
for (const job of pairs) {
try {
const buff = await renderer.renderPdf(job.url, renderer.generatePdfOptions());
fs.writeFileSync(job.pdf, buff);
renderer.log(`Saved ${job.pdf}`);
} catch (e) {
renderer.error('error:', e);
}
}
renderer.killChrome();
}
async renderPdf(url, options) {
return new Promise((resolve) => {
CDP({port: this.port}, async (client) => {
this.log(`Opening ${url}`);
const {Page, Emulation, LayerTree} = client;
await Page.enable();
await LayerTree.enable();
await Page.navigate({url});
// await Emulation.setVirtualTimePolicy({policy: 'pauseIfNetworkFetchesPending', budget: 5000});
const loaded = this.cbToPromise(Page.loadEventFired);
// const jsDone = this.cbToPromise(Emulation.virtualTimeBudgetExpired);
await this.profileScope('Wait for load', async () => {
await loaded;
});
// await this.profileScope('Wait for js execution', async () => {
// await jsDone;
// });
await this.profileScope('Wait for animations', async () => {
await new Promise((resolve) => {
setTimeout(resolve, 5000); // max waiting time
let timeout = setTimeout(resolve, 100);
LayerTree.layerPainted(() => {
clearTimeout(timeout);
timeout = setTimeout(resolve, 100);
});
});
});
const pdf = await Page.printToPDF(options);
const buff = Buffer.from(pdf.data, 'base64');
client.close();
resolve(buff);
});
});
}
generatePdfOptions() {
const options = {};
if (this.options.landscape !== undefined) {
options.landscape = !!this.options.landscape;
}
if (this.options.noMargins) {
options.marginTop = 0;
options.marginBottom = 0;
options.marginLeft = 0;
options.marginRight = 0;
}
if (this.options.includeBackground !== undefined) {
options.printBackground = !!this.options.includeBackground;
}
if(this.options.paperWidth !== undefined) {
options.paperWidth = parseFloat(this.options.paperWidth);
}
if(this.options.paperHeight !== undefined) {
options.paperHeight = parseFloat(this.options.paperHeight);
}
return options;
}
error(...msg) {
if (this.options.printErrors) {
console.error(...msg);
}
}
log(...msg) {
if (this.options.printLogs) {
console.log(...msg);
}
}
async cbToPromise(cb) {
return new Promise((resolve) => {
cb((resp) => {
resolve(resp);
})
});
}
getPerfTime(prev) {
const time = process.hrtime(prev);
return time[0] * 1e3 + time[1] / 1e6;
}
async profileScope(msg, cb) {
const start = process.hrtime();
await cb();
this.log(msg, `took ${Math.round(this.getPerfTime(start))}ms`);
}
browserLog(type, msg) {
const lines = msg.split('\n');
for (const line of lines) {
this.log(`(chrome) (${type}) ${line}`);
}
}
async spawnChrome() {
const chromeExec = this.options.chromeBinary || await this.detectChrome();
this.log('Using', chromeExec);
const commandLineOptions = [
'--headless',
`--remote-debugging-port=${this.port}`,
'--disable-gpu'
];
if (this.commandLineOptions.windowSize !== undefined ) {
commandLineOptions.push(`--window-size=${this.commandLineOptions.windowSize[0]},${this.commandLineOptions.windowSize[1]}`);
}
this.chrome = cp.spawn(
chromeExec,
commandLineOptions
);
this.chrome.on('close', (code) => {
this.log(`Chrome stopped (${code})`);
this.browserLog('out', this.chrome.stdout.toString());
this.browserLog('err', this.chrome.stderr.toString());
});
}
async isCommandExists(cmd) {
return new Promise((resolve, reject) => {
commandExists(cmd, (err, exists) => {
if (err) {
reject(err);
} else {
resolve(exists);
}
})
});
}
async detectChrome() {
if (await this.isCommandExists('google-chrome-unstable')) {
return 'google-chrome-unstable';
}
if (await this.isCommandExists('google-chrome-beta')) {
return 'google-chrome-beta';
}
if (await this.isCommandExists('google-stable')) {
return 'google-stable';
}
if (await this.isCommandExists('google-chrome')) {
return 'google-chrome';
}
if (await this.isCommandExists('chromium')) {
return 'chromium';
}
// windows
if (await this.isCommandExists('chrome')) {
return 'chrome';
}
// macos
if (await this.isCommandExists('/Applications/Google\ Chrome\ Canary.app/Contents/MacOS/Google\ Chrome\ Canary')) {
return '/Applications/Google\ Chrome\ Canary.app/Contents/MacOS/Google\ Chrome\ Canary';
}
if (await this.isCommandExists('/Applications/Google\ Chrome\ Beta.app/Contents/MacOS/Google\ Chrome\ Beta')) {
return '/Applications/Google\ Chrome\ Beta.app/Contents/MacOS/Google\ Chrome\ Beta';
}
if (await this.isCommandExists('/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome')) {
return '/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome';
}
throw Error('Couldn\'t detect chrome version installed! use --chrome-binary to pass custom location');
}
killChrome() {
this.chrome.kill(cp.SIGKILL);
}
async waitForDebugPort() {
this.log('Waiting for chrome to became available');
while (true) {
try {
await this.isPortOpen('localhost', this.port);
this.log('Connected!');
await this.checkChromeVersion();
return;
} catch (e) {
await this.wait(10);
}
}
}
async checkChromeVersion() {
return new Promise((resolve) => {
CDP({port: this.port}, async (client) => {
const {Browser} = client;
const version = await Browser.getVersion();
if(version.product.search('/64.') !== -1) {
console.error(' ===== WARNING =====');
console.error(' Detected Chrome in version 64.x');
console.error(' This version is known to contain bug in remote api that prevents this tool to work');
console.error(' This issue is resolved in version 65');
console.error(' More info: https://github.com/Szpadel/chrome-headless-render-pdf/issues/22');
}
resolve();
});
});
}
async isPortOpen(host, port) {
return new Promise(function (resolve, reject) {
const connection = new net.Socket();
connection.connect({host, port});
connection.on('connect', () => {
connection.end();
resolve();
});
connection.on('error', () => {
reject();
})
});
}
async wait(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
}
module.exports = RenderPDF;
module.exports.default = RenderPDF;