Cannot find help for error worker declaration pdfjs-dist (Typescript RemixJs)

73 Views Asked by At

I'm currently struggling with the pdfjs-dist library because the function getDocument isn't working because of a lack of declaration of GlobalWorkerOptions. The twist is that i have no similar error on internet and also no similar files. I am working in RemixJs and it's the import that does not work.

Here's what in the pdfjs-dist/build :

-pdf.min.mjs
-pdf.mjs
-pdf.mjs.map
-pdf.sandbox.min.mjs
-pdf.sandbox.mjs
-pdf.sandbox.mjs.map
-pdf.worker.min.mjs
-pdf.worker.mjs
-pdf.worker.mjs.map

So i do not have any .entry or .js files.

However when I'm trying to import the library:

import { getDocument, GlobalWorkerOptions } from 'pdfjs-dist';
import * as pdfjsLib from 'pdfjs-dist';
import pdfjsWorker from 'pdfjs-dist/build/pdf.worker.min.mjs';

pdfjsLib.GlobalWorkerOptions.workerSrc = pdfjsWorker;

This happens:

info  rebuilding... (\~ app\\routes\\traduction-document.tsx)
X \[ERROR\] No matching export in "node_modules/pdfjs-dist/build/pdf.worker.min.mjs" for import "default"

    app/routes/traduction-document.tsx:24:7:
      24 │ import pdfjsWorker from 'pdfjs-dist/build/pdf.worker.min.mjs';
         ╵        ~~~~~~~~~~~

So there's not matching imports forpdfjsWorker.

I've already imported in the root of the project, found ways to import with a different path but sadly i'm stuck. THe final goal after the good importation and the declaration of the worker, i can access the document and read it's caracters and maybe display it, but it's for later.This is what the import would be used :

 try{
      if (file.type === "application/pdf") {
        const reader = new FileReader();
        reader.onload = async (e) => {
        try {
          const typedArray = new Uint8Array(e.target.result as ArrayBuffer);
          const pdfDoc = await getDocument({ data: typedArray }).promise;
          let textContent = '';
          for (let i = 1; i <= pdfDoc.numPages; i++) {
            const page = await pdfDoc.getPage(i);
            const text = await page.getTextContent();
            textContent += text.items.map(item => item.str).join(' ');
          }

          setCharacterCount(textContent.length); // Update character count

          // Set download file name
          const baseName = file.name.split('.').slice(0, -1).join('.');
          const newDownloadName = `${baseName}_processed.pdf`;
          setDownloadFileName(newDownloadName);
        } catch (error) {
          console.error("Error reading PDF", error);
        }
      };
      reader.readAsArrayBuffer(file);
      }
    }

If someone can help me with this problem it would really help !

0

There are 0 best solutions below