vbuch / node-signpdf

Simple signing of PDFs in node.
MIT License
700 stars 175 forks source link

Error: A PDF file must end with an EOF line. #35

Closed iopazo closed 3 years ago

iopazo commented 4 years ago

I had some problems when reading the pdf file from the buffer, since once called the function .sign (), this throws the following error:

/Users/iopazo/Develop/uno/transmision-api/node_modules/node-signpdf/dist/helpers/removeTrailingNewLine.js:40
     throw new _SignPdfError.default (
     ^

Error: A PDF file must end with an EOF line.

Imagine the problem was with one of my PDF files, but checking these if they have the final line.

Here is my integration:

    const signer = require('node-signpdf').default;

    let pdfBuffer = fs.readFileSync(file);

    const signedPdf = signer.sign(pdfBuffer, fs.readFileSync(signature));
    console.log(signedPdf);

comprobante_incorporacion.pdf

From already thank you very much!

vbuch commented 4 years ago

Here is the code that gives you an error: https://github.com/vbuch/node-signpdf/blob/develop/src/helpers/removeTrailingNewLine.js#L18

I am on mobile and with only a text editpr available I see this: http://imgur.com/gallery/dapeKIz This looks like an unexpected symbol at the end of the document but i am not sure what it is and if it is valid in PDF1.7. Need to research that when I have access to a computer.

vbuch commented 4 years ago

Yup. Now on a desktop I see it has a NUL byte at the end: https://imgur.com/gallery/0w9fNth Let me track what the spec has to say on that.

vbuch commented 4 years ago

OK. So the first finding is a bug. The specs say in section 7.5.1 that lines may end with \r, \n or both. This needs to be fixed when removing the last character. It also says "the last line shall contain only the EOF marker." in section 7.5.5. Considering that all lines end with \r, \n or both, I would say that the null byte at the end of this file makes it invalid, though the viewers tend to ignore it. I would not go for trimming the end of the file for this specific case. As per the spec, it is an invalid ending of the file.

PedroS11 commented 4 years ago

So how should I solve this problem ? All my pdf's have that null byte in the end and I can't trim a buffer.

vbuch commented 4 years ago

@PedroS11 If I was doing this for my PDFs I would go, get the last char and check it explicitly. If it is the one, I would feed the signer with a buffer that doesn't have it. This is: I would do it out of the library to keep the library closer to the specs.

vbuch commented 4 years ago

I think there is not much to add here, unless this is really an issue with the lib which I think is not the case. Closing.

manashee commented 3 years ago

Dear Valery , Any luck with fixing the issue: The lines may end with \r, \n or both?

I have many pdfs ( generated from magento ) which have only \r before %%EOF. (There is no Null charecter after EOF) and node-signpdf produces the above error "Error: A PDF file must end with an EOF line."

vbuch commented 3 years ago

https://github.com/vbuch/node-signpdf/blob/83c7abcb31815b774a84cf910b258a7be1236945/src/helpers/removeTrailingNewLine.js#L32

Noone has fixed that, so... PRs welcome.

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had activity in the past 90 days. It will be closed if no further activity occurs. Thank you for your contributions.

romulotorres commented 3 years ago

I'm facing this error too. Something news about fix that?

mangoub99 commented 2 years ago

Hi , i have the same issue and i would like to know if there is a fix . i Have a pdf file with %EOF but i always recieve the same error