-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (36 loc) · 1.05 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
const hb = require('handlebars')
const chromium = require('chrome-aws-lambda')
const Promise = require('bluebird')
module.exports.generatePdf = async (file, options, callback) => {
const args = [
'--no-sandbox',
'--headless',
'--disable-dev-shm-usage',
'--disable-gpu',
'--single-process',
'--no-zygote',
'--disable-setuid-sandbox',
]
const browser = await chromium.puppeteer.launch({
args,
executablePath: await chromium.executablePath,
headless: chromium.headless,
})
const page = await browser.newPage()
if (file.content) {
console.log('Compiling the template with handlebars')
const template = hb.compile(file.content, { strict: true })
const result = template(file.content)
await page.setContent(result)
} else {
await page.goto(file.url, {
waitUntil: 'networkidle0',
})
}
await page.waitForTimeout(2000)
return Promise.props(page.pdf(options))
.then(async (data) => {
await browser.close()
return Buffer.from(Object.values(data))
}).asCallback(callback)
}