From 1b9cf8ebb0794af62fbc31fbb6c597c5943d0eb9 Mon Sep 17 00:00:00 2001 From: Holger Huo <50446405+HolgerHuo@users.noreply.github.com> Date: Wed, 16 Sep 2026 18:49:58 +0800 Subject: [PATCH] fix(pdfjs): cMaps not correctly bundled and served --- web/src/lib/pdf.test.ts | 34 ++++++++++++++++++++++++++++++++++ web/src/lib/pdf.ts | 4 +++- web/vite.config.ts | 40 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 web/src/lib/pdf.test.ts diff --git a/web/src/lib/pdf.test.ts b/web/src/lib/pdf.test.ts new file mode 100644 index 0000000..44e2842 --- /dev/null +++ b/web/src/lib/pdf.test.ts @@ -0,0 +1,34 @@ +// @vitest-environment jsdom + +import { beforeEach, describe, expect, it, vi } from "vitest"; + +const { getDocument } = vi.hoisted(() => ({ + getDocument: vi.fn(), +})); + +vi.mock("pdfjs-dist", () => ({ + GlobalWorkerOptions: { workerSrc: "" }, + getDocument, +})); + +import { renderPdfBytesToCanvas } from "@/lib/pdf"; + +describe("PDF rendering", () => { + beforeEach(() => { + getDocument.mockReset(); + getDocument.mockReturnValue({ + promise: Promise.resolve({ numPages: 0 }), + destroy: vi.fn().mockResolvedValue(undefined), + }); + }); + + it("configures packed PDF.js CMaps using the application base URL", async () => { + await renderPdfBytesToCanvas(document.createElement("div"), new Uint8Array([1, 2, 3])); + + expect(getDocument).toHaveBeenCalledWith({ + data: new Uint8Array([1, 2, 3]).buffer, + cMapUrl: "/pdfjs/cmaps/", + cMapPacked: true, + }); + }); +}); diff --git a/web/src/lib/pdf.ts b/web/src/lib/pdf.ts index f477698..55d1c3a 100644 --- a/web/src/lib/pdf.ts +++ b/web/src/lib/pdf.ts @@ -20,7 +20,9 @@ export async function renderPdfBytesToCanvas( const isStale = () => containerRenderToken.get(container) !== renderToken; ensurePdfWorkerConfigured(); const loadingTask = getDocument({ - data: pdfBytes.slice().buffer + data: pdfBytes.slice().buffer, + cMapUrl: `${import.meta.env.BASE_URL}pdfjs/cmaps/`, + cMapPacked: true }); try { const pdf = await loadingTask.promise; diff --git a/web/vite.config.ts b/web/vite.config.ts index 6008b1c..6514924 100644 --- a/web/vite.config.ts +++ b/web/vite.config.ts @@ -21,6 +21,44 @@ const latexIncluded = webTarget === "core" && distribution.latexEnabled; const aiRuntimeBuildId = computeAiRuntimeBuildId(); let distributionOutputDir = path.resolve(__dirname, "dist"); +const pdfjsCMapsDir = path.resolve(__dirname, "node_modules", "pdfjs-dist", "cmaps"); +const pdfjsAssetsPlugin = { + name: "toss-pdfjs-assets", + configureServer(server: { + middlewares: { + use: ( + handler: ( + request: { url?: string }, + response: { statusCode: number; setHeader: (name: string, value: string) => void; end: (body?: Buffer) => void }, + next: () => void, + ) => void, + ) => void; + }; + }) { + server.middlewares.use((request, response, next) => { + const match = request.url?.match(/\/pdfjs\/cmaps\/([A-Za-z0-9._-]+)(?:\?.*)?$/); + if (!match) { + next(); + return; + } + const filePath = path.resolve(pdfjsCMapsDir, match[1]); + if (!fs.existsSync(filePath) || !fs.statSync(filePath).isFile()) { + response.statusCode = 404; + response.end(); + return; + } + response.setHeader("Content-Type", "application/octet-stream"); + response.end(fs.readFileSync(filePath)); + }); + }, + closeBundle() { + fs.mkdirSync(path.resolve(distributionOutputDir, "pdfjs"), { recursive: true }); + fs.cpSync(pdfjsCMapsDir, path.resolve(distributionOutputDir, "pdfjs", "cmaps"), { + recursive: true, + }); + }, +}; + const distributionAssetsPlugin = { name: "toss-distribution-assets", apply: "build" as const, @@ -145,7 +183,7 @@ const browserHtmlPlugin = { export default defineConfig({ base, - plugins: [react(), wasm(), browserHtmlPlugin, distributionAssetsPlugin], + plugins: [react(), wasm(), browserHtmlPlugin, distributionAssetsPlugin, pdfjsAssetsPlugin], define: { __TOSS_BUILD_PROJECT_TYPES__: JSON.stringify(distribution.projectTypes), __TOSS_BUILD_FRONTEND_FEATURES__: JSON.stringify(distribution.frontendFeatures),