Skip to content

fix: clamp extreme number values to valid PDF range - #3337

Open
cyphercodes wants to merge 2 commits into
diegomura:masterfrom
cyphercodes:fix/unsupported-number-error
Open

fix: clamp extreme number values to valid PDF range#3337
cyphercodes wants to merge 2 commits into
diegomura:masterfrom
cyphercodes:fix/unsupported-number-error

Conversation

@cyphercodes

Copy link
Copy Markdown

Fixes #3277

The number() function would throw an error when values exceeded the PDF-valid range of -1e21 to 1e21. This occurred when calculating glyph advance positions with certain font/text combinations.

Instead of throwing, we now clamp values to the valid range, allowing PDF generation to continue while maintaining valid output.

Changes

  • packages/render/src/primitives/renderGlyphs.ts: Clamp instead of throw
  • packages/pdfkit/src/object.js: Clamp instead of throw

Fixes diegomura#3277 - Error: unsupported number when rendering PDFs with extreme glyph positioning values.

The number() function in both renderGlyphs.ts and object.js would throw an error
when values exceeded the PDF-valid range of -1e21 to 1e21. This occurred when
calculating glyph advance positions with certain font/text combinations.

Instead of throwing, we now clamp values to the valid range, which allows PDF
generation to continue while maintaining valid output.
@changeset-bot

changeset-bot Bot commented Apr 5, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 8d7fe98

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/render Patch
@react-pdf/font Patch
@react-pdf/renderer Patch
@react-pdf/types Patch
next-14 Patch
next-15 Patch
@react-pdf/vite-example Patch
@react-pdf/e2e-node-cjs Patch
@react-pdf/e2e-node-esm Patch
@react-pdf/layout 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

@Hulack

Hulack commented Apr 22, 2026

Copy link
Copy Markdown

Thanks for the fix.

@cyphercodes

Copy link
Copy Markdown
Author

Added the missing changeset for this bugfix:

  • @react-pdf/pdfkit: patch
  • @react-pdf/render: patch

Local verification:

  • git diff --check
  • Custom changeset frontmatter validation against package names ✅

Note: GitHub Actions is still waiting on maintainer approval for the forked PR run (action_required).

Comment on lines +119 to +121
// Clamp to valid PDF number range to prevent errors with extreme values
const clamped = Math.max(-1e21 + 1, Math.min(1e21 - 1, n));
return Math.round(clamped * 1e6) / 1e6;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

won't this mean that rendering will be broken? In the sense that it won't show things as they should

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.

Error: unsupported number: -5.115033082580265e+22

3 participants