Skip to content

Commit b95fe24

Browse files
committed
fix: Bundles PDF.js worker inline
Improves PDF processing by bundling the PDF.js worker inline.
1 parent 96870af commit b95fe24

File tree

2 files changed

+9
-32
lines changed

2 files changed

+9
-32
lines changed

tools/server/public/index.html.gz

294 KB
Binary file not shown.

tools/server/webui/src/lib/utils/pdf-processing.ts

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,20 @@
55

66
import { browser } from '$app/environment';
77
import { ImageMimeType, PdfMimeType } from '$lib/constants/supported-file-types';
8+
import * as pdfjs from 'pdfjs-dist';
89

9-
// Types for PDF.js (imported conditionally)
1010
type TextContent = {
1111
items: Array<{ str: string }>;
1212
};
1313

14-
type TextItem = {
15-
str: string;
16-
};
17-
18-
// PDF.js instance (loaded dynamically)
19-
let pdfjs: any = null;
20-
21-
/**
22-
* Initialize PDF.js only on the client side
23-
* Sets up the PDF.js library and worker for processing
24-
*/
25-
async function initializePdfJs() {
26-
if (!browser || pdfjs) return;
27-
28-
try {
29-
// Dynamic import to prevent SSR issues
30-
pdfjs = await import('pdfjs-dist');
31-
32-
// Set up PDF.js worker
33-
pdfjs.GlobalWorkerOptions.workerSrc = new URL(
34-
'pdfjs-dist/build/pdf.worker.min.mjs',
35-
import.meta.url
36-
).toString();
37-
} catch (error) {
38-
console.error('Failed to initialize PDF.js:', error);
39-
throw new Error('PDF.js is not available');
40-
}
14+
if (browser) {
15+
// Import worker as text and create blob URL for inline bundling
16+
import('pdfjs-dist/build/pdf.worker.min.mjs?raw').then((workerModule) => {
17+
const workerBlob = new Blob([workerModule.default], { type: 'application/javascript' });
18+
pdfjs.GlobalWorkerOptions.workerSrc = URL.createObjectURL(workerBlob);
19+
}).catch(() => {
20+
console.warn('Failed to load PDF.js worker, PDF processing may not work');
21+
});
4122
}
4223

4324
/**
@@ -74,8 +55,6 @@ export async function convertPDFToText(file: File): Promise<string> {
7455
}
7556

7657
try {
77-
await initializePdfJs();
78-
7958
const buffer = await getFileAsBuffer(file);
8059
const pdf = await pdfjs.getDocument(buffer).promise;
8160
const numPages = pdf.numPages;
@@ -111,8 +90,6 @@ export async function convertPDFToImage(file: File, scale: number = 1.5): Promis
11190
}
11291

11392
try {
114-
await initializePdfJs();
115-
11693
const buffer = await getFileAsBuffer(file);
11794
const doc = await pdfjs.getDocument(buffer).promise;
11895
const pages: Promise<string>[] = [];

0 commit comments

Comments
 (0)