Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multiple signature using placeholder-pdf-lib package #273

Open
satyajitnayk opened this issue Oct 21, 2024 · 6 comments
Open

multiple signature using placeholder-pdf-lib package #273

satyajitnayk opened this issue Oct 21, 2024 · 6 comments

Comments

@satyajitnayk
Copy link

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();
@vbuch
Copy link
Owner

vbuch commented Oct 22, 2024

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. Hopding/pdf-lib#816 (comment)

@satyajitnayk
Copy link
Author

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

@vp62
Copy link

vp62 commented Oct 25, 2024

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?

@satyajitnayk
Copy link
Author

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!

@vp62
Copy link

vp62 commented Oct 29, 2024

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💗

@thiago-gladstone
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants
@vbuch @satyajitnayk @thiago-gladstone @vp62 and others