|
1 | 1 | import { createRequire } from "node:module"; |
2 | | -import { dirname, join, sep } from "node:path"; |
| 2 | +import { dirname, join } from "node:path"; |
3 | 3 | import type { |
4 | 4 | DocumentExtractedImage, |
5 | 5 | DocumentExtractionRequest, |
@@ -56,24 +56,29 @@ const MAX_RENDER_DIMENSION = 10_000; |
56 | 56 |
|
57 | 57 | let canvasModulePromise: Promise<CanvasModule> | null = null; |
58 | 58 | let pdfJsModulePromise: Promise<PdfJsModule> | null = null; |
59 | | -let standardFontDataUrlCache: string | null = null; |
60 | | - |
61 | | -// pdf.js requires a filesystem path (with trailing separator) to resolve the |
62 | | -// fonts shipped under `pdfjs-dist/standard_fonts/`. Without it, any PDF that |
63 | | -// references the 14 standard fonts (Helvetica, Times, Courier, Symbol, |
64 | | -// ZapfDingbats) yields empty text plus a noisy `UnknownErrorException`. |
65 | | -// pdf.js silently rejects `file://` URLs on Node, so we pass a plain path. |
| 59 | +// `null` = not resolved yet, `false` = resolution failed once, `string` = resolved path. |
| 60 | +let standardFontDataUrlCache: string | false | null = null; |
| 61 | + |
| 62 | +// pdf.js requires a filesystem path that ends in `/` (it throws |
| 63 | +// `Invalid factory url ... must include trailing slash` from |
| 64 | +// `getFactoryUrlProp`). Without it, any PDF that references the 14 standard |
| 65 | +// fonts (Helvetica, Times, Courier, Symbol, ZapfDingbats) yields empty text |
| 66 | +// plus a noisy `UnknownErrorException`. pdf.js silently rejects `file://` |
| 67 | +// URLs on Node, so we pass a plain path. |
66 | 68 | function resolveStandardFontDataUrl(): string | undefined { |
| 69 | + if (standardFontDataUrlCache === false) { |
| 70 | + return undefined; |
| 71 | + } |
67 | 72 | if (standardFontDataUrlCache !== null) { |
68 | | - return standardFontDataUrlCache || undefined; |
| 73 | + return standardFontDataUrlCache; |
69 | 74 | } |
70 | 75 | try { |
71 | 76 | const requireFn = createRequire(import.meta.url); |
72 | 77 | const pkgPath = requireFn.resolve("pdfjs-dist/package.json"); |
73 | | - standardFontDataUrlCache = join(dirname(pkgPath), "standard_fonts") + sep; |
| 78 | + standardFontDataUrlCache = `${join(dirname(pkgPath), "standard_fonts")}/`; |
74 | 79 | return standardFontDataUrlCache; |
75 | 80 | } catch { |
76 | | - standardFontDataUrlCache = ""; |
| 81 | + standardFontDataUrlCache = false; |
77 | 82 | return undefined; |
78 | 83 | } |
79 | 84 | } |
|
0 commit comments