forked from Xmader/musescore-downloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.ts
91 lines (74 loc) · 2.01 KB
/
worker.ts
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/// <reference lib="webworker" />
import PDFDocument from 'pdfkit/lib/document'
import SVGtoPDF from 'svg-to-pdfkit'
type ImgType = 'svg' | 'png'
type DataResultType = 'dataUrl' | 'text'
const readData = (blob: Blob, type: DataResultType): Promise<string> => {
return new Promise((resolve, reject) => {
const reader = new FileReader()
reader.onload = (): void => {
const result = reader.result
resolve(result as string)
}
reader.onerror = reject
if (type === 'dataUrl') {
reader.readAsDataURL(blob)
} else {
reader.readAsText(blob)
}
})
}
const fetchBlob = async (imgUrl: string): Promise<Blob> => {
const r = await fetch(imgUrl, {
cache: 'no-cache',
})
return r.blob()
}
const generatePDF = async (imgBlobs: Blob[], imgType: ImgType, width: number, height: number): Promise<ArrayBuffer> => {
// @ts-ignore
const pdf = new (PDFDocument as typeof import('pdfkit'))({
// compress: true,
size: [width, height],
autoFirstPage: false,
margin: 0,
layout: 'portrait',
})
if (imgType === 'png') {
const imgDataUrlList: string[] = await Promise.all(imgBlobs.map(b => readData(b, 'dataUrl')))
imgDataUrlList.forEach((data) => {
pdf.addPage()
pdf.image(data, {
width,
height,
})
})
} else { // imgType == "svg"
const svgList = await Promise.all(imgBlobs.map(b => readData(b, 'text')))
svgList.forEach((svg) => {
pdf.addPage()
SVGtoPDF(pdf, svg, 0, 0, {
preserveAspectRatio: 'none',
})
})
}
// @ts-ignore
const buf: Uint8Array = await pdf.getBuffer()
return buf.buffer
}
export type PDFWorkerMessage = [string[], ImgType, number, number];
onmessage = async (e): Promise<void> => {
const [
imgUrls,
imgType,
width,
height,
] = e.data as PDFWorkerMessage
const imgBlobs = await Promise.all(imgUrls.map(url => fetchBlob(url)))
const pdfBuf = await generatePDF(
imgBlobs,
imgType,
width,
height,
)
postMessage(pdfBuf, [pdfBuf])
}