1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-22 16:18:08 +02:00

PDF names need to be escaped when saving

This commit is contained in:
Calixte Denizet 2020-09-09 18:39:14 +02:00
parent f9d56320f5
commit dc4eb71ff1
4 changed files with 56 additions and 2 deletions

View file

@ -185,7 +185,33 @@ function parseXFAPath(path) {
});
}
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 || char > 0x7e || char === 0x23) {
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,

View file

@ -16,9 +16,9 @@
import { bytesToString, escapeString, warn } from "../shared/util.js";
import { Dict, isDict, isName, isRef, isStream, Name } from "./primitives.js";
import { escapePDFName, parseXFAPath } from "./core_utils.js";
import { SimpleDOMNode, SimpleXMLParser } from "../shared/xml_parser.js";
import { calculateMD5 } from "./crypto.js";
import { parseXFAPath } from "./core_utils.js";
function writeDict(dict, buffer, transform) {
buffer.push("<<");
@ -73,7 +73,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)) {