Pdf-lib: Failed to parse dictionary value

Created on 31 Jan 2019  路  5Comments  路  Source: Hopding/pdf-lib

Hi @Hopding ,
Got another bug for you. I'm on v0.6.1-rc2. Let me know if you have any questions! Thanks again for your help.

Error: Failed to parse dictionary value
    at error (pdf-lib.js:8569)
    at parseDict (pdf-lib.js:46642)
    at parseDictOrStream (pdf-lib.js:46908)
    at parseIndirectObj (pdf-lib.js:46947)
    at parseBodySection (pdf-lib.js:47174)
    at parseDocument (pdf-lib.js:47226)
    at PDFParser.parse (pdf-lib.js:47257)
    at Function.PDFDocumentFactory.load (pdf-lib.js:47379)

test.pdf

corrupt-pdf

Most helpful comment

@kevinswartz Just wanted to update you on this issue. I'm planning a redesign of pdf-lib's parser in the coming months. One of the goals of this redesign is to tolerate invalid objects to the greatest extent possible. Once the redesign is complete, pdf-lib should be able to support loading/saving this PDF without any issues. And editing the PDF should be supported as well, as long as the operations being performed don't rely on any invalid objects (this particular invalid object wouldn't hinder any common operations).

All 5 comments

Hello @kevinswartz.

I took a look at this PDF. The cause of the error is some completely invalid syntax used in the document. This is a bit different from what I saw in https://github.com/Hopding/pdf-lib/issues/68 and https://github.com/Hopding/pdf-lib/issues/69. Those documents appeared to have had a whole chunk of bytes completely missing. But this document doesn't appear to have any missing data. It just uses invalid values for two dictionary keys:

22 0 obj <</Type/Outlines/First ## 0 R/Last ** 0 R/Count 2>> endobj

Notice the ## and ** in the above object. Those are completely invalid PDF objects. That syntax appears nowhere in the PDF spec. Those values should be integers. E.g. the following would be valid:

22 0 obj <</Type/Outlines/First 3 0 R/Last 1 0 R/Count 2>> endobj

Do you know how this PDF was generated? I don't see how this error could have come about from a transmission error. Whatever tool(s) generated this document must be inserting these characters. It's probably worth opening a bug report against the tool/system that produced this document.

Of course, it looks like most PDF viewers are able to open the document despite the invalid syntax (Though Acrobat flagged the invalid syntax when I ran a pre-flight check on it). I'll have to think about the best way for pdf-lib to handle this.

Ideally it would recognize the invalid syntax and repair it. But I don't really want to add special case logic for these two random characters. And even then, there's no way to truly correct the error, because there's no way to know which two integers belong there.

One feasible approach might be to just ignore the object if it contains invalid syntax. But I'll have to give this some more thought.

Thanks @Hopding ,
This was generated by a client of ours using Drake Software, which is used for tax and accounting. I got a trial of Drake and made some pdfs with it, but couldn't find any others that could reproduce this issue. Could be from an old version? I'd still like to support this if possible?

@kevinswartz Thanks for testing this out! It certainly could be a bug in a previous version of the Drake PDF software.

I'd like for pdf-lib to handle these PDFs as well. But note that it will probably be another couple of weeks before I have time to decide on and implement a fix.

@kevinswartz Just wanted to update you on this issue. I'm planning a redesign of pdf-lib's parser in the coming months. One of the goals of this redesign is to tolerate invalid objects to the greatest extent possible. Once the redesign is complete, pdf-lib should be able to support loading/saving this PDF without any issues. And editing the PDF should be supported as well, as long as the operations being performed don't rely on any invalid objects (this particular invalid object wouldn't hinder any common operations).

v1.0.0 is officially released! It includes a new parser that is able to tolerate this document. The full release notes are available here.

You can install it with npm:

npm install --save [email protected]

or yarn:

yarn add [email protected]

It's also available on the unpkg CDN:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nachum37 picture nachum37  路  3Comments

fatso83 picture fatso83  路  3Comments

DanielJackson-Oslo picture DanielJackson-Oslo  路  8Comments

BoLaMN picture BoLaMN  路  5Comments

zimt28 picture zimt28  路  5Comments