Skip to content

refactor(pdfkit): parse images from plain Uint8Array - #3472

Closed
diegomura wants to merge 1 commit into
masterfrom
diegomura/pdfkit-image-binary-pr
Closed

refactor(pdfkit): parse images from plain Uint8Array#3472
diegomura wants to merge 1 commit into
masterfrom
diegomura/pdfkit-image-binary-pr

Conversation

@diegomura

Copy link
Copy Markdown
Owner

Extracted from #3397 (PR 1 & 3 of the plan, image half). Drops the remaining Node Buffer requirements from image parsing.

Changes

  • image.js — accepts any Uint8Array (a Buffer still is one), and stops copying ArrayBuffer input through Buffer.from. Plain Uint8Array sources now work instead of falling through to fs.readFileSync.
  • image/jpeg.js — uses the binary.js readUInt16BE helper rather than the Buffer.prototype method, matching the EXIF parser already in that file.
  • binary.js — adds fromBinaryString; fromBase64 becomes a one-liner on top of it.

Not included

image/png.js stays on Buffer. It feeds zlib.deflateSync, and the browser build aliases zlib to browserify-zlib, which throws on non-Buffer input — that was #3431. Those lines belong with the pako swap (PR 7 in the #3397 plan), not here.

Tests

New tests/image.test.ts covers PDFImage.open for Uint8Array, Buffer, ArrayBuffer, base64 data URI, and unknown-format input; new tests/binary.test.ts covers the byte helpers against Buffer as the reference. Full renderer/render/image suites pass unchanged.

Drops the remaining Node `Buffer` requirements from image parsing:

- `image.js` accepts any `Uint8Array` (a `Buffer` still is one) and no
  longer copies `ArrayBuffer` input through `Buffer.from`
- `image/jpeg.js` uses the `binary.js` `readUInt16BE` helper instead of
  the `Buffer.prototype` method, matching the EXIF parser already in
  that file
- `binary.js` grows `fromBinaryString`, which `fromBase64` now reuses

`image/png.js` is left alone: it feeds `zlib.deflateSync`, and the
browser build aliases `zlib` to `browserify-zlib`, which rejects
non-`Buffer` input (see #3431). That one goes with the pako swap.

Extracted from #3397.
@changeset-bot

changeset-bot Bot commented Aug 8, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 447eeb8

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 12 packages
Name Type
@react-pdf/pdfkit Patch
@react-pdf/font Patch
@react-pdf/renderer Patch
@react-pdf/types Patch
@react-pdf/math Patch
@react-pdf/mermaid Patch
next-14 Patch
next-15 Patch
@react-pdf/vite-example Patch
@react-pdf/layout Patch
@react-pdf/render Patch
@react-pdf/stylesheet Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@diegomura diegomura closed this Aug 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant