-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.js
42 lines (33 loc) · 1.04 KB
/
handler.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
const chromium = require('chrome-aws-lambda');
exports.handler = async (event, context) => {
let result = null;
let browser = null;
try {
const body = JSON.parse(event.body);
const html = body.html;
browser = await chromium.puppeteer.launch({
args: chromium.args,
defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath,
headless: chromium.headless,
});
const page = await browser.newPage();
await page.setContent(html);
const pdfBuffer = await page.pdf({ format: 'A4' });
result = pdfBuffer.toString('base64'); // Convert the PDF data to a base64 string
} catch (error) {
return context.fail(error);
} finally {
if (browser !== null) {
await browser.close();
}
}
return {
statusCode: 200,
isBase64Encoded: true,
headers: {
'Content-Type': 'application/pdf',
},
body: result
};
};