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