-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathindex.js
43 lines (35 loc) · 1.27 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
import { renderVueToPdfStructure } from './vueRenderer.js'
import { renderPdfStructureToReactPdf, Font } from './reactPdfRenderer.js'
const pdf = (root, props) => {
const render = async (compress = true) => {
// STEP 1: Use Vue primitives (along with our custom logic in nodeOps) to render the Vue
// component tree into a data structure which react-pdf can understand.
const pdfStructure = renderVueToPdfStructure(root, props)
// OPTIONAL: Use the following log statement in case you need to debug the react-pdf
// object structure generated by the renderer:
//console.log('doc', pdfStructure)
// STEP 2: Pass the generated data structure object to react-pdf.
return renderPdfStructureToReactPdf(pdfStructure, compress)
}
const toBlob = async () => {
const chunks = []
const instance = await render()
return new Promise((resolve, reject) => {
instance.on('data', (chunk) => {
chunks.push(chunk instanceof Uint8Array ? chunk : new Uint8Array(chunk))
})
instance.on('end', () => {
try {
const blob = new Blob(chunks, { type: 'application/pdf' })
resolve(blob)
} catch (error) {
reject(error)
}
})
})
}
return {
toBlob,
}
}
export { Font, pdf }