Hopding / pdf-lib

Create and modify PDF documents in any JavaScript environment
https://pdf-lib.js.org
MIT License
6.89k stars 656 forks source link

Bad FCHECK in flate stream: 120, 239 from AWS lambda #1496

Closed ScopeSV closed 1 year ago

ScopeSV commented 1 year ago

What were you trying to do?

Hi.

Not sure if this is a bug, but its not working correctly for me.

I hope someone has some pointers or a workaround :)


I am appending images and pdfs to a new pdf created with this library. It works correctly locally, but not when ran in AWS lambda, I get the error Bad FCHECK in flate stream: 120, 239. This only happens on PDFs though. Both the local server and the lambda uses the same AWS environment with the same S3 bucket where documents are stored. When using a jpeg or png, the PDF is saved as expected, however with pdfs the error is raised.

I've tried with multiple types of pdfs, and pdfs versions. The error is identical. I've tried both x86 and arm64 architecture on the lambda (as I'm working on a mac)

stack trace:

Error: Bad FCHECK in flate stream: 120, 239
    at new FlateStream (/var/task/node_modules/pdf-lib/cjs/core/streams/FlateStream.js:126:19)
    at decodeStream (/var/task/node_modules/pdf-lib/cjs/core/streams/decode.js:18:16)
    at exports.decodePDFRawStream (/var/task/node_modules/pdf-lib/cjs/core/streams/decode.js:47:18)
    at PDFPageEmbedder.decodeContents (/var/task/node_modules/pdf-lib/cjs/core/embedders/PDFPageEmbedder.js:76:36)
    at PDFPageEmbedder.<anonymous> (/var/task/node_modules/pdf-lib/cjs/core/embedders/PDFPageEmbedder.js:46:40)
    at step (/var/task/node_modules/pdf-lib/node_modules/tslib/tslib.js:141:27)
    at Object.next (/var/task/node_modules/pdf-lib/node_modules/tslib/tslib.js:122:57)
    at /var/task/node_modules/pdf-lib/node_modules/tslib/tslib.js:115:75
    at new Promise (<anonymous>)
    at Object.__awaiter (/var/task/node_modules/pdf-lib/node_modules/tslib/tslib.js:111:16)

How did you attempt to do it?

    #addPageWithPDFs = (pdf: PDFDocument, embeddedPDF: PDFEmbeddedPage[]) => {
        for (let i = 0; i < embeddedPDF.length; i++) {
            const page = pdf.addPage()
            page.drawPage(embeddedPDF[i], {})
        }
    }

This is the method that gets called if the mimetype is pdf. It creates a new page, and puts the pdf to embed on it.

What actually happened?

Everything seems to work as expected until the save function is called


const convert = async (
    docs: DocumentS3[],
    expense: Expense
): Promise<Uint8Array> => {
    const PDF = await PDFDocument.create()

    await new PDFDocsEmbedder(PDF).embed(docs)
    // everything till here is ok. It crashes on the next line
    return await PDF.save()
}

### What did you expect to happen?

I would expect the pdf to be created on the AWS lambda, as it is created on the local server

### How can we reproduce the issue?

Hard to reproduce, as it requires an aws account with a lambda and all that comes with that.
Its not necessary to provide specific files, as this happens on all PDFs that is being embedded on the server.

### Version

1.17.1

### What environment are you running pdf-lib in?

Node

### Checklist

- [X] My report includes a Short, Self Contained, Correct (Compilable) Example.
- [X] I have attached all PDFs, images, and other files needed to run my SSCCE.

### Additional Notes

_No response_
kfachas commented 4 months ago

Hello, i got the same error, how did u fix it ? Am getting this error in local, on macbook pro m2