gettalong / hexapdf

Versatile PDF creation and manipulation for Ruby
https://hexapdf.gettalong.org
Other
1.24k stars 70 forks source link

Signatures fail when add an image to it #186

Closed marcelopedras-ufvjm closed 2 years ago

marcelopedras-ufvjm commented 2 years ago

hexapdf version: 0.23.0

How to add an image to the signature? When I try with the code below, it gives an index error on line 250 of signatures.rb:

`add': undefined method `[]' for nil:NilClass (NoMethodError)
        signature_length = data[index + 1][0] - data[index][0]
require 'hexapdf'
require 'byebug'
require HexaPDF.data_dir + '/cert/demo_cert.rb'

doc = HexaPDF::Document.open(ARGV[0])

image_bin = File.new('/pathtoimage/image.jpg', 'rb')

sig_field = doc.acro_form(create: true).create_signature_field('signature')
widget = sig_field.create_widget(doc.pages[0], Rect: [0, 0, 150, 150])
widget.create_appearance.canvas
      .stroke_color('red').rectangle(1, 1, 149, 149).stroke
      .font('Helvetica', size: 10)
      .text('Certified by signer', at: [10, 10])
      .image(image_bin, at: [10, 10], height: 50, width: 50)

doc.sign('signed.pdf', signature: sig_field,
                       reason: 'Some reason',
                       certificate: HexaPDF.demo_cert.cert,
                       key: HexaPDF.demo_cert.key,
                       certificate_chain: [HexaPDF.demo_cert.sub_ca,
                                           HexaPDF.demo_cert.root_ca])
gettalong commented 2 years ago

I can confirm the error and I'm looking at what may cause it.

Edit: Thanks for the concise bug report containing everything to quickly reproduce the problem! :+1:

gettalong commented 2 years ago

I have found the problem (index out of bounds due the selected index being the last one in the array but the algorithm expecting it not to be) and will find a fix for it.

gettalong commented 2 years ago

I have a fix for this problem and will incorporate it into the next release.

marcelopedras-ufvjm commented 2 years ago

Thank you Thomas!

gettalong commented 2 years ago

Added an appropriate test case and implemented the fix - will be in the next release.