2019-12-01 23:30:38 +00:00
|
|
|
|
2020-05-17 22:57:28 +00:00
|
|
|
import { PDFWorkerMessage } from './worker'
|
|
|
|
import { PDFWorker } from '../dist/cache/worker'
|
2019-12-01 23:30:38 +00:00
|
|
|
|
2020-05-17 22:57:28 +00:00
|
|
|
const scriptUrlFromFunction = (fn: Function): string => {
|
|
|
|
const blob = new Blob(['(' + fn.toString() + ')()'], { type: 'application/javascript' })
|
|
|
|
return URL.createObjectURL(blob)
|
2019-12-01 23:30:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class PDFWorkerHelper extends Worker {
|
2020-05-17 22:57:28 +00:00
|
|
|
constructor () {
|
|
|
|
const url = scriptUrlFromFunction(PDFWorker)
|
|
|
|
super(url)
|
|
|
|
}
|
2019-12-01 23:30:38 +00:00
|
|
|
|
2020-05-17 22:57:28 +00:00
|
|
|
generatePDF (imgURLs: string[], imgType: 'svg' | 'png', width: number, height: number): Promise<ArrayBuffer> {
|
|
|
|
const msg: PDFWorkerMessage = [
|
|
|
|
imgURLs,
|
|
|
|
imgType,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
]
|
|
|
|
this.postMessage(msg)
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
this.addEventListener('message', (e) => {
|
|
|
|
resolve(e.data)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2019-12-01 23:30:38 +00:00
|
|
|
}
|