Skip to content

Commit

Permalink
PDF names need to be escaped when saving
Browse files Browse the repository at this point in the history
  • Loading branch information
calixteman committed Sep 9, 2020
1 parent e51e9d1 commit 4b05cc5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
26 changes: 26 additions & 0 deletions src/core/core_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,33 @@ function isWhiteSpace(ch) {
return ch === 0x20 || ch === 0x09 || ch === 0x0d || ch === 0x0a;
}

function escapePDFName(str) {
const buffer = [];
let start = 0;
for (let i = 0, ii = str.length; i < ii; i++) {
const char = str.charCodeAt(i);
if (char < 0x21 || 0x7e < char) {
if (start < i) {
buffer.push(str.substring(start, i));
}
buffer.push(`#${char.toString(16)}`);
start = i + 1;
}
}

if (buffer.length === 0) {
return str;
}

if (start < str.length) {
buffer.push(str.substring(start, str.length));
}

return buffer.join("");
}

export {
escapePDFName,
getLookupTableFactory,
MissingDataException,
XRefEntryException,
Expand Down
3 changes: 2 additions & 1 deletion src/core/writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
import { Dict, isDict, isName, isRef, isStream, Name } from "./primitives.js";
import { SimpleDOMNode, SimpleXMLParser } from "../shared/xml_parser.js";
import { calculateMD5 } from "./crypto.js";
import { escapePDFName } from "./core_utils.js";

function writeDict(dict, buffer, transform) {
buffer.push("<<");
Expand Down Expand Up @@ -77,7 +78,7 @@ function numberToString(value) {

function writeValue(value, buffer, transform) {
if (isName(value)) {
buffer.push(`/${value.name}`);
buffer.push(`/${escapePDFName(value.name)}`);
} else if (isRef(value)) {
buffer.push(`${value.num} ${value.gen} R`);
} else if (Array.isArray(value)) {
Expand Down
12 changes: 12 additions & 0 deletions test/unit/core_utils_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import { Dict, Ref } from "../../src/core/primitives.js";
import {
escapePDFName,
getInheritableProperty,
isWhiteSpace,
log2,
Expand Down Expand Up @@ -211,4 +212,15 @@ describe("core_utils", function () {
expect(isWhiteSpace(undefined)).toEqual(false);
});
});

describe("escapePDFName", function () {
it("should escape PDF name", function () {
expect(escapePDFName("hello")).toEqual("hello");
expect(escapePDFName("\xfehello")).toEqual("#fehello");
expect(escapePDFName("he\xfell\xffo")).toEqual("he#fell#ffo");
expect(escapePDFName("\xfehe\xfell\xffo\xff")).toEqual(
"#fehe#fell#ffo#ff"
);
});
});
});

0 comments on commit 4b05cc5

Please sign in to comment.