lib/fillable-pdf.rb in fillable-pdf-0.9.4 vs lib/fillable-pdf.rb in fillable-pdf-0.9.5

- old
+ new

@@ -8,21 +8,20 @@ ## # Opens a given fillable-pdf PDF file and prepares it for modification. # # @param [String|Symbol] file_path the name of the PDF file or file path # - def initialize(file_path) # rubocop:disable Metrics/MethodLength + def initialize(file_path) raise IOError, "File <#{file_path}> is not found" unless File.exist?(file_path) @file_path = file_path begin @byte_stream = ITEXT::ByteArrayOutputStream.new @pdf_reader = ITEXT::PdfReader.new @file_path.to_s @pdf_writer = ITEXT::PdfWriter.new @byte_stream @pdf_doc = ITEXT::PdfDocument.new @pdf_reader, @pdf_writer @pdf_form = ITEXT::PdfAcroForm.getAcroForm(@pdf_doc, true) @form_fields = @pdf_form.getFormFields - @pdf_form.setGenerateAppearance false rescue StandardError => e raise "#{e.message} (Input file may be corrupt, incompatible, read-only, write-protected, encrypted, or may not have any form fields)" # rubocop:disable Layout/LineLength end end @@ -56,11 +55,11 @@ rescue NoMethodError raise "unknown key name `#{key}'" end ## - # Retrieves the numeric type of a field given its unique field name. + # Retrieves the string type of a field given its unique field name. # # @param [String|Symbol] key the field name # # @return the type of the field # @@ -88,10 +87,13 @@ # # @param [String|Symbol] key the field name # @param [String|Symbol] value the field value # def set_field(key, value) - pdf_field(key).setValue(value.to_s) + # we set generate_appearance to false for buttons to ensure that the chosen + # appearance for checkboxes (i.e. check, circle, diamond) is not changed + generate_appearance = field_type(key) != Field::BUTTON + pdf_field(key).setValue(value.to_s, generate_appearance) end ## # Sets an image within the bounds of the given form field. It doesn't matter # what type of form field it is (signature, image, etc). The image will be scaled