Skip to content
Merged
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
37 changes: 19 additions & 18 deletions src/writer/pdf-writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,44 +284,45 @@ function collectReachableRefs(
encrypt?: PdfRef,
): Set<string> {
const visited = new Set<string>();
const stack: PdfObject[] = [root];

const walk = (obj: PdfObject | null): void => {
if (obj === null) {
return;
}
if (info) {
stack.push(info);
}

if (encrypt) {
stack.push(encrypt);
}

while (stack.length > 0) {
const obj = stack.pop()!;

if (obj instanceof PdfRef) {
const key = `${obj.objectNumber} ${obj.generation}`;

if (visited.has(key)) {
return;
continue;
}

visited.add(key);

const resolved = registry.resolve(obj);

walk(resolved);
if (resolved !== null) {
stack.push(resolved);
}
} else if (obj instanceof PdfDict) {
// PdfStream extends PdfDict, so this handles both
for (const [, value] of obj) {
walk(value);
if (value != null) {
stack.push(value);
}
}
} else if (obj instanceof PdfArray) {
for (const item of obj) {
walk(item);
stack.push(item);
}
}
};

walk(root);

if (info) {
walk(info);
}

if (encrypt) {
walk(encrypt);
}

return visited;
Expand Down