TypeError: worker.load is not a function

909 Views Asked by At

I use vue.js and nuxt.js when i want to extract text from pdf or image file. it doesn't seem work. I don't know why. this is my first time to ask question if you want more some information. please feel free to ask me. I'm stuck with this error for 4 days T^T. I really need help.

i try to fix about this error. All of I want it's just fix error.

async parseFile() {
      if (this.uploadedFile) {
        const fileType = this.getFileType(this.uploadedFile.name);
        if (fileType === "image") {
          const worker = createWorker();
          await worker.load();
          await worker.loadLanguage("eng");
          await worker.initialize("eng");
          const {
            data: { text },
          } = await worker.recognize(this.uploadedFile);
          console.log(text);
          await worker.terminate();
        } else if (fileType === "pdf") {
          const fileReader = new FileReader();
          fileReader.onload = async () => {
            const typedArray = new Uint8Array(fileReader.result);
            const pdf = await pdfjsLib.getDocument(typedArray).promise;
            const numPages = pdf.numPages;
            let pdfText = "";
            for (let i = 1; i <= numPages; i++) {
              const page = await pdf.getPage(i);
              const content = await page.getTextContent();
              const pageText = content.items.map((item) => item.str).join(" ");
              pdfText += pageText + "\n";
            }
            console.log(pdfText);
          };
          fileReader.readAsArrayBuffer(this.uploadedFile);
        }

3

There are 3 best solutions below

0
Giorgos Saramantis On

Just a suggestion, Check if the function createWorker() is not properly imported or if there is an issue with the library you are using and also check that you have installed the necessary dependencies

1
Shadwar Nayyar On

Use this version = "tesseract.js": "^2.1.5" it will run.

0
Erik On

Update the syntax to the version of Tesseract you are using. Try replacing

const worker = createWorker();
await worker.load();
await worker.loadLanguage("eng");
wait worker.initialize("eng");

with

const worker = await createWorker("eng")