svelte-pdf-view 0.1.1 → 0.1.3
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/dist/PdfRenderer.svelte
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
|
+
import { BROWSER } from 'esm-env';
|
|
2
3
|
import { onDestroy, onMount } from 'svelte';
|
|
3
|
-
|
|
4
|
-
const browser = typeof window !== 'undefined';
|
|
5
4
|
import { getPdfViewerContext, type PdfViewerActions } from './pdf-viewer/context.js';
|
|
6
|
-
import rendererStyles from './pdf-viewer/renderer-styles.
|
|
5
|
+
import { rendererStyles } from './pdf-viewer/renderer-styles.js';
|
|
7
6
|
|
|
8
7
|
/** PDF source - can be a URL string, ArrayBuffer, Uint8Array, or Blob */
|
|
9
8
|
export type PdfSource = string | ArrayBuffer | Uint8Array | Blob;
|
|
@@ -48,17 +47,23 @@
|
|
|
48
47
|
let pdfjsLib: typeof import('pdfjs-dist/legacy/build/pdf.mjs') | null = null;
|
|
49
48
|
|
|
50
49
|
async function initPdfJs() {
|
|
51
|
-
if (!
|
|
50
|
+
if (!BROWSER) return null;
|
|
52
51
|
|
|
53
52
|
pdfjsLib = await import('pdfjs-dist/legacy/build/pdf.mjs');
|
|
54
|
-
|
|
55
|
-
|
|
53
|
+
|
|
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;
|
|
56
61
|
|
|
57
62
|
return pdfjsLib;
|
|
58
63
|
}
|
|
59
64
|
|
|
60
65
|
async function loadPdf(source: PdfSource) {
|
|
61
|
-
if (!
|
|
66
|
+
if (!BROWSER || !scrollContainerEl) return;
|
|
62
67
|
|
|
63
68
|
viewerState.loading = true;
|
|
64
69
|
viewerState.error = null;
|
|
@@ -176,7 +181,7 @@
|
|
|
176
181
|
};
|
|
177
182
|
|
|
178
183
|
onMount(async () => {
|
|
179
|
-
if (
|
|
184
|
+
if (BROWSER && hostEl) {
|
|
180
185
|
// Create shadow root for style isolation
|
|
181
186
|
shadowRoot = hostEl.attachShadow({ mode: 'open' });
|
|
182
187
|
|
|
@@ -212,7 +217,7 @@
|
|
|
212
217
|
|
|
213
218
|
// Load PDF when src changes
|
|
214
219
|
$effect(() => {
|
|
215
|
-
if (
|
|
220
|
+
if (BROWSER && src && scrollContainerEl && mounted) {
|
|
216
221
|
loadPdf(src);
|
|
217
222
|
}
|
|
218
223
|
});
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
|
+
import { BROWSER } from 'esm-env';
|
|
2
3
|
import { onDestroy, onMount } from 'svelte';
|
|
3
|
-
|
|
4
|
-
const browser = typeof window !== 'undefined';
|
|
5
4
|
import {
|
|
6
5
|
ZoomIn,
|
|
7
6
|
ZoomOut,
|
|
@@ -46,17 +45,23 @@
|
|
|
46
45
|
let pdfjsLib: typeof import('pdfjs-dist/legacy/build/pdf.mjs') | null = null;
|
|
47
46
|
|
|
48
47
|
async function initPdfJs() {
|
|
49
|
-
if (!
|
|
48
|
+
if (!BROWSER) return null;
|
|
50
49
|
|
|
51
50
|
pdfjsLib = await import('pdfjs-dist/legacy/build/pdf.mjs');
|
|
52
|
-
|
|
53
|
-
|
|
51
|
+
|
|
52
|
+
// Create worker using import.meta.url
|
|
53
|
+
const worker = new pdfjsLib.PDFWorker({
|
|
54
|
+
port: new Worker(new URL('pdfjs-dist/legacy/build/pdf.worker.mjs', import.meta.url), {
|
|
55
|
+
type: 'module'
|
|
56
|
+
}) as unknown as null
|
|
57
|
+
});
|
|
58
|
+
pdfjsLib.GlobalWorkerOptions.workerPort = worker.port;
|
|
54
59
|
|
|
55
60
|
return pdfjsLib;
|
|
56
61
|
}
|
|
57
62
|
|
|
58
63
|
async function loadPdf(url: string) {
|
|
59
|
-
if (!
|
|
64
|
+
if (!BROWSER || !scrollContainerEl) return;
|
|
60
65
|
|
|
61
66
|
loading = true;
|
|
62
67
|
error = null;
|
|
@@ -203,7 +208,7 @@
|
|
|
203
208
|
|
|
204
209
|
// Load PDF when src changes
|
|
205
210
|
$effect(() => {
|
|
206
|
-
if (
|
|
211
|
+
if (BROWSER && src && scrollContainerEl && mounted) {
|
|
207
212
|
loadPdf(src);
|
|
208
213
|
}
|
|
209
214
|
});
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PDF Renderer Styles - Shadow DOM Isolated
|
|
3
|
+
* This is a derivative work based on PDF.js text_layer_builder.css
|
|
4
|
+
*/
|
|
5
|
+
export declare const rendererStyles = "\n/* CSS Custom Properties with defaults */\n.pdf-renderer-container {\n\t--pdf-background-color: #e8e8e8;\n\t--pdf-page-shadow: 0 2px 8px rgba(0, 0, 0, 0.12), 0 1px 3px rgba(0, 0, 0, 0.08);\n\t--pdf-scrollbar-track-color: #f1f1f1;\n\t--pdf-scrollbar-thumb-color: #c1c1c1;\n\t--pdf-scrollbar-thumb-hover-color: #a1a1a1;\n\t--pdf-scrollbar-width: 10px;\n\n\tdisplay: flex;\n\tflex-direction: column;\n\twidth: 100%;\n\theight: 100%;\n\tbackground-color: var(--pdf-background-color);\n\toverflow: hidden;\n}\n\n/* Scroll container */\n.pdf-scroll-container {\n\tflex: 1;\n\toverflow: auto;\n\tposition: relative;\n\tbackground-color: var(--pdf-background-color);\n}\n\n/* Custom scrollbar styling */\n.pdf-scroll-container::-webkit-scrollbar {\n\twidth: var(--pdf-scrollbar-width);\n\theight: var(--pdf-scrollbar-width);\n}\n\n.pdf-scroll-container::-webkit-scrollbar-track {\n\tbackground: var(--pdf-scrollbar-track-color);\n\tborder-radius: calc(var(--pdf-scrollbar-width) / 2);\n}\n\n.pdf-scroll-container::-webkit-scrollbar-thumb {\n\tbackground: var(--pdf-scrollbar-thumb-color);\n\tborder-radius: calc(var(--pdf-scrollbar-width) / 2);\n}\n\n.pdf-scroll-container::-webkit-scrollbar-thumb:hover {\n\tbackground: var(--pdf-scrollbar-thumb-hover-color);\n}\n\n/* Firefox scrollbar */\n.pdf-scroll-container {\n\tscrollbar-width: thin;\n\tscrollbar-color: var(--pdf-scrollbar-thumb-color) var(--pdf-scrollbar-track-color);\n}\n\n/* Viewer - dynamically created */\n.pdfViewer {\n\tdisplay: flex;\n\tflex-direction: column;\n\talign-items: center;\n\tpadding: 20px;\n\tgap: 16px;\n}\n\n/* Page - dynamically created with CSS variables for text layer */\n.page {\n\t--user-unit: 1;\n\t--total-scale-factor: calc(var(--scale-factor, 1) * var(--user-unit));\n\t--scale-round-x: 1px;\n\t--scale-round-y: 1px;\n\n\tposition: relative;\n\tbackground-color: white;\n\tbox-shadow: var(--pdf-page-shadow);\n\tborder-radius: 2px;\n\tmargin: 0;\n\tdirection: ltr;\n}\n\n.page .loadingIcon {\n\tposition: absolute;\n\ttop: 50%;\n\tleft: 50%;\n\ttransform: translate(-50%, -50%);\n\tcolor: #666;\n\tfont-size: 14px;\n}\n\n.page .canvasWrapper {\n\tposition: absolute;\n\tinset: 0;\n\toverflow: hidden;\n\tz-index: 0;\n}\n\n.page .pdf-canvas {\n\tdisplay: block;\n}\n\n/* Text layer - essential styles from PDF.js */\n.textLayer {\n\tposition: absolute;\n\ttext-align: initial;\n\tinset: 0;\n\toverflow: clip;\n\topacity: 1;\n\tline-height: 1;\n\t-webkit-text-size-adjust: none;\n\t-moz-text-size-adjust: none;\n\ttext-size-adjust: none;\n\tforced-color-adjust: none;\n\ttransform-origin: 0 0;\n\tcaret-color: CanvasText;\n\tz-index: 2;\n}\n\n/* Text layer rotation transforms */\n.textLayer[data-main-rotation='90'] {\n\ttransform: rotate(90deg) translateY(-100%);\n}\n\n.textLayer[data-main-rotation='180'] {\n\ttransform: rotate(180deg) translate(-100%, -100%);\n}\n\n.textLayer[data-main-rotation='270'] {\n\ttransform: rotate(270deg) translateX(-100%);\n}\n\n.textLayer :is(span, br) {\n\tcolor: transparent;\n\tposition: absolute;\n\twhite-space: pre;\n\tcursor: text;\n\ttransform-origin: 0% 0%;\n}\n\n.textLayer > :not(.markedContent),\n.textLayer .markedContent span:not(.markedContent) {\n\tz-index: 1;\n}\n\n.textLayer span.markedContent {\n\ttop: 0;\n\theight: 0;\n}\n\n.textLayer ::-moz-selection {\n\tbackground: rgba(0, 0, 255, 0.25);\n}\n\n.textLayer ::selection {\n\tbackground: rgba(0, 0, 255, 0.25);\n}\n\n.textLayer br::-moz-selection,\n.textLayer br::selection {\n\tbackground: transparent;\n}\n\n/* Search highlights */\n.textLayer .highlight {\n\tmargin: -1px;\n\tpadding: 1px;\n\tbackground-color: rgba(255, 255, 0, 0.4);\n\tborder-radius: 4px;\n}\n\n.textLayer .highlight.appended {\n\tposition: initial;\n}\n\n.textLayer .highlight.selected {\n\tbackground-color: rgba(255, 128, 0, 0.6);\n}\n\n.textLayer .highlight.begin {\n\tborder-radius: 4px 0 0 4px;\n}\n\n.textLayer .highlight.end {\n\tborder-radius: 0 4px 4px 0;\n}\n\n.textLayer .highlight.middle {\n\tborder-radius: 0;\n}\n";
|
|
@@ -12,12 +12,11 @@
|
|
|
12
12
|
* See the License for the specific language governing permissions and
|
|
13
13
|
* limitations under the License.
|
|
14
14
|
*/
|
|
15
|
-
|
|
16
|
-
/*
|
|
15
|
+
/**
|
|
17
16
|
* PDF Renderer Styles - Shadow DOM Isolated
|
|
18
17
|
* This is a derivative work based on PDF.js text_layer_builder.css
|
|
19
18
|
*/
|
|
20
|
-
|
|
19
|
+
export const rendererStyles = `
|
|
21
20
|
/* CSS Custom Properties with defaults */
|
|
22
21
|
.pdf-renderer-container {
|
|
23
22
|
--pdf-background-color: #e8e8e8;
|
|
@@ -201,3 +200,4 @@
|
|
|
201
200
|
.textLayer .highlight.middle {
|
|
202
201
|
border-radius: 0;
|
|
203
202
|
}
|
|
203
|
+
`;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "svelte-pdf-view",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "A modern, modular PDF viewer component for Svelte 5. Built on PDF.js with TypeScript support",
|
|
5
5
|
"author": "Louis Li",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -84,6 +84,7 @@
|
|
|
84
84
|
"svelte-component"
|
|
85
85
|
],
|
|
86
86
|
"dependencies": {
|
|
87
|
+
"esm-env": "^1.2.2",
|
|
87
88
|
"pdfjs-dist": "^5.4.394"
|
|
88
89
|
}
|
|
89
90
|
}
|