export overridable config.pdfjsPath for bundled usage

This commit is contained in:
2026-05-24 19:59:52 -04:00
parent 832e3d37cc
commit d164d6f495
+10 -8
View File
@@ -1,20 +1,22 @@
const pdfjsPath = (path) => const _defaultPdfjsPath = (path) =>
new URL(`./vendor/pdfjs/${path}`, import.meta.url).toString(); new URL(`./vendor/pdfjs/${path}`, import.meta.url).toString();
export const config = {
pdfjsPath: _defaultPdfjsPath,
};
import "./vendor/pdfjs/pdf.mjs"; import "./vendor/pdfjs/pdf.mjs";
const pdfjsLib = globalThis.pdfjsLib; const pdfjsLib = globalThis.pdfjsLib;
pdfjsLib.GlobalWorkerOptions.workerSrc = pdfjsPath("pdf.worker.mjs"); pdfjsLib.GlobalWorkerOptions.workerSrc = _defaultPdfjsPath("pdf.worker.mjs");
const fetchText = async (url) => await (await fetch(url)).text(); const fetchText = async (url) => await (await fetch(url)).text();
// https://raw.githubusercontent.com/mozilla/pdf.js/refs/tags/v5.5.207/web/text_layer_builder.css
const textLayerBuilderCSS = await fetchText( const textLayerBuilderCSS = await fetchText(
pdfjsPath("text_layer_builder.css"), _defaultPdfjsPath("text_layer_builder.css"),
); );
// https://raw.githubusercontent.com/mozilla/pdf.js/refs/tags/v5.5.207/web/annotation_layer_builder.css
const annotationLayerBuilderCSS = await fetchText( const annotationLayerBuilderCSS = await fetchText(
pdfjsPath("annotation_layer_builder.css"), _defaultPdfjsPath("annotation_layer_builder.css"),
); );
const render = async (page, doc, zoom) => { const render = async (page, doc, zoom) => {
@@ -130,8 +132,8 @@ const makeTOCItem = (item) => ({
}); });
export const makePDF = async (file, options = {}) => { export const makePDF = async (file, options = {}) => {
const cMapUrl = options.cMapUrl ?? pdfjsPath("cmaps/"); const cMapUrl = options.cMapUrl ?? config.pdfjsPath("cmaps/");
const standardFontDataUrl = options.standardFontDataUrl ?? pdfjsPath("standard_fonts/"); const standardFontDataUrl = options.standardFontDataUrl ?? config.pdfjsPath("standard_fonts/");
if (options.workerSrc) { if (options.workerSrc) {
pdfjsLib.GlobalWorkerOptions.workerSrc = options.workerSrc; pdfjsLib.GlobalWorkerOptions.workerSrc = options.workerSrc;
} }