Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 241
amir mohammad golestani Avatar asked Nov 16 '25 10:11

amir mohammad golestani


2 Answers

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 < v22. You can try upgrading your Node.js version.

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

like image 112
mb21 Avatar answered Nov 19 '25 10:11

mb21


Here is an another answer https://github.com/wojtekmaj/react-pdf/issues/1811#issuecomment-2157866061

// @ts-expect-error This does not exist outside of polyfill which this is doing
if (typeof Promise.withResolvers === 'undefined') {
    if (window)
        // @ts-expect-error This does not exist outside of polyfill which this is doing
        window.Promise.withResolvers = function () {
            let resolve, reject;
            const promise = new Promise((res, rej) => {
                resolve = res;
                reject = rej;
            });
            return { promise, resolve, reject };
        };
}
// there is your `/legacy/build/pdf.worker.min.mjs` url
pdfjs.GlobalWorkerOptions.workerSrc = new URL(
    'pdfjs-dist/legacy/build/pdf.worker.min.mjs',
    import.meta.url
).toString();

// or you can use this
// pdfjs.GlobalWorkerOptions.workerSrc="https://unpkg.com/[email protected]/legacy/build/pdf.worker.min.mjs"

like image 39
tf z Avatar answered Nov 19 '25 10:11

tf z