node-signpdf icon indicating copy to clipboard operation
node-signpdf copied to clipboard

multiple signature using `placeholder-pdf-lib` package

Open satyajitnayk opened this issue 1 year ago • 8 comments

The given example works & it uses placeholder-plain. image

I want to achieve the same using placeholder-pdf-lib, but signed pdf has only 1 invalid signature image

import { pdflibAddPlaceholder } from '@signpdf/placeholder-pdf-lib';
import { P12Signer } from '@signpdf/signer-p12';
import { SignPdf } from '@signpdf/signpdf';
import { PDFDocument } from 'pdf-lib';

 function writeFile(filename, buffer) {
  fs.writeFileSync(`resources/${filename}`, buffer);
}

 function readFile(filename) {
  return fs.readFileSync(`resources/${filename}`);
}

async function buyerSign(pdfBuffer) {
  const pdfDoc = await PDFDocument.load(pdfBuffer);
  // Add a placeholder for John Doe - the customer
  pdflibAddPlaceholder({
    pdfDoc,
    reason: 'Agrees to buy the truck trailer.',
    contactInfo: '[email protected]',
    name: 'John Doe',
    location: 'Free Text Str., Free World',
  });
  const pdfWithPlaceholder = await pdfDoc.save();

  // John signs the PDF
  // certificate.p12 is the certificate that is going to be used to sign
  const certificateBuffer = readFile('certificate.p12');
  var signer = new P12Signer(certificateBuffer);
  const signPdf = new SignPdf();

  return signPdf.sign(pdfWithPlaceholder, signer).then(function (signedPdf) {
    // signedPdf is a Buffer of an electronically signed PDF. Store it.
    // writeFile(targetPath, signedPdf);
    return signedPdf;
  });
}

async function sellerSign(pdfBuffer) {
  const pdfDoc = await PDFDocument.load(pdfBuffer);

  pdflibAddPlaceholder({
    pdfDoc,
    reason: 'Agrees to sell a truck trailer to John Doe.',
    contactInfo: '[email protected]',
    name: 'Thug Dealer',
    location: 'Automotive Str., Free World',
  });
  const pdfWithPlaceholder = await pdfDoc.save();

  const certificateBuffer = readFile('certificate2.p12');
  var signer = new P12Signer(certificateBuffer);
  const signPdf = new SignPdf();
  return signPdf.sign(pdfWithPlaceholder, signer).then(function (signedPdf) {
    // signedPdf is a Buffer of an electronically signed PDF. Store it.
    // writeFile(targetPath, signedPdf);
    return signedPdf;
  });
}


async function work() {
  const pdfBuffer = readFile('input.pdf');
  // A copy of the PDF is signed by the buyer and then by the seller.
  buyerSign(pdfBuffer).then(async function (signedByCustomer) {
    const signedPdf = await sellerSign(signedByCustomer);
    writeFile('buyer_seller_signed_pdflib.pdf', signedPdf);
  });

  // A copy of the PDF is signed by the seller and then by the buyer.
  // sellerSign(pdfBuffer).then(async function (signedBySeller) {
  //   const signedPdf = await buyerSign(signedBySeller);
  //   writeFile('seller_buyer_signed_pdflib.pdf', signedPdf);
  // });
}

work();

satyajitnayk avatar Oct 21 '24 05:10 satyajitnayk

I think pdf-lib still doesn't have incremental updates so you want be able to do it with it. I've seen eople having forks or patches to do that. https://github.com/Hopding/pdf-lib/issues/816#issuecomment-926229228

vbuch avatar Oct 22 '24 07:10 vbuch

Since placeholder-plain supports incremental signing, how could I attach a physical signature to the pdf before each signing ?

satyajitnayk avatar Oct 22 '24 13:10 satyajitnayk

You can use any PDF program, such as PDF-lib or PDF-kit, to add that as an image. And my doubt, is it possible for us to choose the page —such as first or second pages—where the placeholder should go?

vp62 avatar Oct 25 '24 17:10 vp62

pdf-lib does not support incremental update so not useful for multiple signature as as per this. pdf kit does not support editing pdf files as discussed here!

satyajitnayk avatar Oct 29 '24 12:10 satyajitnayk

Hi @satyajitnayk Like you said, we can done incremental signature using placeholder-plain, before adding placeholder you can attach your signature image using pdf-lib. With pdf-lib output buffer you can iteratively add plain-placeholder with widgetRect of signature position on pdf. But the draw back of using it, like we can only able to sign on 1st page. If we know how to make Multiple sign on multiple page then it's perfect. Hello @vbuch, kindly help us to solve this problem. Thanks in advance💗

vp62 avatar Oct 29 '24 16:10 vp62

Hi, I believe the problem originates from the repetition of the value /T (Signature1) when adding the placeholder. Below is an example of a PDF file in text format. pdftest.txt

thiago-gladstone avatar Nov 19 '24 17:11 thiago-gladstone

Created pull-request #289, that "fixes" this problem, by using a pdf-lib library with incremental updates.

adnsistemas avatar May 09 '25 19:05 adnsistemas

use placeholder-plain

Thanks @satyajitnayk . The sample with pdf-lib and placeholder-plain worked for me. but there still is an issue, i recognized that only works for original PDF without modification from pdf-lib.

i created new issue #296

taitrd avatar Oct 24 '25 04:10 taitrd