-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
39 lines (30 loc) · 928 Bytes
/
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
import fastifyPlugin from 'fastify-plugin'
import { launch } from 'puppeteer'
export async function pdfExport (options = {}) {
const {
launchOptions = { headless: 'new', ...options.launchOptions },
pdfUrl,
pdfOptions
} = options
try {
if (!pdfUrl) {
throw new Error('`pdfUrl` is required')
}
if (pdfOptions && pdfOptions.path) {
throw new Error('`pdfOptions.path` is not supported')
}
const browser = await launch(launchOptions)
const page = await browser.newPage()
await page.goto(pdfUrl)
const pdf = await page.pdf(pdfOptions)
await browser.close()
return { pdf }
} catch (error) {
return { error: `fastify-pdf-export: ${error.message}` }
}
}
const fastifyPdfExport = fastifyPlugin(async fastify => {
fastify.decorate('pdfExport', pdfExport)
}, { name: 'fastify-pdf-export' })
export { fastifyPdfExport }
export default fastifyPdfExport