vbuch / node-signpdf

Simple signing of PDFs in node.
MIT License
711 stars 178 forks source link

Error occured when sign after Adobe Acrobat #172

Closed totalism53 closed 1 year ago

totalism53 commented 2 years ago

Describe the bug and the expected behaviour I tried to sign the document after the document signed on Adobe Acrobat but I found that after using node-signpdf there is an error which is "SignPdfError: Expected xref at NaN but found other content." but if I use this lib as the starter, it can be sign twice or more without any error.

Is it a bug in signing or in the helpers? I guess it might be in signing.

To Reproduce Here is the code and file that I used.

import { SignPdf } from 'node-signpdf'; import fs from 'fs'; import { Buffer } from 'buffer'; import { plainAddPlaceholder } from 'node-signpdf/dist/helpers/index.js';

const pathSignedPdf = 'resource/pdf_a_signed_signed.pdf' const p12Buffer = fs.readFileSync('resource/certificate.p12'); const pdfBuffer = fs.readFileSync('resource/pdf_a_signed.pdf'); const pdfBufferToSign = plainAddPlaceholder({ pdfBuffer, reason: 'twice', signatureLength: p12Buffer.length }); const signer = new SignPdf() const signedPdf = signer.sign(pdfBufferToSign, p12Buffer); console.log(signedPdf)

const bufferPdf = Buffer.from(signedPdf) fs.createWriteStream(pathSignedPdf).write(bufferPdf);

Here is the log from console.log(prefix.toString().replace(/\s*/g, '')) in readRefTable.js

%PDF-1.7%����1210obj<</Linearized1/L217009/O123/E207511/N1/T216681/H[546239]>>endobj1490obj<</DecodeParms<</Columns5/Predictor12>>/Filter/FlateDecode/ID[<9AFE1C437623C840A385A0AECE1DFA20>]/Index[12158]/Info1200R/Length124/Prev216682/Root1220R/Size179/Type/XRef/W[131]>>streamh�bbd`bf���LN�A$C�d�"o��\O��6�Is0��,&σI�Ȭf��H�P��g��.���`O�"�>�T�E�����t-/�=��������)@����endstreamendobjstart

Is there any suggestion or example?

Thank you for your help.

the original file signed by acrobat signed twice with node-signpdf

bretto36 commented 2 years ago

@totalism53 I'm getting this same issue. But only for one document. Other documents work fine

vbuch commented 2 years ago

Some features of PDF > 1.3 such as streams and linearization are not supported. Your signed document uses those.

https://github.com/vbuch/node-signpdf/issues?q=+label%3A%22PDF+%3E+1.3%22

bretto36 commented 2 years ago

@vbuch Yes this is a PDF version 1.4 document. The code i've written the PDF is generated by using a chrome headless print to pdf, so i have only THIS document not working, but all the other 1000+ documents have worked.

I'm quite confused as to why it's only the one document.

trailer
<</Size 206
/Root 29 0 R
/Info 1 0 R>>
startxref
137505
%%EOF

I don;t know what else to try to debug it. i'm on 1.5.0

It just can't seem to determine the position to add the signature when calling plainAddPlaceholder

stale[bot] commented 1 year 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.

bretto36 commented 1 year ago

Just for other people's awareness. I was using version 1.3.3. I had the word "trailer" in the content i was adding. And it caused the error. What a mission to figure that out.