Skip to content

ReferenceError: Buffer is not defined #2758

@shuaybi

Description

@shuaybi

Why is the code using Buffer when running inside a browser?

When creating a PDFDocument I get the following error in the browser console:

pdfkit.js:441 Uncaught (in promise) ReferenceError: Buffer is not defined
at wordArrayToBuffer2 (pdfkit.js:441:3)
at Function.generateFileID (pdfkit.js:454:12)
at new PDFDocument2 (pdfkit.js:38748:29)

Seems to be coming from the module pdfkit.js

I am using version 3.4.4 of the library.

I am trying to generate pdf in the browser. Isn't Buffer only available in Node? Why is the code using Buffer when running inside a browser?

Using Chrome Browser: Version 124.0.6367.208 (Official Build) (arm64)

Workaround:

I installed buffer (npm install buffer) and then added the following to my index.js file:

window.Buffer = window.Buffer || require("buffer").Buffer;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions