Allow explicit PDF.js resource paths for bundler compatibility

Bundlers like Vite cannot resolve directory paths (cmaps/, standard_fonts/)
through the new URL(..., import.meta.url) pattern. Add optional config to
makePDF, makeBook, and View.open so consumers can provide explicit URLs.

When options are not provided, falls back to import.meta.url resolution
(preserving existing behavior for direct module usage).
This commit is contained in:
2026-05-09 21:03:39 -04:00
parent c38486c296
commit 74c317d58c
2 changed files with 12 additions and 7 deletions
+8 -3
View File
@@ -129,7 +129,12 @@ const makeTOCItem = (item) => ({
subitems: item.items.length ? item.items.map(makeTOCItem) : null,
});
export const makePDF = async (file) => {
export const makePDF = async (file, options = {}) => {
const cMapUrl = options.cMapUrl ?? pdfjsPath("cmaps/");
const standardFontDataUrl = options.standardFontDataUrl ?? pdfjsPath("standard_fonts/");
if (options.workerSrc) {
pdfjsLib.GlobalWorkerOptions.workerSrc = options.workerSrc;
}
const transport = new pdfjsLib.PDFDataRangeTransport(file.size, []);
transport.requestDataRange = (begin, end) => {
file
@@ -141,8 +146,8 @@ export const makePDF = async (file) => {
};
const pdf = await pdfjsLib.getDocument({
range: transport,
cMapUrl: pdfjsPath("cmaps/"),
standardFontDataUrl: pdfjsPath("standard_fonts/"),
cMapUrl,
standardFontDataUrl,
isEvalSupported: false,
}).promise;