-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdf.js
51 lines (44 loc) · 1.64 KB
/
pdf.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
const puppeteer = require('puppeteer')
/**
* Starts a headless chrome instance to
* generate a PDF of the given URL.
*
* @param {string} url URL to generate a PDF from
*/
async function generate(url = null, contents = null, options = {}) {
const title = options.title || ''
const header = options.header || ''
const footer = options.footer || ''
const browser = await puppeteer.launch({
headless: true,
args: ['--no-sandbox', '--disable-setuid-sandbox']
})
const page = await browser.newPage()
if (url) {
await page.goto(url, { waitUntil: 'networkidle0' })
} else if (contents) {
await page.setContent(contents, { waitUntil: 'networkidle0' })
}
const defaultHeaderFooterStyles = `display: flex; justify-content: space-between; width: 100%; margin: 0 50px; font-size: 8px; font-family: Archivo,-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,'Helvetica Neue',Arial,'Noto Sans',sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol','Noto Color Emoji';`
const pdf = await page.pdf({
format: 'A4',
printBackground: true,
margin: {
top: options.marginTop || 180,
bottom: options.marginBottom || 40,
left: options.marginLeft || 0,
right: options.marginRight || 0,
},
displayHeaderFooter: true,
headerTemplate: header || `
<div style="${defaultHeaderFooterStyles}">
<div>${title}</div>
<div></div>
</div>
`,
footerTemplate: ' ',
})
await browser.close()
return pdf
}
exports.generate = generate