///
///
///
import type {
getDocument,
GlobalWorkerOptions,
} from "./deno-pdfjs-dist-types/mod.d.ts";
declare global {
interface Window {
pdfjsLib: {
getDocument: typeof getDocument;
GlobalWorkerOptions: typeof GlobalWorkerOptions;
};
}
}
// load pdfjs
await new Promise((resolve, reject) => {
const script = document.createElement("script");
script.src =
"https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.13.216/pdf.min.js";
script.addEventListener("load", () => resolve());
script.addEventListener("error", reject);
document.body.append(script);
});
// initialize
window.pdfjsLib.GlobalWorkerOptions.workerSrc =
"https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.13.216/pdf.worker.min.js";
// upload file
const file = await new Promise((resolve, reject) => {
const input = document.createElement("input");
input.type = "file";
input.accept = "application/pdf,.pdf";
input.addEventListener("change", () => {
resolve(
input.files?.[0] ?? undefined,
);
});
input.addEventListener("error", reject);
document.body.append(input);
input.click();
input.remove();
});
if (file) {
const data = new Uint8Array(await file.arrayBuffer());
const pdf = await window.pdfjsLib.getDocument({
data,
cMapUrl:
"https://storage.googleapis.com/chrono-lexica/ci7lus-assets/pdfjs/cmaps/",
cMapPacked: true,
}).promise;
// browse
console.log(pdf);
}