Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that saveDocument works if there's no /ID-entry in the PDF document (issue 13279) #13280

Merged
merged 1 commit into from
Apr 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/core/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -633,11 +633,11 @@ class WorkerMessageHandler {

newXrefInfo = {
rootRef: xref.trailer.getRaw("Root") || null,
encrypt: xref.trailer.getRaw("Encrypt") || null,
encryptRef: xref.trailer.getRaw("Encrypt") || null,
newRef: xref.getNewRef(),
infoRef: xref.trailer.getRaw("Info") || null,
info: infoObj,
fileIds: xref.trailer.getRaw("ID") || null,
fileIds: xref.trailer.get("ID") || null,
startXRef,
filename,
};
Expand Down
6 changes: 3 additions & 3 deletions src/core/writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ function incrementalUpdate({
if (xrefInfo.infoRef !== null) {
newXref.set("Info", xrefInfo.infoRef);
}
if (xrefInfo.encrypt !== null) {
newXref.set("Encrypt", xrefInfo.encrypt);
if (xrefInfo.encryptRef !== null) {
newXref.set("Encrypt", xrefInfo.encryptRef);
}

// Add a ref for the new xref and sort them
Expand All @@ -226,7 +226,7 @@ function incrementalUpdate({

newXref.set("Index", indexes);

if (xrefInfo.fileIds.length !== 0) {
if (Array.isArray(xrefInfo.fileIds) && xrefInfo.fileIds.length > 0) {
const md5 = computeMD5(baseOffset, xrefInfo);
newXref.set("ID", [xrefInfo.fileIds[0], md5]);
}
Expand Down
36 changes: 35 additions & 1 deletion test/unit/writer_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe("Writer", function () {
fileIds: ["id", ""],
rootRef: null,
infoRef: null,
encrypt: null,
encryptRef: null,
filename: "foo.pdf",
info: {},
};
Expand All @@ -59,6 +59,40 @@ describe("Writer", function () {

expect(data).toEqual(expected);
});

it("should update a file, missing the /ID-entry, with new objects", function () {
const originalData = new Uint8Array();
const newRefs = [{ ref: Ref.get(123, 0x2d), data: "abc\n" }];
const xrefInfo = {
newRef: Ref.get(789, 0),
startXRef: 314,
fileIds: null,
rootRef: null,
infoRef: null,
encryptRef: null,
filename: "foo.pdf",
info: {},
};

let data = incrementalUpdate({ originalData, xrefInfo, newRefs });
data = bytesToString(data);

const expected =
"\nabc\n" +
"789 0 obj\n" +
"<< /Size 790 /Prev 314 /Type /XRef /Index [0 1 123 1 789 1] " +
"/W [1 1 2] /Length 12>> stream\n" +
"\x00\x01\xff\xff" +
"\x01\x01\x00\x2d" +
"\x01\x05\x00\x00\n" +
"endstream\n" +
"endobj\n" +
"startxref\n" +
"5\n" +
"%%EOF\n";

expect(data).toEqual(expected);
});
});

describe("writeDict", function () {
Expand Down