Class: Acrofill::Template
- Inherits:
-
Object
- Object
- Acrofill::Template
- Defined in:
- lib/acrofill/template.rb
Overview
A pre-parsed, reusable template. Parsing is the dominant cost of a fill (tokenizing and inflating the whole file); Template pays it once and restores a pristine object graph from a Marshal snapshot for each subsequent fill — measured at 7-8x faster per fill on real forms.
template = Acrofill::Template.new('claim_form.pdf')
template.fill_form('a.pdf', { 'Name' => 'Jane' }, flatten: true)
template.fill_form('b.pdf', { 'Name' => 'John' }, flatten: true)
Instances are cheap to keep around (the snapshot is a single string, comparable to the file size) and safe to share across threads: every fill works on its own restored copy.
Instance Method Summary collapse
- #field_names ⇒ Object
-
#fields ⇒ Object
Field metadata without touching disk again.
- #fill_form(destination, data = {}, options = {}) ⇒ Object
-
#initialize(path) ⇒ Template
constructor
A new instance of Template.
Constructor Details
Instance Method Details
#field_names ⇒ Object
40 41 42 |
# File 'lib/acrofill/template.rb', line 40 def field_names fields.map(&:name) end |
#fields ⇒ Object
Field metadata without touching disk again.
36 37 38 |
# File 'lib/acrofill/template.rb', line 36 def fields Form.new(Document.restore(@snapshot)).fields end |
#fill_form(destination, data = {}, options = {}) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/acrofill/template.rb', line 22 def fill_form(destination, data = {}, = {}) # Template#fill_form takes the destination first; Filler#fill_form # takes the template first. Passing the template here is the natural # slip, and since Template never re-reads the file the overwrite would # go unnoticed until another consumer opened the corrupted template. if @source && absolute(destination) == @source raise Error, 'destination is the template itself; ' \ 'Template#fill_form takes (destination, data, options)' end Filler.new.apply(Document.restore(@snapshot), destination, data, ) end |