|
5 | 5 |
|
6 | 6 | import { browser } from '$app/environment'; |
7 | 7 | import { ImageMimeType, PdfMimeType } from '$lib/constants/supported-file-types'; |
| 8 | +import * as pdfjs from 'pdfjs-dist'; |
8 | 9 |
|
9 | | -// Types for PDF.js (imported conditionally) |
10 | 10 | type TextContent = { |
11 | 11 | items: Array<{ str: string }>; |
12 | 12 | }; |
13 | 13 |
|
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 | + }); |
41 | 22 | } |
42 | 23 |
|
43 | 24 | /** |
@@ -74,8 +55,6 @@ export async function convertPDFToText(file: File): Promise<string> { |
74 | 55 | } |
75 | 56 |
|
76 | 57 | try { |
77 | | - await initializePdfJs(); |
78 | | - |
79 | 58 | const buffer = await getFileAsBuffer(file); |
80 | 59 | const pdf = await pdfjs.getDocument(buffer).promise; |
81 | 60 | const numPages = pdf.numPages; |
@@ -111,8 +90,6 @@ export async function convertPDFToImage(file: File, scale: number = 1.5): Promis |
111 | 90 | } |
112 | 91 |
|
113 | 92 | try { |
114 | | - await initializePdfJs(); |
115 | | - |
116 | 93 | const buffer = await getFileAsBuffer(file); |
117 | 94 | const doc = await pdfjs.getDocument(buffer).promise; |
118 | 95 | const pages: Promise<string>[] = []; |
|
0 commit comments