Skip to content

Error: PDF.js could not be resolved: ReferenceError: DOMMatrix is not defined #48

@mariusa

Description

@mariusa

Environment

├── pdfjs-dist@5.4.449 (I also tried pdfjs-dist@5.4.394, same error)
├── unpdf@1.4.0

node 24

Reproduction


import { definePDFJSModule, renderPageAsImage } from 'unpdf'
// Use the official PDF.js build
await definePDFJSModule(() => import('pdfjs-dist'))
import { promises as fsPromises } from 'fs'
const { readFile, writeFile } = fsPromises

async function pdfPageToImage() {


    const pdf = await readFile('./misc/test-places.pdf')
    const buffer = new Uint8Array(pdf)
    const pageNumber = 1

    const result = await renderPageAsImage(buffer, pageNumber, {
        canvasImport: () => import('@napi-rs/canvas'),
        scale: 2,
    })
    await writeFile('dummy-page-1.png', new Uint8Array(result))
}



await pdfPageToImage()

Describe the bug

Hallo Johann and team,

Trying to render pdf page as image, as the example, but that gives error

Warning: Please use the `legacy` build in Node.js environments.
file:///.../node_modules/unpdf/dist/index.mjs:30
      throw new Error(`PDF.js could not be resolved: ${error}`);
            ^

Error: PDF.js could not be resolved: ReferenceError: DOMMatrix is not defined

I see DOMMatrix is somehow handled in this repo, but don't know how.

Additional context

No response

Logs

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions