add signature id

This commit is contained in:
Pete Matsyburka
2024-07-24 20:55:35 +03:00
parent 09074e2ac0
commit 43db15b350
18 changed files with 236 additions and 12 deletions
+4
View File
@@ -102,6 +102,8 @@ module Submissions
.rectangle(0, 0, box.width, 20)
.rectangle(0, box.height - 20, box.width, 20)
.fill
maybe_add_background(canvas, submission, page_size)
end
style.frame = style.create_frame(canvas.context, 50)
end
@@ -336,6 +338,8 @@ module Submissions
'Signed with DocuSeal.co'
end
def maybe_add_background(_canvas, _submission, _page_size); end
def add_logo(column, _submission = nil)
column.image(PdfIcons.logo_io, width: 40, height: 40, position: :float)
@@ -88,6 +88,9 @@ module Submissions
def generate_pdfs(submitter)
cell_layouter = HexaPDF::Layout::TextLayouter.new(text_valign: :center, text_align: :center)
with_signature_id = submitter.account.account_configs
.exists?(key: AccountConfig::WITH_SIGNATURE_ID, value: true)
is_flatten =
submitter.account.account_configs
.find_or_initialize_by(key: AccountConfig::FLATTEN_RESULT_PDF_KEY).value != false
@@ -97,6 +100,22 @@ module Submissions
pdfs_index = build_pdfs_index(submitter, flatten: is_flatten)
if with_signature_id
pdfs_index.each_value do |pdf|
next if pdf.trailer.info[:DocumentID].present?
pdf.trailer.info[:DocumentID] = Digest::MD5.hexdigest(submitter.submission.slug).upcase
pdf.pages.each do |page|
font_size = (([page.box.width, page.box.height].min / A4_SIZE[0].to_f) * 9).to_i
cnv = page.canvas(type: :overlay)
cnv.font(FONT_NAME, size: font_size)
cnv.text("Document ID: #{Digest::MD5.hexdigest(submitter.submission.slug).upcase}",
at: [2, 4])
end
end
end
submitter.submission.template_fields.each do |field|
next if field['submitter_uuid'] != submitter.uuid
@@ -150,6 +169,69 @@ module Submissions
canvas.font(FONT_NAME, size: font_size)
case field['type']
when ->(type) { type == 'signature' && with_signature_id }
attachment = submitter.attachments.find { |a| a.uuid == value }
attachments_data_cache[attachment.uuid] ||= attachment.download
image = Vips::Image.new_from_buffer(attachments_data_cache[attachment.uuid], '').autorot
id_string = "ID: #{attachment.uuid}".upcase
while true
text = HexaPDF::Layout::TextFragment.create(id_string,
font:,
font_size: (font_size / 1.8).to_i)
result = layouter.fit([text], area['w'] * width, (font_size / 1.8) / 0.65)
break if result.status == :success
id_string = "#{id_string.delete_suffix('...')[0..-2]}..."
break if id_string.length < 8
end
reason_string =
"#{I18n.t('reason')}: #{I18n.t('digitally_signed_by')} " \
"#{submitter.name}#{submitter.email.present? ? " <#{submitter.email}>" : ''}\n" \
"#{I18n.l(attachment.created_at.in_time_zone(submitter.account.timezone),
format: :long, locale: submitter.account.locale)} " \
"#{TimeUtils.timezone_abbr(submitter.account.timezone, attachment.created_at)}"
reason_text = HexaPDF::Layout::TextFragment.create(reason_string,
font:,
font_size: (font_size / 1.8).to_i)
reason_result = layouter.fit([reason_text], area['w'] * width, height)
text_height = result.lines.sum(&:height) + reason_result.lines.sum(&:height)
image_height = (area['h'] * height) - text_height
image_height = (area['h'] * height) / 2 if image_height < (area['h'] * height) / 2
scale = [(area['w'] * width) / image.width, image_height / image.height].min
io = StringIO.new(image.resize([scale * 4, 1].select(&:positive?).min).write_to_buffer('.png'))
layouter.fit([text], area['w'] * width, (font_size / 1.8) / 0.65)
.draw(canvas, (area['x'] * width) + TEXT_LEFT_MARGIN,
height - (area['y'] * height) - TEXT_TOP_MARGIN - image_height)
layouter.fit([reason_text], area['w'] * width, reason_result.lines.sum(&:height))
.draw(canvas, (area['x'] * width) + TEXT_LEFT_MARGIN,
height - (area['y'] * height) - TEXT_TOP_MARGIN -
result.lines.sum(&:height) - image_height)
canvas.image(
io,
at: [
(area['x'] * width) + (area['w'] * width / 2) - ((image.width * scale) / 2),
height - (area['y'] * height) - (image.height * scale / 2) - (image_height / 2)
],
width: image.width * scale,
height: image.height * scale
)
when 'image', 'signature', 'initials', 'stamp'
attachment = submitter.attachments.find { |a| a.uuid == value }
+8 -1
View File
@@ -6,6 +6,7 @@ module Submitters
AccountConfig::FORM_COMPLETED_MESSAGE_KEY,
AccountConfig::FORM_WITH_CONFETTI_KEY,
AccountConfig::FORM_PREFILL_SIGNATURE_KEY,
AccountConfig::WITH_SIGNATURE_ID,
AccountConfig::ALLOW_TYPED_SIGNATURE].freeze
module_function
@@ -18,8 +19,14 @@ module Submitters
with_typed_signature = find_safe_value(configs, AccountConfig::ALLOW_TYPED_SIGNATURE) != false
with_confetti = find_safe_value(configs, AccountConfig::FORM_WITH_CONFETTI_KEY) != false
prefill_signature = find_safe_value(configs, AccountConfig::FORM_PREFILL_SIGNATURE_KEY) != false
with_signature_id = find_safe_value(configs, AccountConfig::WITH_SIGNATURE_ID) == true
attrs = { completed_button:, with_typed_signature:, with_confetti:, completed_message:, prefill_signature: }
attrs = { completed_button:,
with_typed_signature:,
with_confetti:,
completed_message:,
prefill_signature:,
with_signature_id: }
keys.each do |key|
attrs[key.to_sym] = configs.find { |e| e.key == key.to_s }&.value