-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
145 lines (100 loc) · 3.86 KB
/
script.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
if ( ! process.env.P_URL ) {
process.stdout.write(`Environment variable P_URL is not defined`);
process.exit(1);
}
if ( ! process.env.P_TMPFILE) {
process.stdout.write(`Environment variable P_TMPFILE is not defined`);
process.exit(2);
}
// https://stackoverflow.com/a/6182519/5560682
const config = JSON.parse(Buffer.from(process.env.P_JSON, 'base64').toString('ascii') || '{}') || {};
const puppeteer = require('puppeteer');
const fs = require('fs');
const path = require('path');
const timeout = 15000;
(async() => {
const browser = await puppeteer.launch({
args: [
'--no-sandbox',
'--disable-setuid-sandbox'
],
// without this will continue to use the bundled version of Chromium that Puppeteer installs
// from: https://github.com/GoogleChrome/puppeteer/issues/379#issuecomment-328572755
// executablePath: '/usr/bin/chromium-browser', // chromium
executablePath: '/usr/bin/google-chrome-unstable', // google chrome
timeout
});
try {
const page = await browser.newPage();
page.setDefaultNavigationTimeout(timeout);
page.on('error', msg => {
process.stdout.write(`error: ` + JSON.stringify(msg));
browser.close();
process.exit(1);
});
page.on('pageerror', msg => {
process.stdout.write(`pageerror: ` + JSON.stringify(msg));
browser.close();
process.exit(1);
});
process.on("unhandledRejection", (reason, p) => {
process.stdout.write("unhandledRejection: Unhandled Rejection at: Promise" + JSON.stringify(p) + "reason:" + JSON.stringify(reason));
browser.close();
process.exit(1);
});
await page.goto(process.env.P_URL, {waitUntil: [
'load',
'domcontentloaded',
'networkidle0',
'networkidle2',
]});
await new Promise(resolve => setTimeout(resolve, 300));
// https://github.com/GoogleChrome/puppeteer/issues/666#issuecomment-326796411
// await page.pdf({
// path: '/app/app/' + process.env.P_TMPFILE,
// printBackground: true,
// format: 'A4',
// margin: { top: 0, right: 0, bottom: 0, left: 0 },
// scale: 0.7
// });
let pdfConfig = {
...config.pdf,
...{
path: '/app/app/' + process.env.P_TMPFILE
}
};
if (/^(\d+\.)?\d+$/.test(pdfConfig.scale)) {
pdfConfig.scale = parseFloat(pdfConfig.scale);
}
else {
delete pdfConfig.scale;
}
// pdfConfig = {
// path: '/app/app/' + process.env.P_TMPFILE,
// format: 'A4',
// displayHeaderFooter: true,
// footerTemplate: '<div>Page <span class="pageNumber"></span> of <span class="totalPages"></span></div>',
// margin: {
// top: '100px',
// right: '20px',
// bottom: '100px',
// left: '20px'
// },
// }
// fs.writeFileSync('/app/app/test.log', "\n\n"+ (new Date()).toISOString().substring(0, 19).replace('T', ' ')+"\n\n"+JSON.stringify(pdfConfig, null, 4)+"\n\n");
await page.pdf(pdfConfig);
// process.stdout.write(`ret: ${JSON.stringify({
// ...config.pdf,
// ...{
// path: '/app/app/' + process.env.P_TMPFILE
// }
// }, null, 4)}`);
// process.exit(1);
browser.close();
}
catch (e) {
process.stdout.write(`puppeteer catch: ` + JSON.stringify(e.message));
browser.close();
process.exit(1);
}
})();