-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (53 loc) · 1.45 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
const chromium = require('chrome-aws-lambda');
const log = require('lambda-log');
function parseEventBody (event) {
let body = {};
if (event.body !== null && event.body !== undefined) {
body = (typeof event.body === 'string') ? JSON.parse(event.body) : event.body;
}
log.info('body', body);
return body;
};
exports.handler = async (event, context) => {
log.info('event: ', event );
const body = parseEventBody(event)
const { data: { attributes: { html }}} = body
log.info('body: ', body );
let browser = null;
let buffer = null;
const puppeteerOptions = {
printBackground: true,
scale: 1
}
try {
browser = await chromium.puppeteer.launch({
args: chromium.args,
defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath,
headless: chromium.headless,
});
let page = await browser.newPage();
await page.setContent(html)
buffer = await page.pdf(puppeteerOptions)
} catch (error) {
return context.fail(error);
} finally {
if (browser !== null) {
await browser.close();
}
}
const results = {
id: '235325',
mime_type: 'application/pdf',
binary_encoding: 'base64',
binary_stream: buffer.toString('base64')
}
return {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*",
"Content-Type": "application/json"
},
body: JSON.stringify({ type: 'BinarySerializer', results })
}
};