Module: PdfFill::Filler
- Defined in:
- lib/pdf_fill/filler.rb
Overview
Provides functionality to fill and process PDF forms.
This module includes methods to register form classes, fill out PDF forms, and handle extra PDF generation.
Defined Under Namespace
Classes: PdfFillerException
Constant Summary collapse
- PDF_FORMS =
A PdfForms instance for handling standard PDF forms.
PdfForms.new(Settings.binaries.pdftk)
- UNICODE_PDF_FORMS =
A PdfForms instance for handling Unicode PDF forms with XFdf data format.
PdfForms.new(Settings.binaries.pdftk, data_format: 'XFdf', utf8_fields: true)
- FORM_CLASSES =
A hash mapping form IDs to their corresponding form classes. This constant is intentionally mutable.
{}
Class Method Summary collapse
-
.combine_extras(old_file_path, extras_generator) ⇒ String
Combines extra pages into the main PDF if necessary.
-
.fill_ancillary_form(form_data, claim_id, form_id) ⇒ String
Fills an ancillary form based on the provided data and form ID.
-
.fill_form(saved_claim, file_name_extension = nil, fill_options = {}) ⇒ String
Fills a form based on the provided saved claim and options.
-
.process_form(form_id, form_data, form_class, file_name_extension, fill_options = {}) ⇒ String
Processes a form by filling it with data and saving it to a file.
-
.register_form(form_id, form_class) ⇒ Object
Registers a form class with a specific form ID.
Instance Method Summary collapse
-
#combine_extras(old_file_path, extras_generator) ⇒ String
private
Combines extra pages into the main PDF if necessary.
-
#fill_ancillary_form(form_data, claim_id, form_id) ⇒ String
private
Fills an ancillary form based on the provided data and form ID.
-
#fill_form(saved_claim, file_name_extension = nil, fill_options = {}) ⇒ String
private
Fills a form based on the provided saved claim and options.
-
#process_form(form_id, form_data, form_class, file_name_extension, fill_options = {}) ⇒ String
private
Processes a form by filling it with data and saving it to a file.
-
#register_form(form_id, form_class) ⇒ Object
private
Registers a form class with a specific form ID.
Class Method Details
.combine_extras(old_file_path, extras_generator) ⇒ String
Combines extra pages into the main PDF if necessary.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/pdf_fill/filler.rb', line 76 def combine_extras(old_file_path, extras_generator) if extras_generator.text? file_path = "#{old_file_path.gsub('.pdf', '')}_final.pdf" extras_path = extras_generator.generate PDF_FORMS.cat(old_file_path, extras_path, file_path) File.delete(extras_path) File.delete(old_file_path) file_path else old_file_path end end |
.fill_ancillary_form(form_data, claim_id, form_id) ⇒ String
Fills an ancillary form based on the provided data and form ID.
120 121 122 |
# File 'lib/pdf_fill/filler.rb', line 120 def fill_ancillary_form(form_data, claim_id, form_id) process_form(form_id, form_data, FORM_CLASSES[form_id], claim_id) end |
.fill_form(saved_claim, file_name_extension = nil, fill_options = {}) ⇒ String
Fills a form based on the provided saved claim and options.
102 103 104 105 106 107 108 109 |
# File 'lib/pdf_fill/filler.rb', line 102 def fill_form(saved_claim, file_name_extension = nil, = {}) form_id = saved_claim.form_id form_class = FORM_CLASSES[form_id] raise PdfFillerException, "Form #{form_id} was not found." unless form_class process_form(form_id, saved_claim.parsed_form, form_class, file_name_extension || saved_claim.id, ) end |
.process_form(form_id, form_data, form_class, file_name_extension, fill_options = {}) ⇒ String
Processes a form by filling it with data and saving it to a file.
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/pdf_fill/filler.rb', line 135 def process_form(form_id, form_data, form_class, file_name_extension, = {}) folder = 'tmp/pdfs' FileUtils.mkdir_p(folder) file_path = "#{folder}/#{form_id}_#{file_name_extension}.pdf" hash_converter = HashConverter.new(form_class.date_strftime) new_hash = hash_converter.transform_data( form_data: form_class.new(form_data).merge_fields(), pdftk_keys: form_class::KEY ) has_template = form_class.const_defined?(:TEMPLATE) template_path = has_template ? form_class::TEMPLATE : "lib/pdf_fill/forms/pdfs/#{form_id}.pdf" (form_id == SavedClaim::CaregiversAssistanceClaim::FORM ? UNICODE_PDF_FORMS : PDF_FORMS).fill_form( template_path, file_path, new_hash, flatten: Rails.env.production? ) combine_extras(file_path, hash_converter.extras_generator) end |
.register_form(form_id, form_class) ⇒ Object
Registers a form class with a specific form ID.
43 44 45 |
# File 'lib/pdf_fill/filler.rb', line 43 def register_form(form_id, form_class) FORM_CLASSES[form_id] = form_class end |
Instance Method Details
#combine_extras(old_file_path, extras_generator) ⇒ String (private)
Combines extra pages into the main PDF if necessary.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/pdf_fill/filler.rb', line 76 def combine_extras(old_file_path, extras_generator) if extras_generator.text? file_path = "#{old_file_path.gsub('.pdf', '')}_final.pdf" extras_path = extras_generator.generate PDF_FORMS.cat(old_file_path, extras_path, file_path) File.delete(extras_path) File.delete(old_file_path) file_path else old_file_path end end |
#fill_ancillary_form(form_data, claim_id, form_id) ⇒ String (private)
Fills an ancillary form based on the provided data and form ID.
120 121 122 |
# File 'lib/pdf_fill/filler.rb', line 120 def fill_ancillary_form(form_data, claim_id, form_id) process_form(form_id, form_data, FORM_CLASSES[form_id], claim_id) end |
#fill_form(saved_claim, file_name_extension = nil, fill_options = {}) ⇒ String (private)
Fills a form based on the provided saved claim and options.
102 103 104 105 106 107 108 109 |
# File 'lib/pdf_fill/filler.rb', line 102 def fill_form(saved_claim, file_name_extension = nil, = {}) form_id = saved_claim.form_id form_class = FORM_CLASSES[form_id] raise PdfFillerException, "Form #{form_id} was not found." unless form_class process_form(form_id, saved_claim.parsed_form, form_class, file_name_extension || saved_claim.id, ) end |
#process_form(form_id, form_data, form_class, file_name_extension, fill_options = {}) ⇒ String (private)
Processes a form by filling it with data and saving it to a file.
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/pdf_fill/filler.rb', line 135 def process_form(form_id, form_data, form_class, file_name_extension, = {}) folder = 'tmp/pdfs' FileUtils.mkdir_p(folder) file_path = "#{folder}/#{form_id}_#{file_name_extension}.pdf" hash_converter = HashConverter.new(form_class.date_strftime) new_hash = hash_converter.transform_data( form_data: form_class.new(form_data).merge_fields(), pdftk_keys: form_class::KEY ) has_template = form_class.const_defined?(:TEMPLATE) template_path = has_template ? form_class::TEMPLATE : "lib/pdf_fill/forms/pdfs/#{form_id}.pdf" (form_id == SavedClaim::CaregiversAssistanceClaim::FORM ? UNICODE_PDF_FORMS : PDF_FORMS).fill_form( template_path, file_path, new_hash, flatten: Rails.env.production? ) combine_extras(file_path, hash_converter.extras_generator) end |
#register_form(form_id, form_class) ⇒ Object (private)
Registers a form class with a specific form ID.
43 44 45 |
# File 'lib/pdf_fill/filler.rb', line 43 def register_form(form_id, form_class) FORM_CLASSES[form_id] = form_class end |