Skip to content

Commit

Permalink
Remove indirect objects with objectNumber=0 after parsing (#496)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hopding committed Jun 20, 2020
1 parent 52b1fe9 commit 05e34dd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/core/parser/PDFParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ class PDFParser extends PDFObjectParser {

this.maybeRecoverRoot();

if (this.context.lookup(PDFRef.of(0))) {
console.warn('Removing parsed object: 0 0 R');
this.context.delete(PDFRef.of(0));
}

return this.context;
}

Expand Down
29 changes: 28 additions & 1 deletion tests/core/parser/PDFParser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ describe(`PDFParser`, () => {
console.warn = jest.fn((...args) => {
if (
!args[0].includes('Trying to parse invalid object:') &&
!args[0].includes('Invalid object ref:')
!args[0].includes('Invalid object ref:') &&
!args[0].includes('Removing parsed object: 0 0 R')
) {
origConsoleWarn(...args);
}
Expand Down Expand Up @@ -368,4 +369,30 @@ describe(`PDFParser`, () => {
const object28 = context.lookup(PDFRef.of(28));
expect(object28).toBeInstanceOf(PDFDict);
});

it(`removes indirect objects with objectNumber=0`, async () => {
const input = `
%PDF-1.7
1 0 obj
(foo)
endobj
0 0 obj
(bar)
endobj
2 0 obj
(baz)
endobj
%%EOF
`;
const parser = PDFParser.forBytesWithOptions(typedArrayFor(input));
const context = await parser.parseDocument();

expect(context.enumerateIndirectObjects().length).toBe(2);
const object1 = context.lookup(PDFRef.of(1));
expect(object1).toBeInstanceOf(PDFString);
const object0 = context.lookup(PDFRef.of(0));
expect(object0).toBe(undefined);
const object2 = context.lookup(PDFRef.of(2));
expect(object2).toBeInstanceOf(PDFString);
});
});

0 comments on commit 05e34dd

Please sign in to comment.