svelte-pdf-view 0.1.2 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -0
- package/dist/PdfRenderer.svelte +8 -20
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/pdf-viewer/PDFPageView.js +14 -3
- package/dist/pdf-viewer/context.d.ts +0 -18
- package/dist/pdf-viewer/context.js +1 -34
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -32,6 +32,22 @@ If you want to use the default `<PdfToolbar>` component, also install:
|
|
|
32
32
|
npm install @lucide/svelte
|
|
33
33
|
```
|
|
34
34
|
|
|
35
|
+
### Vite Configuration
|
|
36
|
+
|
|
37
|
+
If you're using Vite (including SvelteKit), you may need to exclude `pdfjs-dist` from dependency optimization to ensure the PDF.js worker loads correctly:
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
// vite.config.ts
|
|
41
|
+
export default defineConfig({
|
|
42
|
+
// ... other config
|
|
43
|
+
optimizeDeps: {
|
|
44
|
+
exclude: ['pdfjs-dist']
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
This is especially important in monorepo setups where Vite's optimizer may incorrectly bundle the worker.
|
|
50
|
+
|
|
35
51
|
## Quick Start
|
|
36
52
|
|
|
37
53
|
### Basic Usage
|
package/dist/PdfRenderer.svelte
CHANGED
|
@@ -1,11 +1,7 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
2
|
import { BROWSER } from 'esm-env';
|
|
3
3
|
import { onDestroy, onMount } from 'svelte';
|
|
4
|
-
import {
|
|
5
|
-
getPdfViewerContext,
|
|
6
|
-
getPdfWorkerContext,
|
|
7
|
-
type PdfViewerActions
|
|
8
|
-
} from './pdf-viewer/context.js';
|
|
4
|
+
import { getPdfViewerContext, type PdfViewerActions } from './pdf-viewer/context.js';
|
|
9
5
|
import { rendererStyles } from './pdf-viewer/renderer-styles.js';
|
|
10
6
|
|
|
11
7
|
/** PDF source - can be a URL string, ArrayBuffer, Uint8Array, or Blob */
|
|
@@ -45,9 +41,6 @@
|
|
|
45
41
|
let scrollContainerEl: HTMLDivElement | null = null;
|
|
46
42
|
let mounted = $state(false);
|
|
47
43
|
|
|
48
|
-
// Get worker from context (if set via initPdfWorker)
|
|
49
|
-
const workerContext = getPdfWorkerContext();
|
|
50
|
-
|
|
51
44
|
// Core instances
|
|
52
45
|
let viewer: import('./pdf-viewer/PDFViewerCore.js').PDFViewerCore | null = null;
|
|
53
46
|
let findController: import('./pdf-viewer/FindController.js').FindController | null = null;
|
|
@@ -58,18 +51,13 @@
|
|
|
58
51
|
|
|
59
52
|
pdfjsLib = await import('pdfjs-dist/legacy/build/pdf.mjs');
|
|
60
53
|
|
|
61
|
-
//
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
type: 'module'
|
|
69
|
-
}) as unknown as null
|
|
70
|
-
});
|
|
71
|
-
pdfjsLib.GlobalWorkerOptions.workerPort = worker.port;
|
|
72
|
-
}
|
|
54
|
+
// Create worker using import.meta.url for proper bundler resolution
|
|
55
|
+
const worker = new pdfjsLib.PDFWorker({
|
|
56
|
+
port: new Worker(new URL('pdfjs-dist/legacy/build/pdf.worker.mjs', import.meta.url), {
|
|
57
|
+
type: 'module'
|
|
58
|
+
}) as unknown as null
|
|
59
|
+
});
|
|
60
|
+
pdfjsLib.GlobalWorkerOptions.workerPort = worker.port;
|
|
73
61
|
|
|
74
62
|
return pdfjsLib;
|
|
75
63
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export { default as PdfViewer, Toolbar as PdfToolbar, Renderer as PdfRenderer } from './PdfViewer.svelte';
|
|
2
2
|
export type { PdfSource } from './PdfRenderer.svelte';
|
|
3
|
-
export { getPdfViewerContext,
|
|
3
|
+
export { getPdfViewerContext, type PdfViewerState, type PdfViewerActions, type PdfViewerContext } from './pdf-viewer/context.js';
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
// Reexport your entry components here
|
|
2
2
|
export { default as PdfViewer, Toolbar as PdfToolbar, Renderer as PdfRenderer } from './PdfViewer.svelte';
|
|
3
3
|
// Export context for custom toolbars
|
|
4
|
-
export { getPdfViewerContext
|
|
4
|
+
export { getPdfViewerContext } from './pdf-viewer/context.js';
|
|
@@ -12,7 +12,14 @@
|
|
|
12
12
|
* See the License for the specific language governing permissions and
|
|
13
13
|
* limitations under the License.
|
|
14
14
|
*/
|
|
15
|
-
|
|
15
|
+
// Dynamically loaded pdfjs utilities
|
|
16
|
+
let setLayerDimensions;
|
|
17
|
+
async function ensurePdfJsLoaded() {
|
|
18
|
+
if (!setLayerDimensions) {
|
|
19
|
+
const pdfjs = await import('pdfjs-dist/legacy/build/pdf.mjs');
|
|
20
|
+
setLayerDimensions = pdfjs.setLayerDimensions;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
16
23
|
export const RenderingStates = {
|
|
17
24
|
INITIAL: 0,
|
|
18
25
|
RUNNING: 1,
|
|
@@ -61,7 +68,7 @@ export class PDFPageView {
|
|
|
61
68
|
this.loadingDiv.textContent = 'Loading...';
|
|
62
69
|
this.div.appendChild(this.loadingDiv);
|
|
63
70
|
}
|
|
64
|
-
setDimensions() {
|
|
71
|
+
async setDimensions() {
|
|
65
72
|
const { width, height } = this.viewport;
|
|
66
73
|
this.div.style.width = `${Math.floor(width)}px`;
|
|
67
74
|
this.div.style.height = `${Math.floor(height)}px`;
|
|
@@ -75,6 +82,7 @@ export class PDFPageView {
|
|
|
75
82
|
// mustFlip=false because the text layer uses raw page coordinates
|
|
76
83
|
// and rotation is handled via CSS transforms
|
|
77
84
|
if (this.textLayerDiv) {
|
|
85
|
+
await ensurePdfJsLoaded();
|
|
78
86
|
setLayerDimensions(this.textLayerDiv, this.viewport, /* mustFlip */ false);
|
|
79
87
|
}
|
|
80
88
|
}
|
|
@@ -227,7 +235,10 @@ export class PDFPageView {
|
|
|
227
235
|
this.div.appendChild(this.textLayerDiv);
|
|
228
236
|
try {
|
|
229
237
|
// Import TextLayer from pdfjs-dist
|
|
230
|
-
const { TextLayer } = await
|
|
238
|
+
const [{ TextLayer }] = await Promise.all([
|
|
239
|
+
import('pdfjs-dist/legacy/build/pdf.mjs'),
|
|
240
|
+
ensurePdfJsLoaded()
|
|
241
|
+
]);
|
|
231
242
|
const textContent = await this.pdfPage.getTextContent();
|
|
232
243
|
this.textDivs = [];
|
|
233
244
|
this.textContentItemsStr = [];
|
|
@@ -29,21 +29,3 @@ export interface PdfViewerContext {
|
|
|
29
29
|
}
|
|
30
30
|
export declare function setPdfViewerContext(ctx: PdfViewerContext): void;
|
|
31
31
|
export declare function getPdfViewerContext(): PdfViewerContext;
|
|
32
|
-
export interface PdfWorkerContext {
|
|
33
|
-
worker: import('pdfjs-dist/legacy/build/pdf.mjs').PDFWorker;
|
|
34
|
-
}
|
|
35
|
-
export declare function setPdfWorkerContext(worker: PdfWorkerContext['worker']): void;
|
|
36
|
-
export declare function getPdfWorkerContext(): PdfWorkerContext | undefined;
|
|
37
|
-
/**
|
|
38
|
-
* Initialize PDF.js worker and set it in context.
|
|
39
|
-
* Call this in your root layout or page component.
|
|
40
|
-
*
|
|
41
|
-
* @example
|
|
42
|
-
* ```svelte
|
|
43
|
-
* <script>
|
|
44
|
-
* import { initPdfWorker } from 'svelte-pdf-view';
|
|
45
|
-
* initPdfWorker();
|
|
46
|
-
* </script>
|
|
47
|
-
* ```
|
|
48
|
-
*/
|
|
49
|
-
export declare function initPdfWorker(): void;
|
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* PDF Viewer Context - Shared state between toolbar and renderer
|
|
3
3
|
*/
|
|
4
|
-
import {
|
|
5
|
-
import { getContext, setContext, onDestroy } from 'svelte';
|
|
4
|
+
import { getContext, setContext } from 'svelte';
|
|
6
5
|
const PDF_VIEWER_CONTEXT_KEY = Symbol('pdf-viewer');
|
|
7
|
-
const PDF_WORKER_CONTEXT_KEY = Symbol('pdf-worker');
|
|
8
6
|
export function setPdfViewerContext(ctx) {
|
|
9
7
|
setContext(PDF_VIEWER_CONTEXT_KEY, ctx);
|
|
10
8
|
}
|
|
@@ -15,34 +13,3 @@ export function getPdfViewerContext() {
|
|
|
15
13
|
}
|
|
16
14
|
return ctx;
|
|
17
15
|
}
|
|
18
|
-
export function setPdfWorkerContext(worker) {
|
|
19
|
-
setContext(PDF_WORKER_CONTEXT_KEY, { worker });
|
|
20
|
-
}
|
|
21
|
-
export function getPdfWorkerContext() {
|
|
22
|
-
return getContext(PDF_WORKER_CONTEXT_KEY);
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* Initialize PDF.js worker and set it in context.
|
|
26
|
-
* Call this in your root layout or page component.
|
|
27
|
-
*
|
|
28
|
-
* @example
|
|
29
|
-
* ```svelte
|
|
30
|
-
* <script>
|
|
31
|
-
* import { initPdfWorker } from 'svelte-pdf-view';
|
|
32
|
-
* initPdfWorker();
|
|
33
|
-
* </script>
|
|
34
|
-
* ```
|
|
35
|
-
*/
|
|
36
|
-
export function initPdfWorker() {
|
|
37
|
-
if (!BROWSER)
|
|
38
|
-
return;
|
|
39
|
-
import('pdfjs-dist/legacy/build/pdf.mjs').then((pdfjs) => {
|
|
40
|
-
const worker = new pdfjs.PDFWorker({
|
|
41
|
-
port: new Worker(new URL('pdfjs-dist/legacy/build/pdf.worker.mjs', import.meta.url), {
|
|
42
|
-
type: 'module'
|
|
43
|
-
})
|
|
44
|
-
});
|
|
45
|
-
setPdfWorkerContext(worker);
|
|
46
|
-
onDestroy(() => worker.destroy());
|
|
47
|
-
});
|
|
48
|
-
}
|