From c2d065a8a5bbf6fb5db53d8236a927da0b961907 Mon Sep 17 00:00:00 2001 From: enes Date: Mon, 2 Sep 2024 12:59:35 +0200 Subject: [PATCH] refactor(pdf-dist): optimize workers --- src/utils/pdf.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/utils/pdf.ts b/src/utils/pdf.ts index 1dd75f6..7803d91 100644 --- a/src/utils/pdf.ts +++ b/src/utils/pdf.ts @@ -1,12 +1,14 @@ import { PdfPage } from '../types/drawing.ts' -import * as PDFJS from 'pdfjs-dist' import { PDFDocument } from 'pdf-lib' import { Mark } from '../types/mark.ts' -PDFJS.GlobalWorkerOptions.workerSrc = new URL( - 'pdfjs-dist/build/pdf.worker.min.mjs', - import.meta.url -).toString() +import * as PDFJS from 'pdfjs-dist' +import PDFJSWorker from 'pdfjs-dist/build/pdf.worker.mjs?worker' +if (!PDFJS.GlobalWorkerOptions.workerPort) { + // Use workerPort and allow worker to be shared between all getDocument calls + const worker = new PDFJSWorker() + PDFJS.GlobalWorkerOptions.workerPort = worker +} /** * Defined font size used when generating a PDF. Currently it is difficult to fully