From 9710372a1bacd0988e41a86a2440245fdd849d69 Mon Sep 17 00:00:00 2001 From: Yarchik Date: Thu, 2 Jul 2026 10:43:59 +0100 Subject: [PATCH] Fix escapePDFName producing malformed name escapes for control characters escapePDFName emitted a single hex digit for character codes below 0x10 (TAB became #9, not #09). PDF 32000-1 7.3.5 requires exactly two hex digits after #. On re-save (annotations, form fields, font names) such a Name is written malformed and the lexer mis-parses it on reload, dropping bytes. Pad the hex to two digits; a no-op for codes 0x10 to 0xFF. --- src/core/core_utils.js | 2 +- test/unit/core_utils_spec.js | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/core/core_utils.js b/src/core/core_utils.js index 5556c9e15..5360d9ae2 100644 --- a/src/core/core_utils.js +++ b/src/core/core_utils.js @@ -376,7 +376,7 @@ function escapePDFName(str) { if (start < i) { buffer.push(str.substring(start, i)); } - buffer.push(`#${char.toString(16)}`); + buffer.push(`#${char.toString(16).padStart(2, "0")}`); start = i + 1; } } diff --git a/test/unit/core_utils_spec.js b/test/unit/core_utils_spec.js index a9c6bd8f0..6abfae364 100644 --- a/test/unit/core_utils_spec.js +++ b/test/unit/core_utils_spec.js @@ -290,6 +290,11 @@ describe("core_utils", function () { "#23#28#29#3c#3e#5b#5d#7b#7d#2f#25" ); }); + + it("should escape control characters using two hexadecimal digits", function () { + expect(escapePDFName("\x00\x09\x0a\x1f")).toEqual("#00#09#0a#1f"); + expect(escapePDFName("a\tb")).toEqual("a#09b"); + }); }); describe("escapeString", function () {