typescriptsveltesveltekitpdfjs-dist

Using pdfjs in Sveltekit/Typescript


I need to use pdfjs (current 4.1.392) for text extraction in a sveltekit 4 typescript project. This is how I try to import pdfjs into my src/routes/+page.svelte:

<script lang="ts">
import * as pdfjs from 'pdfjs-dist/build/pdf';
import pdfjsWorker from 'pdfjs-dist/build/pdf.worker';
pdfjs.GlobalWorkerOptions.workerSrc = pdfjsWorker;
</script>

However, I get the error message "Invalid 'workerSrc' type".

How can I use pdfjs in sveltekit/typescript?


Solution

  • The following code works for me with pdfjs-dist 4.2.67, svelte 4.2.15 and typescript 5.4.5:

    <script lang="ts">
      // @ts-nocheck
      import * as pdfjs from 'pdfjs-dist';
      import * as pdfWorker from 'pdfjs-dist/build/pdf.worker.mjs';
      pdfjs.GlobalWorkerOptions.workerSrc = import.meta.url + 'pdfjs-dist/build/pdf.worker.mjs';
    </script>