-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.js
107 lines (99 loc) · 2.99 KB
/
service.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
const http = require('node:http');
const qs = require('node:querystring');
const puppeteer = require('puppeteer');
const port = 8080;
const server = http.createServer(async (request, response) =>
{
let body = '';
request.on('data', function (data) {
body += data;
if (body.length > 1e8) {
request.connection.destroy();
}
});
request.on('end', async function () {
try {
// POST must be "content-type: application/x-www-form-urlencoded"
const post = qs.parse(body);
const pdf = await generatePDF(post.html, {
format: post.format ?? 'A4',
margin: {top: '20mm', bottom: '20mm', left: '10mm', right: '10mm'},
landscape: !!post.landscape,
displayHeaderFooter: true,
headerTemplate: post.headerTemplate ?? '',
footerTemplate: post.footerTemplate ?? ''
});
response.statusCode = 200;
response.setHeader('Content-Type', 'application/pdf');
response.setHeader('Content-Length', pdf.length);
response.end(pdf);
}
catch (error) {
response.statusCode = 500;
response.end(error.message);
}
});
});
server.listen(port, () => {
console.log(`Server running on port ${port}`);
});
/**
* Generate PDF with puppeteer
* @param {string} html
* @param {object} options
* @returns {Promise<*>}
*/
async function generatePDF(html, options) {
const browser = await puppeteer.launch({headless: true, args: getPuppeteerArgs()});
const page = await browser.newPage();
await page.setContent(html, {waitUntil: 'networkidle0'});
const pdf = await page.pdf(options);
await browser.close();
return pdf;
}
/**
* Get arguments for launching puppeteer
* @see https://apitemplate.io/blog/tips-for-generating-pdfs-with-puppeteer/
* @returns {string[]}
*/
function getPuppeteerArgs() {
return [
'--disable-features=IsolateOrigins',
'--disable-site-isolation-trials',
'--autoplay-policy=user-gesture-required',
'--disable-background-networking',
'--disable-background-timer-throttling',
'--disable-backgrounding-occluded-windows',
'--disable-breakpad',
'--disable-client-side-phishing-detection',
'--disable-component-update',
'--disable-default-apps',
'--disable-dev-shm-usage',
'--disable-domain-reliability',
'--disable-extensions',
'--disable-features=AudioServiceOutOfProcess',
'--disable-hang-monitor',
'--disable-ipc-flooding-protection',
'--disable-notifications',
'--disable-offer-store-unmasked-wallet-cards',
'--disable-popup-blocking',
'--disable-print-preview',
'--disable-prompt-on-repost',
'--disable-renderer-backgrounding',
'--disable-setuid-sandbox',
'--disable-speech-api',
'--disable-sync',
'--hide-scrollbars',
'--ignore-gpu-blacklist',
'--metrics-recording-only',
'--mute-audio',
'--no-default-browser-check',
'--no-first-run',
'--no-pings',
'--no-sandbox',
'--no-zygote',
'--password-store=basic',
'--use-gl=swiftshader',
'--use-mock-keychain'
];
}