Merge pdf files(with form fields) issue with pdf-lib

354 Views Asked by At

I am trying to merge multiple files using pdf-lib. The code is working fine for most of the file but it gives error when there are form fields/fillable fields in the file.

Trying to parse invalid object: {"line":228,"column":16,"offset":32962})
Invalid object ref: 457 0 R
Trying to parse invalid object: {"line":336,"column":16,"offset":244737})
Invalid object ref: 6 0 R
Trying to parse invalid object: {"line":344,"column":16,"offset":245437})
Invalid object ref: 8 0 R
Trying to parse invalid object: {"line":352,"column":16,"offset":245879})
Invalid object ref: 9 0 R
Trying to parse invalid object: {"line":358,"column":16,"offset":246012})
Invalid object ref: 10 0 R
Trying to parse invalid object: {"line":364,"column":16,"offset":246204})
Invalid object ref: 11 0 R
Trying to parse invalid object: {"line":382,"column":16,"offset":248201})
Invalid object ref: 12 0 R
Trying to parse invalid object: {"line":402,"column":16,"offset":249457})
Invalid object ref: 13 0 R
Trying to parse invalid object: {"line":416,"column":16,"offset":250897})
Invalid object ref: 14 0 R
Trying to parse invalid object: {"line":430,"column":16,"offset":257366})
Invalid object ref: 18 0 R
Trying to parse invalid object: {"line":436,"column":16,"offset":257515})
Invalid object ref: 19 0 R
ERROR: Error: Expected instance of PDFDict, but got instance of undefined
    at new UnexpectedObjectTypeError (/Users/banjaxedpk/Documents/salesforce-litify-backend/node_modules/pdf-lib/src/core/errors.ts:31:5)
    at PDFContext.lookup (/Users/banjaxedpk/Documents/salesforce-litify-backend/node_modules/pdf-lib/src/core/PDFContext.ts:166:11)
    at PDFCatalog.PDFDict.lookup (/Users/banjaxedpk/Documents/salesforce-litify-backend/node_modules/pdf-lib/src/core/objects/PDFDict.ts:143:38)
    at PDFCatalog.Pages (/Users/banjaxedpk/Documents/salesforce-litify-backend/node_modules/pdf-lib/src/core/structures/PDFCatalog.ts:24:17)
    at Cache.PDFDocument.computePages [as populate] (/Users/banjaxedpk/Documents/salesforce-litify-backend/node_modules/pdf-lib/src/api/PDFDocument.ts:1366:18)
    at Cache.access (/Users/banjaxedpk/Documents/salesforce-litify-backend/node_modules/pdf-lib/src/utils/Cache.ts:17:40)

This is my code.

        const doc = await PDFDocument.create();

        const content = await PDFDocument.load(body, {
          ignoreEncryption: true,
        });
        const contentPages = await doc.copyPages(
          content,
          content.getPageIndices()
        );
        for (const page of contentPages) {
          doc.addPage(page);
        };
        doc.save()

Is there any other library or is there any way that pdf-lib can the handles such files?

0

There are 0 best solutions below