Search code examples
pdf.jsastrojspdfjs-dist

pdf.js pdfjs-dist Promise.withResolvers is not a function


I'm trying to extract data from pdf files and return it. here's the code in the serverside in astro

import * as pdfjsLib from "pdfjs-dist";
pdfjsLib.GlobalWorkerOptions.workerSrc = "../../node_modules/pdfjs-dist/build/pdf.worker.mjs";

export const contentExtractor = async (arrayBufferPDF: ArrayBuffer): Promise<string> => {
  const pdf = (pdfjsLib).getDocument(arrayBufferPDF);
  return pdf.promise.then(async (pdf) => {
    let totalContent = ""
    const maxPages = pdf._pdfInfo.numPages;

    for (let pageNumber = 1; pageNumber <= maxPages; pageNumber++) {
      const page = await pdf.getPage(pageNumber);
      const pageContent = await page.getTextContent();
      const content = pageContent.items.map((s: any) => s.str).join(" ")
      totalContent = totalContent + content
    }
    return totalContent
  })
}

and the error is

12:44:40 [ERROR] Promise.withResolvers is not a function
  Stack trace:
    at /Users/some-user/Documents/Projects/Github/pdf-extractor/app/node_modules/pdfjs-dist/build/pdf.mjs:3026:32
    [...] See full stack trace in the browser, or rerun with --verbose.

I don't understand where the problem is. Could someone help me with it?


Solution

  • The build of PDF.js you are using does not support running in Node.js (i.e. only in the browser). The error comes from Promise.withResolvers being called, which is not supported by Node.js.

    It seems, the recommended way to run it under Node.js is to use the legacy build (using pdfjs-dist/legacy/build/pdf.js).