I would like to digital sign several times (twice would be fine) a PDF using Ruby on Rails.
I have tried using Origami Gem which works great for a single signature (thank you MrWater for your very helpful post: to Insert digital signature into existing pdf file)
However, I can't sign twice the document. When I do, using the same method, my pdf file displays a signature error (signature invalid).
Do you have any idea of how to make that work using Origami or any other Ruby Gem?
Thank you in advance for your help.
Update 2015-10-25:
You will find below my code to sign a document (maybe it can help to find a solution to the problem, and at least it shows you how to make a single signature, which works quite fine). In comment is my failing attempt for a double signature. I also tried to sign a first time doing the whole process, and sign a second time with the same process but without any success:
PDF_ORI = "contrat_old.pdf"
PDF_NEW = "contrat_new.pdf"
pdf = Origami::PDF.read(PDF_ORI)
# Open certificate files
key = OpenSSL::PKey::RSA.new 2048
cert = OpenSSL::X509::Certificate.new
cert.version = 2
cert.serial = 0
cert.not_before = Time.now
cert.not_after = Time.now + 10 * 365 * 24 * 60 * 60 # 10 years validity
cert.public_key = key.public_key
cert.issuer = OpenSSL::X509::Name.parse('CN=Test')
cert.subject = OpenSSL::X509::Name.parse('CN=test1 ESSAI1')
# Open certificate files
#key2 = OpenSSL::PKey::RSA.new 2048
#cert2 = OpenSSL::X509::Certificate.new
#cert2.version = 2
#cert2.serial = 0
#cert2.not_before = Time.now
#cert2.not_after = Time.now + 10 * 365 * 24 * 60 * 60 # 10 years validity
#cert2.public_key = key2.public_key
#cert2.issuer = OpenSSL::X509::Name.parse('CN=Test2')
#cert2.subject = OpenSSL::X509::Name.parse('CN=test2 ESSAI2')
sigannot = Origami::Annotation::Widget::Signature.new
sigannot.Rect = Origami::Rectangle[:llx => 89.0, :lly => 386.0, :urx => 190.0, :ury => 353.0]
pdf.get_page(1).add_annot(sigannot)
#sigannot2 = Origami::Annotation::Widget::Signature.new
#sigannot2.Rect = Origami::Rectangle[:llx => 89.0, :lly => 386.0, :urx => 190.0, :ury => 353.0]
#pdf.get_page(1).add_annot(sigannot2)
# Sign the PDF with the specified keys
pdf.sign(cert, key,
:method => 'adbe.pkcs7.sha1',
:annotation => sigannot,
:location => "France",
:contact => "tmp@security.org",
:reason => "Proof of Concept"
)
# Sign the PDF with the specified keys
#pdf.sign(cert2, key2,
# :method => 'adbe.pkcs7.sha1',
# :annotation => sigannot2,
# :location => "France",
# :contact => "tmp@security.org",
# :reason => "Proof of Concept"
#)
# Save the resulting file
pdf.save(PDF_NEW)
I know it is quite tricky, but no one can help me? Or using another solution maybe?