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-09-27 15:45:51 +00:00
|
|
|
const scriptUrlFromFunction = (fn: () => any): string => {
|
2020-05-17 22:57:28 +00:00
|
|
|
const blob = new Blob(['(' + fn.toString() + ')()'], { type: 'application/javascript' })
|
2020-12-01 19:10:36 +00:00
|
|
|
return window.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
|
|
|
}
|