vbuch / node-signpdf

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

Illegal values in ByteRange array/ Signature has not yet been verified #251

Open stefanmiodrag opened 5 months ago

stefanmiodrag commented 5 months ago

When I run pdfsig <document_path> I get the following error:

Syntax Error (0): Illegal values in ByteRange array
  - Signer Certificate Common Name: (null)
  - Signer full Distinguished Name: (null)
  - Signing Time: Apr 21 2024 19:09:32
  - Signing Hash Algorithm: unknown
  - Signature Type: adbe.pkcs7.detached
  - Signed Ranges: [0 - 25497], [41883 - 43123]
  - Not total document signed
  - Signature Validation: Signature has not yet been verified.

My code looks like this:

export async function POST(request: Request) {
    try {
        const { pdfBase64 } = await request.json();
        const pdfBuffer = Buffer.from(pdfBase64, "base64");

        // certificate.p12 is the certificate that is going to be used to sign
        const certificatePath = join(process.cwd(), "/public/client-identity.p12");
        const certificateBuffer = fs.readFileSync(certificatePath);

        const signer = new P12Signer(certificateBuffer);

        const pdfWithPlaceholder = plainAddPlaceholder({
            pdfBuffer,
            reason: "The user is decalaring consent.",
            contactInfo: "signpdf@example.com",
            name: "John Doe",
            location: "Free Text Str., Free World",
        });

        // pdfWithPlaceholder is now a modified buffer that is ready to be signed.
        const signedPdf = await signpdf.sign(pdfWithPlaceholder, signer);
        const signedPdfBase64 = signedPdf.toString('base64');
        console.log(signedPdf);
        return NextResponse.json({ signedPdfBase64 }, { status: 200 });
    } catch (error) {
        console.error(error);
        return NextResponse.json(error, { status: 500 });
    }
}