///
///
///
import type {
getDocument,
GlobalWorkerOptions,
} from "https://raw.githubusercontent.com/takker99/deno-pdfjs-dist-types/0.1.0/mod.d.ts";
declare global {
interface Window {
pdfjsLib?: {
getDocument: typeof getDocument;
GlobalWorkerOptions: typeof GlobalWorkerOptions;
};
}
}
export type PDFjsLib = Required["pdfjsLib"];
const ensurePDFjsLib = (): Promise => new Promise((resolve) => {
if (window.pdfjsLib) resolve(window.pdfjsLib);
const timer = setInterval(() => {
if (!window.pdfjsLib) return;
clearInterval(timer);
resolve(window.pdfjsLib);
}, 1000);
});
export const load = async (version: string): Promise => {
const src = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${version}/pdf.min.js`;
if (!document.querySelector(`script[src="${src}"]`)) {
const script = document.createElement("script");
script.src = src;
await new Promise((resolve, reject) => {
script.addEventListener("load", () => resolve());
script.addEventListener("error", reject);
document.body.append(script);
});
const pdfjsLib = await ensurePDFjsLib();
pdfjsLib.GlobalWorkerOptions.workerSrc =
`//cdnjs.cloudflare.com/ajax/libs/pdf.js/${version}/pdf.worker.min.js`;
return pdfjsLib;
}
return await ensurePDFjsLib();
};