From acf02ef8f6c683c6737727ca466daf1f8ebf2387 Mon Sep 17 00:00:00 2001 From: Rayan Salhab Date: Sun, 5 Apr 2026 01:37:04 +0000 Subject: [PATCH 1/2] fix: clamp extreme number values to valid PDF range instead of throwing Fixes #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. --- packages/pdfkit/src/object.js | 8 +++----- packages/render/src/primitives/renderGlyphs.ts | 8 +++----- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/packages/pdfkit/src/object.js b/packages/pdfkit/src/object.js index 5e876c48e..7177de8a8 100644 --- a/packages/pdfkit/src/object.js +++ b/packages/pdfkit/src/object.js @@ -116,11 +116,9 @@ class PDFObject { } static number(n) { - if (n > -1e21 && n < 1e21) { - return Math.round(n * 1e6) / 1e6; - } - - throw new Error(`unsupported number: ${n}`); + // 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; } } diff --git a/packages/render/src/primitives/renderGlyphs.ts b/packages/render/src/primitives/renderGlyphs.ts index 46bfc9984..a07a62591 100644 --- a/packages/render/src/primitives/renderGlyphs.ts +++ b/packages/render/src/primitives/renderGlyphs.ts @@ -5,11 +5,9 @@ import { Context } from '../types'; import encodeGlyphs from '../operations/encodeGlyphs'; const number = (n: number) => { - if (n > -1e21 && n < 1e21) { - return Math.round(n * 1e6) / 1e6; - } - - throw new Error(`unsupported number: ${n}`); + // 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; }; const _renderGlyphs = ( From 8d7fe98169d428cf97514cd7c512f1c9b3147a72 Mon Sep 17 00:00:00 2001 From: Rayan Salhab Date: Sun, 26 Apr 2026 16:58:01 +0300 Subject: [PATCH 2/2] chore: add changeset for number clamp fix --- .changeset/hip-rockets-punch.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/hip-rockets-punch.md diff --git a/.changeset/hip-rockets-punch.md b/.changeset/hip-rockets-punch.md new file mode 100644 index 000000000..a4bd4bf8c --- /dev/null +++ b/.changeset/hip-rockets-punch.md @@ -0,0 +1,6 @@ +--- +"@react-pdf/pdfkit": patch +"@react-pdf/render": patch +--- + +fix: clamp extreme number values to valid PDF range