/// // PDF.js for the PDF Viewer feature. The worker, the character maps, the standard fonts, and the WebAssembly image // decoders come from the app (`'self'`) as Vite assets, so a PDF never causes a network request. // Each open document has its own worker. `close` stops it. // The reference to the Vite types serves the unit test type check, which reaches this file through `loader.ts`. import { getDocument, PDFWorker, PasswordResponses, type PDFDocumentLoadingTask, type PDFDocumentProxy } from "pdfjs-dist/build/pdf.min.mjs"; import workerUrl from "pdfjs-dist/build/pdf.worker.min.mjs?url"; export { TextLayer, type PDFDocumentProxy, type RenderTask } from "pdfjs-dist/build/pdf.min.mjs"; // `build.assetsInlineLimit` in vite.config.js keeps these files out of the code, so each one is a file of the app. // The JPEG 2000 and JBIG2 decoders are the only WebAssembly files that PDF.js asks for with `useWorkerFetch: false`. // The viewer runs no PDF scripts (quickjs) and uses no ICC color profiles (qcms), as the PDF Viewer plugin did. const assets = import.meta.glob([ "/node_modules/pdfjs-dist/cmaps/*.bcmap", "/node_modules/pdfjs-dist/standard_fonts/*.{pfb,ttf}", "/node_modules/pdfjs-dist/wasm/{jbig2,openjpeg}.wasm", ], { query: "?url", import: "default", eager: true }); const folders: Record = { cMapUrl: "cmaps", standardFontDataUrl: "standard_fonts", wasmUrl: "wasm" }; /** The app URL of a PDF.js data file, or undefined for an unknown kind or file. */ export function assetUrl(kind: string, filename: string): string | undefined { const folder = folders[kind]; if (!folder || !/^[\w.-]+$/.test(filename)) return undefined; return assets[`/node_modules/pdfjs-dist/${folder}/${filename}`]; } /** PDF.js asks the window for its data files (`useWorkerFetch: false`). This class reads them from the app. */ class AppBinaryDataFactory { async fetch({ kind, filename }: { kind: string; filename: string }): Promise { const url = assetUrl(kind, filename); if (!url) throw new Error(`PDF.js has no data file ${filename}.`); const response = await fetch(url); if (!response.ok) throw new Error(`PDF.js could not read ${filename}: ${response.status}.`); return new Uint8Array(await response.arrayBuffer()); } } export interface OpenPdf { /** Resolves with the document after the password, if the file needs one. */ promise: Promise; /** Stop the load or release the document, and stop the worker. Later calls return the same promise. */ close(): Promise; } /** * Open the bytes of a PDF in a new worker. PDF.js calls `onPassword` for a protected file, with "incorrect" after a * wrong password. The viewer calls `answer` with the next password. */ export function openPdf(data: Uint8Array, onPassword: (reason: "needed" | "incorrect", answer: (password: string) => void) => void): OpenPdf { const port = new Worker(workerUrl, { type: "module", name: "PDF.js" }); const worker = PDFWorker.create({ port }); const task: PDFDocumentLoadingTask = getDocument({ data, worker, useWorkerFetch: false, BinaryDataFactory: AppBinaryDataFactory, useSystemFonts: false, isImageDecoderSupported: false, enableXfa: false, }); task.onPassword = (answer: (password: string) => void, reason: number) => { onPassword(reason === PasswordResponses.INCORRECT_PASSWORD ? "incorrect" : "needed", answer); }; let closing: Promise | undefined; return { promise: task.promise, close: () => closing ??= (async () => { try { await task.destroy(); } finally { worker.destroy(); port.terminate(); } })(), }; }