Skip to content

Commit 05e34dd

Browse files
authored
Remove indirect objects with objectNumber=0 after parsing (Hopding#496)
1 parent 52b1fe9 commit 05e34dd

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/core/parser/PDFParser.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ class PDFParser extends PDFObjectParser {
6969

7070
this.maybeRecoverRoot();
7171

72+
if (this.context.lookup(PDFRef.of(0))) {
73+
console.warn('Removing parsed object: 0 0 R');
74+
this.context.delete(PDFRef.of(0));
75+
}
76+
7277
return this.context;
7378
}
7479

tests/core/parser/PDFParser.spec.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ describe(`PDFParser`, () => {
2121
console.warn = jest.fn((...args) => {
2222
if (
2323
!args[0].includes('Trying to parse invalid object:') &&
24-
!args[0].includes('Invalid object ref:')
24+
!args[0].includes('Invalid object ref:') &&
25+
!args[0].includes('Removing parsed object: 0 0 R')
2526
) {
2627
origConsoleWarn(...args);
2728
}
@@ -368,4 +369,30 @@ describe(`PDFParser`, () => {
368369
const object28 = context.lookup(PDFRef.of(28));
369370
expect(object28).toBeInstanceOf(PDFDict);
370371
});
372+
373+
it(`removes indirect objects with objectNumber=0`, async () => {
374+
const input = `
375+
%PDF-1.7
376+
1 0 obj
377+
(foo)
378+
endobj
379+
0 0 obj
380+
(bar)
381+
endobj
382+
2 0 obj
383+
(baz)
384+
endobj
385+
%%EOF
386+
`;
387+
const parser = PDFParser.forBytesWithOptions(typedArrayFor(input));
388+
const context = await parser.parseDocument();
389+
390+
expect(context.enumerateIndirectObjects().length).toBe(2);
391+
const object1 = context.lookup(PDFRef.of(1));
392+
expect(object1).toBeInstanceOf(PDFString);
393+
const object0 = context.lookup(PDFRef.of(0));
394+
expect(object0).toBe(undefined);
395+
const object2 = context.lookup(PDFRef.of(2));
396+
expect(object2).toBeInstanceOf(PDFString);
397+
});
371398
});

0 commit comments

Comments
 (0)