Skip to content

Commit b6982aa

Browse files
authored
Merge pull request #27 from LibreSign/fix/prevent-load-multiple-pdf-worker
fix: prevent load multiple pdf worker
2 parents 4a8e649 + 5a8d88a commit b6982aa

2 files changed

Lines changed: 23 additions & 3 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@libresign/pdf-elements",
33
"description": "PDF viewer with draggable and resizable element overlays for Vue 2",
4-
"version": "0.3.0",
4+
"version": "0.3.1",
55
"author": "LibreCode <contact@librecode.coop>",
66
"private": false,
77
"main": "dist/pdf-elements.umd.js",

src/utils/asyncReader.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
// SPDX-FileCopyrightText: 2026 LibreCode coop and contributors
22
// SPDX-License-Identifier: AGPL-3.0-or-later
33

4-
import { getDocument, GlobalWorkerOptions } from 'pdfjs-dist'
4+
import { getDocument, GlobalWorkerOptions, PDFWorker } from 'pdfjs-dist'
55
import pdfWorkerCode from 'pdfjs-dist/build/pdf.worker.min.mjs'
66

77
GlobalWorkerOptions.workerSrc = pdfWorkerCode
88

9+
let sharedWorker = null
10+
11+
function getSharedWorker() {
12+
if (!sharedWorker) {
13+
sharedWorker = new PDFWorker({ name: 'libresign-pdf-worker' })
14+
}
15+
return sharedWorker
16+
}
17+
918
export function setWorkerPath(path) {
1019
GlobalWorkerOptions.workerSrc = path
1120
}
@@ -20,5 +29,16 @@ export function readAsArrayBuffer(file) {
2029
}
2130

2231
export function readAsPDF(file) {
23-
return getDocument(file).promise
32+
const worker = getSharedWorker()
33+
const isArrayBuffer = file instanceof ArrayBuffer
34+
const isView = ArrayBuffer.isView(file)
35+
const isBlob = typeof Blob !== 'undefined' && file instanceof Blob
36+
37+
if (file && typeof file === 'object' && !isArrayBuffer && !isView && !isBlob) {
38+
return getDocument({ ...file, worker }).promise
39+
}
40+
if (typeof file === 'string') {
41+
return getDocument({ url: file, worker }).promise
42+
}
43+
return getDocument({ data: file, worker }).promise
2444
}

0 commit comments

Comments
 (0)