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

How to create multiple signature annotations. #278

Closed
therpobinski opened this issue Dec 23, 2019 · 11 comments
Closed

How to create multiple signature annotations. #278

therpobinski opened this issue Dec 23, 2019 · 11 comments

Comments

@therpobinski
Copy link

Hi, I followed this guide-issue to create a placeholder, to sign a document.
Since this guide was made by @Hopding with old code, I was able to attach a new pdf-lib code, I am glad that I was able to solve the goal of creating a placeholder with this large pdf-lib library.
Now I have a doubt, when I sign several times it is necessary that AcroForm and /Type/Dict be updated with the new references of the annotations. How can I update this? I hope you have understood me and can give me an idea to solve the problem.
This is code, from line 180 I create the AcroForm.

@Hopding
Copy link
Owner

Hopding commented Dec 27, 2019

Hello @therpobinski!

If I understand correctly, you are asking how to add a new widget/annotation reference to an existing document's AcroForm dictionary? This is pretty straightforward to do. I've provided an example below:

const pdfDoc = await PDFDocument.load(...)
const annotationRef: PDFRef = ...
const acroForm = pdfDoc.catalog.lookupMaybe(PDFName.of('AcroForm'), PDFDict);
if (acroForm) {
  const fields = acroForm.lookup(PDFName.of('Fields'), PDFArray);
  fields.push(annotationRef)
} 

Please also note that I've provided an updated version of my signature example for the latest version of pdf-lib here: #112 (comment).

I hope this helps. Please let me know if you have any additional questions!

@Hopding Hopding closed this as completed Dec 27, 2019
@therpobinski
Copy link
Author

Hi, sorry to bother you again!
I'm still a little confused, with const annotationRef do you mean my widgetDict?
The const acroForm, should I do it before or after pdfDoc.catalog.set?
Or, I think in your if (acroForm) { statement that you put as an example, should there be an else where the pdfDoc.catalog.set goes?

@therpobinski
Copy link
Author

I have been able to solve the problem, I do not know if I am using it wrong or something, but when I sign for the first time everything is correct, but I do it for the second time, when placing my placeholder, I receive an error like the one in the attached photo ¿ Do you think you can give me an idea of ​​why it happens?
My first signature is correct but the second damaged the first.
And if I sign with that error, give me a pdf with the signatures panel with errors like this.

@Hopding
Copy link
Owner

Hopding commented Jan 7, 2020

@therpobinski The reason this happens is that the digest of the file stored in your first signature is invalidated when you add the second one. This happens because pdf-lib rewrites the file when you call const pdfDoc = PDFDocument.load(...); pdfDoc.save() such that it no longer matches the original file from which the digest was computed.

This should make intuitive sense. If you modify a file, the signature should be invalidated. After all, signatures are supposed to prove that a file has not been tampered with. So on the one hand, you can view this result as an indication that the digital signature process is working correctly.

However, as I'm sure you've observed before, there are plenty of PDFs out there with multiple signatures. And Acrobat allows you to add signatures to files, even after modifying them. This is possible thanks to a feature of PDFs called "Incremental Updates", documented in section 7.5.6 Incremental Updates of the PDF specification:

The contents of a PDF file can be updated incrementally without rewriting the entire file. When updating a PDF file incrementally, changes shall be appended to the end of the file, leaving its original contents intact.

Screen Shot 2020-01-06 at 7 03 28 PM

In Acrobat, when modifications are made to a signed document (including addition of a new signature), the changes necessary to arrive at the new state of the document are simply appended to the end of the file. This makes it possible to retrieve an earlier version of the document when a given signature was applied.

Unfortunately, as of today, pdf-lib does not support writing modifications as incremental update sections. It can only perform a full rewrite of the document. However, many of the necessary features already exist in pdf-lib to add an incremental update section. It's just a matter of putting all the pieces together. Support for incremental updates is a precondition for #172, so I've been thinking about how to add this capability to pdf-lib. But I have not begun implementing it yet.

@aallvi
Copy link

aallvi commented Aug 25, 2022

opding

hi, is now posible make incremental updates with pdf-lib? or have any idea how i can doi it?

Thanks.

@taotaow2n
Copy link

hi, is now posible make incremental updates with pdf-lib? or have any idea how i can doi it? thanks

@aallvi
Copy link

aallvi commented Oct 25, 2023

hi, is now posible make incremental updates with pdf-lib? or have any idea how i can doi it? thanks

i dont know, i solve my problem with the library endesive in python, is easy to use

@taotaow2n
Copy link

hi,bro ,this https://github.com/vbuch/node-signpdf can do it

@ShahMeet3001
Copy link

@Hopding @aallvi @taotaow2n @vbuch is it possible now to do incremental update with the pdb-lib? or are there any better alternatives to do a digital signature on a pdf already having digital signature ?

@vbuch
Copy link
Sponsor

vbuch commented Jan 11, 2024

@ShahMeet3001 I think pdf-lib doesn't have incremental updates yet. @signpdf/placeholder-plain can do the job in simple scenarios so it's worth checking. Once pdf-lib has the support, placeholder-plain will become obsolete.

@aallvi
Copy link

aallvi commented Jan 11, 2024

@Hopding @aallvi @taotaow2n @vbuch is it possible now to do incremental update with the pdb-lib? or are there any better alternatives to do a digital signature on a pdf already having digital signature ?

i dont know if you can do incremental update with pdf-lib, in my app I do this part with python with the library endesive

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

No branches or pull requests

6 participants