Class: Acrofill::Filler

Inherits:
Object
  • Object
show all
Defined in:
lib/acrofill/filler.rb

Overview

Public entry point, signature-compatible with PdfForms#fill_form:

Acrofill.new.fill_form(template, output, { 'Field' => 'value' }, flatten: true)

Note: PdfForms-specific options (data_format:, utf8_fields:, ...) are accepted and ignored — acrofill needs no FDF and is always UTF-aware.

Instance Method Summary collapse

Constructor Details

#initialize(_pdftk_path = nil, options = {}, **kwargs) ⇒ Filler

Accepts and ignores a pdftk path argument for drop-in compatibility. Options given here are defaults for every #fill_form call, the way PdfForms.new(path, flatten: true) behaves — accepting them and then ignoring them would silently ship unflattened documents.



15
16
17
# File 'lib/acrofill/filler.rb', line 15

def initialize(_pdftk_path = nil, options = {}, **kwargs)
  @options = (options.is_a?(Hash) ? options : {}).merge(kwargs)
end

Instance Method Details

#apply(doc, destination, data, options) ⇒ Object

Shared fill pipeline, also driven by Template with a restored document. Values are normalized to valid UTF-8 up front so that a mis-encoded input degrades (replacement character) instead of leaking a raw Encoding error from deep inside the appearance code.



38
39
40
41
42
43
44
45
46
# File 'lib/acrofill/filler.rb', line 38

def apply(doc, destination, data, options)
  form = Form.new(doc)
  (data || {}).each do |name, value|
    form.fill(normalize(name), normalize(value))
  end
  form.flatten! if flatten?(options)
  write(destination, Writer.new(doc).render)
  destination
end

#field_names(template) ⇒ Object

Fully-qualified names of all fillable fields.



30
31
32
# File 'lib/acrofill/filler.rb', line 30

def field_names(template)
  fields(template).map(&:name)
end

#fields(template) ⇒ Object

Field metadata (name, type, current value, checkbox/radio states) without modifying the document.



25
26
27
# File 'lib/acrofill/filler.rb', line 25

def fields(template)
  Form.new(Document.new(template)).fields
end

#fill_form(template, destination, data = {}, options = {}) ⇒ Object



19
20
21
# File 'lib/acrofill/filler.rb', line 19

def fill_form(template, destination, data = {}, options = {})
  apply(Document.new(template), destination, data, options)
end