diff --git a/package.json b/package.json index e4fa84b..fce8460 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@libresign/pdf-elements", "description": "PDF viewer with draggable and resizable element overlays for Vue 2", - "version": "0.3.0", + "version": "0.3.1", "author": "LibreCode ", "private": false, "main": "dist/pdf-elements.umd.js", diff --git a/src/utils/asyncReader.js b/src/utils/asyncReader.js index 26035ee..81b81a7 100644 --- a/src/utils/asyncReader.js +++ b/src/utils/asyncReader.js @@ -1,11 +1,20 @@ // SPDX-FileCopyrightText: 2026 LibreCode coop and contributors // SPDX-License-Identifier: AGPL-3.0-or-later -import { getDocument, GlobalWorkerOptions } from 'pdfjs-dist' +import { getDocument, GlobalWorkerOptions, PDFWorker } from 'pdfjs-dist' import pdfWorkerCode from 'pdfjs-dist/build/pdf.worker.min.mjs' GlobalWorkerOptions.workerSrc = pdfWorkerCode +let sharedWorker = null + +function getSharedWorker() { + if (!sharedWorker) { + sharedWorker = new PDFWorker({ name: 'libresign-pdf-worker' }) + } + return sharedWorker +} + export function setWorkerPath(path) { GlobalWorkerOptions.workerSrc = path } @@ -20,5 +29,16 @@ export function readAsArrayBuffer(file) { } export function readAsPDF(file) { - return getDocument(file).promise + const worker = getSharedWorker() + const isArrayBuffer = file instanceof ArrayBuffer + const isView = ArrayBuffer.isView(file) + const isBlob = typeof Blob !== 'undefined' && file instanceof Blob + + if (file && typeof file === 'object' && !isArrayBuffer && !isView && !isBlob) { + return getDocument({ ...file, worker }).promise + } + if (typeof file === 'string') { + return getDocument({ url: file, worker }).promise + } + return getDocument({ data: file, worker }).promise }