Class: FillPdf::Fill

Inherits:
Object
  • Object
show all
Defined in:
lib/fill_pdf/methods.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template, dictionary = {}) ⇒ Fill

Template is path of a pdf file



7
8
9
10
11
12
13
# File 'lib/fill_pdf/methods.rb', line 7

def initialize(template, dictionary={})
  @attributes = {}
  @template = template
  @dictionary = dictionary
  @dirname = Rails.application.config.fill_pdf.output_path
  @pdftk = PdfForms.new(Rails.application.config.fill_pdf.pdftk_path)
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



3
4
5
# File 'lib/fill_pdf/methods.rb', line 3

def attributes
  @attributes
end

#dictionaryObject

Returns the value of attribute dictionary.



3
4
5
# File 'lib/fill_pdf/methods.rb', line 3

def dictionary
  @dictionary
end

#dirnameObject

Returns the value of attribute dirname.



3
4
5
# File 'lib/fill_pdf/methods.rb', line 3

def dirname
  @dirname
end

#pdftkObject

Returns the value of attribute pdftk.



3
4
5
# File 'lib/fill_pdf/methods.rb', line 3

def pdftk
  @pdftk
end

#templateObject

Returns the value of attribute template.



3
4
5
# File 'lib/fill_pdf/methods.rb', line 3

def template
  @template
end

Instance Method Details

#exportObject

This method populate attributes with data based on template fields.

Generate document path.

Create new document and return path of this document.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/fill_pdf/methods.rb', line 37

def export
  # Create directory used for store documents
  Dir.mkdir(@dirname) unless File.directory?(@dirname)

  #call method populate to set field with value.
  populate

  # Generate Path of document
  document = Rails.root.join(dirname, "#{SecureRandom.uuid}.pdf")

  # Generate document
  pdftk.fill_form template, document, attributes, :flatten => true

  # Return path of document
  document
rescue Exception => exception
  logger exception
end

#populateObject

Populate all pdf fields with values.



23
24
25
26
27
28
29
# File 'lib/fill_pdf/methods.rb', line 23

def populate
  @attributes = {}
  template_field_names.each do |field|
    set(field, value(field).to_s)
  end
  @attributes
end

#template_field_namesObject

Return list of template fields in array



17
18
19
# File 'lib/fill_pdf/methods.rb', line 17

def template_field_names
  pdftk.get_field_names template
end