aboutsummaryrefslogtreecommitdiff
path: root/src/worker.ts
blob: 297aa77b8e4c30033a14306c80fa8a2f4428eb94 (plain) (blame)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/// <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 = (
    data: Blob | Buffer,
    type: DataResultType
): string | Promise<string> => {
    if (!(data instanceof Uint8Array)) {
        // blob
        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(data);
            } else {
                reader.readAsText(data);
            }
        });
    } else {
        // buffer
        if (type === "dataUrl") {
            return "data:image/png;base64," + data.toString("base64");
        } else {
            return data.toString("utf-8");
        }
    }
};

/**
 * @platform browser
 */
const fetchBlob = async (imgUrl: string): Promise<Blob> => {
    const r = await fetch(imgUrl, {
        cache: "no-cache",
    });
    return r.blob();
};

/**
 * @example
 * import { PDFWorker } from '../dist/cache/worker'
 * const { generatePDF } = PDFWorker()
 * const pdfData = await generatePDF(...)
 */
export const generatePDF = async (
    imgBlobs: Blob[] | Buffer[],
    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];

/**
 * @platform browser (web worker)
 */
if (typeof onmessage !== "undefined") {
    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]);
    };
}