pdf-lib: Trying to parse invalid object

When I open my beautiful PDF with this lib, all is great. But because the file is big, I compressed it using smallpdf.com, and now when I open the compressed version, I get these warnings:

Trying to parse invalid object: {"line":140,"column":0,"offset":33170})
Invalid object ref: 9 0 R
Trying to parse invalid object: {"line":301,"column":0,"offset":65943})
Invalid object ref: 21 0 R
Trying to parse invalid object: {"line":309,"column":0,"offset":65969})
Invalid object ref: 22 0 R
Trying to parse invalid object: {"line":317,"column":0,"offset":65995})
Invalid object ref: 23 0 R

Any idea why? Is this an issue if I just use drawText?

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 19 (6 by maintainers)

Most upvoted comments

@yakupteke did you read this thread?..

So when the next release of pdf-lib goes out, you will no longer see these warnings.

Hi @Hopding, do you mind reopening this issue?

I am on "pdf-lib": "1.16.0" and I am still seeing this problem (even for non encrypted PDFs).

Thank you.

@antoinerousseau Version 1.4.1 is now published. It contains the fix for this issue (@yakupteke it will hopefully fix your issue as well). The full release notes are available here.

You can install this new version with npm:

npm install pdf-lib@1.4.1

It’s also available on unpkg:

As well as jsDelivr:

Still getting this error. I’m on latest (1.11.2) image

Link for the PDF file: https://www.icloud.com/iclouddrive/0de0B7Vuvr6gTeYYAn4c9GC_Q#large

Errors: image

Haha, indeed. They are useless as far as I can tell. Though, even if they aren’t, smallpdf.com could eliminate the extra whitespace in them. But they only contribute a few bytes to the total size of the document, so I guess it’s probably more effort than its worth for them.

@Hopding hi, I use "pdf-lib": "^1.17.1", in puppeteer, page.pdf([options] returns: <Promise> Promise which resolves with PDF buffer.

const buffer = await page.pdf([options];
  // Give the buffer to pdf-lib;
const pdfDoc = await PDFDocument.load(buffer);

error log:

err message: Failed to parse PDF document (line:47350 col:464825 offset=8891): Failed to parse invalid PDF object 
err stack:  Error: Failed to parse PDF document (line:47350 col:464825 offset=8891): Failed to parse invalid PDF object 
at PDFInvalidObjectParsingError.PDFParsingError [as constructor] (/node_modules/pdf-lib/cjs/core/errors.js:229:24) 
at new PDFInvalidObjectParsingError (/node_modules/pdf-lib/cjs/core/errors.js:262:24) 
at PDFParser.tryToParseInvalidIndirectObject (/node_modules/pdf-lib/cjs/core/parser/PDFParser.js:181:19) 
at PDFParser.<anonymous> (/node_modules/pdf-lib/cjs/core/parser/PDFParser.js:209:30)

I’m not sure if this is a bug or a concurrency problem, it’s not necessary, I found it in the error log, I look forward to your help, thank you very much

@deammer It looks like the problem you’re encountering is due to the fact that your PDF is encrypted. pdf-lib does not currently support encrypted documents.

Just to be clear, when I run the following script:

import { PDFDocument } from 'pdf-lib';

(async () => {
  const url = 'https://www.uscis.gov/sites/default/files/files/form/i-130.pdf';
  const i130Bytes = await fetch(url).then((res) => res.arrayBuffer());
  const pdfDoc = await PDFDocument.load(i130Bytes);
})();

It produces the following output:

Trying to parse invalid object: {"line":24,"column":6,"offset":5055})
Invalid object ref: 4 0 R
...
Trying to parse invalid object: {"line":3862,"column":6,"offset":563722})
Invalid object ref: 408 0 R
Error: Input document to `PDFDocument.load` is encrypted. You can use `PDFDocument.load(..., { ignoreEncryption: true })` if you wish to load the document anyways.
    at new EncryptedPDFError (/Users/user/github/pdf-lib/scratchpad/build/src/api/errors.js:11:24)
    at new PDFDocument (/Users/user/github/pdf-lib/scratchpad/build/src/api/PDFDocument.js:52:19)
    at Function.<anonymous> (/Users/user/github/pdf-lib/scratchpad/build/src/api/PDFDocument.js:123:47)
    at step (/Users/user/github/pdf-lib/node_modules/tslib/tslib.js:139:27)
    at Object.next (/Users/user/github/pdf-lib/node_modules/tslib/tslib.js:120:57)
    at fulfilled (/Users/user/github/pdf-lib/node_modules/tslib/tslib.js:110:62)

Notice the encryption error. The ignoreEncryption flag is explained here. It doesn’t actually do anything but suppress the error. I only added it to the library for backwards compatibility reasons. In hindsight it probably should not have been added.