musescore-downloader/src/worker.ts

92 lines
2.0 KiB
TypeScript
Raw Normal View History

/// <reference lib="webworker" />
2020-05-17 22:57:28 +00:00
import PDFDocument from 'pdfkit/lib/document'
import SVGtoPDF from 'svg-to-pdfkit'
2020-05-17 22:57:28 +00:00
type ImgType = 'svg' | 'png'
2021-01-27 17:26:32 +00:00
type DataResultType = 'dataUrl' | 'text'
const readData = (blob: Blob, type: DataResultType): Promise<string> => {
2020-05-17 22:57:28 +00:00
return new Promise((resolve, reject) => {
const reader = new FileReader()
reader.onload = (): void => {
const result = reader.result
resolve(result as string)
}
reader.onerror = reject
2021-01-27 17:26:32 +00:00
if (type === 'dataUrl') {
reader.readAsDataURL(blob)
} else {
reader.readAsText(blob)
}
2020-05-17 22:57:28 +00:00
})
}
2021-01-27 17:35:52 +00:00
const fetchBlob = async (imgUrl: string): Promise<Blob> => {
const r = await fetch(imgUrl, {
cache: 'no-cache',
})
2021-01-27 17:35:52 +00:00
return r.blob()
}
2021-01-27 17:35:52 +00:00
const generatePDF = async (imgBlobs: Blob[], imgType: ImgType, width: number, height: number): Promise<ArrayBuffer> => {
2020-05-17 22:57:28 +00:00
// @ts-ignore
const pdf = new (PDFDocument as typeof import('pdfkit'))({
// compress: true,
size: [width, height],
autoFirstPage: false,
margin: 0,
layout: 'portrait',
})
if (imgType === 'png') {
2021-01-27 17:35:52 +00:00
const imgDataUrlList: string[] = await Promise.all(imgBlobs.map(b => readData(b, 'dataUrl')))
2020-05-17 22:57:28 +00:00
imgDataUrlList.forEach((data) => {
pdf.addPage()
pdf.image(data, {
width,
height,
2020-05-17 22:57:28 +00:00
})
})
} else { // imgType == "svg"
2021-01-27 17:35:52 +00:00
const svgList = await Promise.all(imgBlobs.map(b => readData(b, 'text')))
2020-05-17 22:57:28 +00:00
svgList.forEach((svg) => {
pdf.addPage()
SVGtoPDF(pdf, svg, 0, 0, {
preserveAspectRatio: 'none',
})
})
}
2020-05-17 22:57:28 +00:00
// @ts-ignore
const buf: Uint8Array = await pdf.getBuffer()
return buf.buffer
}
export type PDFWorkerMessage = [string[], ImgType, number, number];
2020-05-17 22:57:28 +00:00
onmessage = async (e): Promise<void> => {
const [
2021-01-27 17:35:52 +00:00
imgUrls,
2020-05-17 22:57:28 +00:00
imgType,
width,
height,
] = e.data as PDFWorkerMessage
2021-01-27 17:35:52 +00:00
const imgBlobs = await Promise.all(imgUrls.map(url => fetchBlob(url)))
2020-05-17 22:57:28 +00:00
const pdfBuf = await generatePDF(
2021-01-27 17:35:52 +00:00
imgBlobs,
2020-05-17 22:57:28 +00:00
imgType,
width,
height,
)
postMessage(pdfBuf, [pdfBuf])
}