class FillPdf::Fill

Attributes

attributes[RW]
dictionary[RW]
dirname[RW]
pdftk[RW]
template[RW]

Public Class Methods

new(template, dictionary={}) click to toggle source

Template is path of a pdf file

# 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

Public Instance Methods

export() click to toggle source

This method populate attributes with data based on template fields.

Generate document path.

Create new document and return path of this document.

# 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
populate() click to toggle source

Populate all pdf fields with values.

# 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_names() click to toggle source

Return list of template fields in array

# File lib/fill_pdf/methods.rb, line 17
def template_field_names
  pdftk.get_field_names template
end

Protected Instance Methods

logger(exception) click to toggle source

Logger is a method used for log exceptions

# File lib/fill_pdf/methods.rb, line 65
def logger(exception)
  Rails.logger.warn "------------An error occurred: -------------"
  Rails.logger.warn exception
  Rails.logger.warn "--------------------------------------------"
  false
end
set(attribute, value=nil) click to toggle source

This methods is setter for used to set field value

# File lib/fill_pdf/methods.rb, line 74
def set(attribute, value=nil)
  attributes[attribute.to_s] = value
end
value(field) click to toggle source

Based on dictionary this methods return value of fields

# File lib/fill_pdf/methods.rb, line 59
def value(field)
  dictionary[field.to_sym] || dictionary[field.to_s] rescue nil
end