Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/hip-rockets-punch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@react-pdf/pdfkit": patch
"@react-pdf/render": patch
---

fix: clamp extreme number values to valid PDF range
8 changes: 3 additions & 5 deletions packages/pdfkit/src/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Comment on lines +119 to +121

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

}
}

Expand Down
8 changes: 3 additions & 5 deletions packages/render/src/primitives/renderGlyphs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down