pdf-lib icon indicating copy to clipboard operation
pdf-lib copied to clipboard

Trying to parse invalid object

Open antoinerousseau opened this issue 6 years ago • 21 comments

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?

antoinerousseau avatar Feb 29 '20 21:02 antoinerousseau

Hello @antoinerousseau!

The warnings will not cause you any issues. pdf-lib's parser failed to parse those objects, but that's because they are a very specific type of empty object that don't do anything in your document anyways. Regardless, I fixed the parser bug in #370. So when the next release of pdf-lib goes out, you will no longer see these warnings.

Hopding avatar Mar 01 '20 00:03 Hopding

Wow, so fast, thanks! So I wonder, those extra whitespace-like characters, are they useless? If so, I guess smallpdf.com can further reduce the output size of the PDF in their code ;)

antoinerousseau avatar Mar 01 '20 12:03 antoinerousseau

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 avatar Mar 01 '20 14:03 Hopding

Hi, I have the same issue.

Trying to parse invalid object: {"line":342,"column":6,"offset":132428})
Invalid object ref: 389 0 R
Trying to parse invalid object: {"line":393,"column":6,"offset":137107})
Invalid object ref: 390 0 R

How can I solve this issue? Thanks.

yakupteke avatar Mar 04 '20 10:03 yakupteke

@yakupteke did you read this thread?...

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

antoinerousseau avatar Mar 04 '20 12:03 antoinerousseau

@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 [email protected]

It's also available on unpkg:

As well as jsDelivr:

Hopding avatar Mar 07 '20 17:03 Hopding

Thanks a lot.

yakupteke avatar Mar 09 '20 11:03 yakupteke

Hi @Hopding, I seem to be running into the same issue when I delete a page from some PDF files, like this one. I'm using 1.4.1 and seeing the console warnings below:

Trying to parse invalid object: {"line":24,"column":6,"offset":5055}) PDFParser.js:213:12
Invalid object ref: 4 0 R PDFParser.js:215:12
Trying to parse invalid object: {"line":152,"column":6,"offset":21555}) PDFParser.js:213:12
Invalid object ref: 8 0 R PDFParser.js:215:12
Trying to parse invalid object: {"line":273,"column":6,"offset":40714}) PDFParser.js:213:12
Invalid object ref: 12 0 R PDFParser.js:215:12
Trying to parse invalid object: {"line":390,"column":6,"offset":55361}) PDFParser.js:213:12
Invalid object ref: 16 0 R PDFParser.js:215:12
Trying to parse invalid object: {"line":439,"column":6,"offset":61912}) PDFParser.js:213:12
Invalid object ref: 20 0 R PDFParser.js:215:12
Trying to parse invalid object: {"line":504,"column":6,"offset":69247}) PDFParser.js:213:12
Invalid object ref: 24 0 R PDFParser.js:215:12
Trying to parse invalid object: {"line":566,"column":6,"offset":77542}) PDFParser.js:213:12
Invalid object ref: 28 0 R PDFParser.js:215:12
Trying to parse invalid object: {"line":641,"column":6,"offset":99802}) PDFParser.js:213:12
Invalid object ref: 34 0 R PDFParser.js:215:12
Trying to parse invalid object: {"line":700,"column":6,"offset":106641}) PDFParser.js:213:12
Invalid object ref: 38 0 R PDFParser.js:215:12
Trying to parse invalid object: {"line":755,"column":6,"offset":112760})

Do you have any idea why that would be happening?

Sample relevant code, nothing too wild:
const deleteFirstPage = async (pdfUrl: string) => {
  const pdfBytes: ArrayBuffer = await axios
    .get(pdfUrl, {
      responseType: 'arraybuffer'
    })
    .then(res => res.data as ArrayBuffer)

  const document = await PDFDocument.load(pdfBytes)
  document.removePage(1)

  const updatedPdfBytes = await document.save()

  return new Blob([updatedPdfBytes], { type: 'application/pdf' })
}

deammer avatar Apr 13 '20 23:04 deammer

I just installed this package.. Im using 1.4.1 and i still get the parse invalid object error. Any idea? Thanks

kimsean avatar Apr 14 '20 03:04 kimsean

@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.

Hopding avatar Apr 16 '20 23:04 Hopding

@kimsean Can you share the document that is causing the error?

Hopding avatar Apr 16 '20 23:04 Hopding

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

pedro-surf avatar Oct 17 '20 16:10 pedro-surf

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

same to me

tranghuyen215 avatar Oct 29 '20 08:10 tranghuyen215

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.

dbaq avatar Jul 02 '21 15:07 dbaq

Same as dbaq, on 1.16.0: image

ccoddington avatar Aug 03 '21 18:08 ccoddington

The same here!

bs-pdf.entry.js:16547 Trying to parse invalid object: {"line":12386,"column":19174,"offset":178542}) PDFParser.tryToParseInvalidIndirectObject @ bs-pdf.entry.js:16547 (anonymous) @ bs-pdf.entry.js:16590 step @ bs-pdf.entry.js:102 (anonymous) @ bs-pdf.entry.js:83 rejected @ bs-pdf.entry.js:74 Promise.then (async) step @ bs-pdf.entry.js:75 fulfilled @ bs-pdf.entry.js:73 Promise.then (async) step @ bs-pdf.entry.js:75 fulfilled @ bs-pdf.entry.js:73 Promise.then (async) step @ bs-pdf.entry.js:75 fulfilled @ bs-pdf.entry.js:73 Promise.then (async) step @ bs-pdf.entry.js:75 fulfilled @ bs-pdf.entry.js:73 Promise.then (async) step @ bs-pdf.entry.js:75 fulfilled @ bs-pdf.entry.js:73 Promise.then (async) step @ bs-pdf.entry.js:75 fulfilled @ bs-pdf.entry.js:73 Promise.then (async) step @ bs-pdf.entry.js:75 fulfilled @ bs-pdf.entry.js:73 Promise.then (async) step @ bs-pdf.entry.js:75 fulfilled @ bs-pdf.entry.js:73 Promise.then (async) step @ bs-pdf.entry.js:75 fulfilled @ bs-pdf.entry.js:73 Promise.then (async) step @ bs-pdf.entry.js:75 fulfilled @ bs-pdf.entry.js:73 Promise.then (async) step @ bs-pdf.entry.js:75 fulfilled @ bs-pdf.entry.js:73 Promise.then (async) step @ bs-pdf.entry.js:75 (anonymous) @ bs-pdf.entry.js:76 __awaiter @ bs-pdf.entry.js:72 PDFParser.parseIndirectObjects @ bs-pdf.entry.js:16569 (anonymous) @ bs-pdf.entry.js:16669 step @ bs-pdf.entry.js:102 (anonymous) @ bs-pdf.entry.js:83 (anonymous) @ bs-pdf.entry.js:76 __awaiter @ bs-pdf.entry.js:72 PDFParser.parseDocumentSection @ bs-pdf.entry.js:16666 (anonymous) @ bs-pdf.entry.js:16434 step @ bs-pdf.entry.js:102 (anonymous) @ bs-pdf.entry.js:83 (anonymous) @ bs-pdf.entry.js:76 __awaiter @ bs-pdf.entry.js:72 PDFParser.parseDocument @ bs-pdf.entry.js:16421 (anonymous) @ bs-pdf.entry.js:23008 step @ bs-pdf.entry.js:102 (anonymous) @ bs-pdf.entry.js:83 (anonymous) @ bs-pdf.entry.js:76 __awaiter @ bs-pdf.entry.js:72 PDFDocument.load @ bs-pdf.entry.js:22997 downloadPDF @ bs-pdf.entry.js:25776 onClick @ bs-pdf.entry.js:25794 bs-pdf.entry.js:16549 Invalid object ref: 36 0 R

JMAmimacom avatar Sep 26 '21 09:09 JMAmimacom

If anybody is having trouble with this (for non-encrypted PDFs), please create a new bug report

Hopding avatar Oct 01 '21 19:10 Hopding

Does anyone know a way around this error ?

the same erro

pdf: https://drive.google.com/file/d/14K67CUc5fZ3XPbXYlHQPUW3PXufxPRVZ/view?usp=sharing

DouglasHFonseca avatar Apr 15 '22 00:04 DouglasHFonseca

@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

rui-ys avatar May 11 '22 09:05 rui-ys

Still getting this error: image

Any idea on how to fix it?

gerardburgues avatar Aug 21 '24 07:08 gerardburgues