From 0b0f0b497e8b6eec1ac4d2b5bd4f99973e4ca713 Mon Sep 17 00:00:00 2001 From: Alex Turchyn Date: Wed, 6 Nov 2024 19:27:37 +0200 Subject: [PATCH] fix acroform select field --- lib/templates/find_acro_fields.rb | 23 ++++++++++++++++++++--- lib/templates/process_document.rb | 9 +++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/lib/templates/find_acro_fields.rb b/lib/templates/find_acro_fields.rb index b6ae50ce..ac2d5c45 100644 --- a/lib/templates/find_acro_fields.rb +++ b/lib/templates/find_acro_fields.rb @@ -6,6 +6,20 @@ module Templates FIELD_NAME_REGEXP = /\A(?=.*\p{L})[\p{L}\d\s-]+\z/ SKIP_FIELD_DESCRIPTION = %w[undefined].freeze + SELECT_PLACEHOLDER_REGEXP = /\b( + Select | + Choose | + Wählen | + Auswählen | + Sélectionner| + Choisir | + Seleccionar | + Elegir | + Seleziona | + Scegliere | + Selecionar | + Escolher + )\b/ix module_function @@ -143,8 +157,8 @@ module Templates { **attrs, type: 'select', - options: build_options(field[:Opt]), - default_value: field.field_value + options: build_options(field[:Opt], 'select'), + default_value: field.field_value.to_s.match?(SELECT_PLACEHOLDER_REGEXP) ? nil : field.field_value } elsif field.field_type == :Ch && field.concrete_field_type == :multi_select && field[:Opt].present? { @@ -178,11 +192,14 @@ module Templates def build_options(values, type = nil) is_skip_single_value = type.in?(%w[radio multiple]) && values.uniq.size == 1 - values.map do |option| + values.filter_map do |option| is_option_number = option.is_a?(Symbol) && option.to_s.match?(/\A\d+\z/) + option = option[1] if option.is_a?(Array) && option.size == 2 option = option.encode('utf-8', invalid: :replace, undef: :replace, replace: '') if option.is_a?(String) + next if type == 'select' && option.to_s.match?(SELECT_PLACEHOLDER_REGEXP) + { uuid: SecureRandom.uuid, value: is_option_number || is_skip_single_value ? '' : option diff --git a/lib/templates/process_document.rb b/lib/templates/process_document.rb index b840bd9a..d7f3d36f 100644 --- a/lib/templates/process_document.rb +++ b/lib/templates/process_document.rb @@ -122,6 +122,15 @@ module Templates io = StringIO.new + pdf.acro_form.each_field do |field| + next if field.field_type != :Ch || + field[:Opt].blank? || + %i[combo_box editable_combo_box].exclude?(field.concrete_field_type) || + !field.field_value.to_s.match?(FindAcroFields::SELECT_PLACEHOLDER_REGEXP) + + field[:V] = '' + end + pdf.acro_form.create_appearances(force: true) if pdf.acro_form[:NeedAppearances] pdf.acro_form.flatten