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'
55import pdfWorkerCode from 'pdfjs-dist/build/pdf.worker.min.mjs'
66
77GlobalWorkerOptions . 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+
918export function setWorkerPath ( path ) {
1019 GlobalWorkerOptions . workerSrc = path
1120}
@@ -20,5 +29,16 @@ export function readAsArrayBuffer(file) {
2029}
2130
2231export 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