Class: Acrofill::Template

Inherits:
Object
  • Object
show all
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 — roughly 50x faster than re-parsing per fill.

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

Constructor Details

#initialize(path) ⇒ Template

Returns a new instance of Template.



17
18
19
20
# File 'lib/acrofill/template.rb', line 17

def initialize(path)
  doc = Document.new(path)
  @snapshot = doc.snapshot
end

Instance Method Details

#field_namesObject



31
32
33
# File 'lib/acrofill/template.rb', line 31

def field_names
  fields.map(&:name)
end

#fieldsObject

Field metadata without touching disk again.



27
28
29
# File 'lib/acrofill/template.rb', line 27

def fields
  Form.new(Document.restore(@snapshot)).fields
end

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



22
23
24
# File 'lib/acrofill/template.rb', line 22

def fill_form(destination, data = {}, options = {})
  Filler.new.apply(Document.restore(@snapshot), destination, data, options)
end