Pdf-lib: TypeError: Cannot read property 'clone' of undefined

Created on 18 Sep 2019  路  11Comments  路  Source: Hopding/pdf-lib

Hi,
When trying to copy a specific PDF, i have the error : TypeError: Cannot read property 'clone' of undefined

Here is a not working example :
https://runkit.com/gautiert/5d81e279596dfc0015e16a7c

Probably related to #186

Most helpful comment

I was able to fix the problem in #200. It will go out in the next pdf-lib release.

Incase, you're interested, the problem was due to a missing newline. The PDF states that:

The last line of the file shall contain only the end-of-file marker, %%EOF.

This seems pretty clear cut. But it becomes ambiguous when you start dealing with incremental update sections (the file you shared here has one of these):

Each trailer shall be terminated by its own end-of-file (%%EOF) marker.

But it doesn't really state whether the previous %%EOF marker should be followed by a newline before the update section's content begins. All examples in the spec do have a newline. And every other updated PDF I've come across does as well.

However, the PDF you shared here does not follow this convention:

285 | 17201^M
286 | %%EOF28 0 obj^M
287 | <<^M

Notice line 286. That's what caused the issue. pdf-lib's parser was implicitly expecting a newline after the %%EOF. And when it didn't find one, that caused it to enter error recovery mode and skip object 28.

Gotta love the PDF spec's ambiguity and lack of clarity 馃檮. Anyhow, it's fixed now 馃憤

All 11 comments

@GautierT yes I think it's the same issue

@mlecoq
When i try your hotfix with this file the resulting file is broken.
Any idea why ?
Thanks

I will have a look tomorrow

It's funny, pdf is still readable but barcodes are missing

I have tried several use cases : merging this pdf with another one, adding content on it.... Barcodes are always missing. The generated pdf is slightly different from the original one, for example version is updated : PDF-1.4 to PDF-1.7

I think there is a second issue in the library

Barcodes were added with Hummusjs

This does appear to be a separate issue. I verified that the barcodes are still missing after merging #199.

I was able to fix the problem in #200. It will go out in the next pdf-lib release.

Incase, you're interested, the problem was due to a missing newline. The PDF states that:

The last line of the file shall contain only the end-of-file marker, %%EOF.

This seems pretty clear cut. But it becomes ambiguous when you start dealing with incremental update sections (the file you shared here has one of these):

Each trailer shall be terminated by its own end-of-file (%%EOF) marker.

But it doesn't really state whether the previous %%EOF marker should be followed by a newline before the update section's content begins. All examples in the spec do have a newline. And every other updated PDF I've come across does as well.

However, the PDF you shared here does not follow this convention:

285 | 17201^M
286 | %%EOF28 0 obj^M
287 | <<^M

Notice line 286. That's what caused the issue. pdf-lib's parser was implicitly expecting a newline after the %%EOF. And when it didn't find one, that caused it to enter error recovery mode and skip object 28.

Gotta love the PDF spec's ambiguity and lack of clarity 馃檮. Anyhow, it's fixed now 馃憤

Thanks a lot @Hopding !!
Bravo for this awesome lib ! 馃憦

Version 1.1.1 is now published. It contains the fix for this issue. The full release notes are available here.

You can install this new version with npm:

npm install [email protected]

It's also available on unpkg:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dhollenbeck picture dhollenbeck  路  4Comments

zimt28 picture zimt28  路  5Comments

zimt28 picture zimt28  路  5Comments

lepidotteri picture lepidotteri  路  3Comments

DanielJackson-Oslo picture DanielJackson-Oslo  路  3Comments