Skip to content

multiple signature using placeholder-pdf-lib package #273

@satyajitnayk

Description

@satyajitnayk

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: 'john@example.com',
    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: 'dealer@example.com',
    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();

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions