remdra / sign-pdf-lib

10 stars 3 forks source link

Library creates new signature instead of signing existing signature field #2

Closed stanvanheumen closed 8 months ago

stanvanheumen commented 8 months ago

We are currently facing an issue with sign-pdf-lib. Despite our best efforts and thorough research, we have been unable to successfully sign our PDF correctly using this library.

View our code below, we have attached the PDF as well. When we try to sign our PDF (which contains a signature field already) it creates a new signature instead of signing the existing signature field.

How can we (visually and digitally) sign our PDF using your library?

async function main() {
    const pdfFilePath = path.join(__dirname, 'Personal_History_Disclosure_Form_v01092023.pdf');
    const pdfBuffer = fs.readFileSync(pdfFilePath);

    const pdfSigner = new PdfSigner({
        certificatePassword: '123.123',
        p12Certificate: fs.readFileSync('./key.p12'),
        signatureLength: 8192,
        rangePlaceHolder: '**********'
    });

    const pdfOutput = await pdfSigner.signAsync(pdfBuffer, {
        pageNumber: 9,
        name: 'Name',
        location: 'Location',
        reason: 'Signing',
        contactInfo: 'info@email.com',
        modified: new Date(),
        visual: {
            image: fs.readFileSync('./signature.png'),
            imageRectangle: {
                left: 10,
                top: 10,
                right: 10,
                bottom: 10
            }
        }
    });

    const outputPdfPath = path.join(__dirname, 'signed.pdf');
    fs.writeFileSync(outputPdfPath, pdfOutput);
}

Personal_History_Disclosure_Form_v01092023.pdf

remdra commented 8 months ago

Indeed the library is designed to sign from scratch a file. I will have a look at your scenario (sign in a signature field). This is a busy week for me, I'll have time for it next week.

remdra commented 8 months ago

Feature added in 1.0.15. Documentation updated. The field you want to sign is called 'Signature28'. For the moment both background and texts have to be provided. This will be fixed in the next version.

Q: I want to add a regression test for your pdf. Is it ok to add it to git repository?

stanvanheumen commented 8 months ago

Great work! I've just tested it and I can now successfully digitally sign the PDF. Yes, you can add the PDF to the git repository.

I did however get a small issue with the background property. I've tried to resize the image to the dimensions of the signature widget in the PDF file (width: 128px; height: 58px): signature_test

The resulting drawn image looks like this:

Scherm­afbeelding 2023-11-05 om 19 46 56

Is there something I can do to fix this?

remdra commented 8 months ago

I know that there are some issues with the visual aspect. I'm working on. Also same text should be visible (it is in fact in the pdf reder in Ubuntu, but not in Adobe Reader on windows).

You can solve your problem by providing an empty array for texts: const parameters: SignFieldParameters = { fieldName: 'Signature1,

name: 'Test Signer',
location: 'Timisoara',
reason: 'Signing',
contactInfo: 'signer@semnezonline.ro',

background: await fse.readFile(...),
texts: [  ]

};

remdra commented 8 months ago

Now both background and texts are displayed (version 1.0.16).