Class: PdfForm

Inherits:
Object
  • Object
show all
Defined in:
app/pdf_forms/pdf_form.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ PdfForm

Returns a new instance of PdfForm.



4
5
6
7
8
9
10
11
12
13
14
15
# File 'app/pdf_forms/pdf_form.rb', line 4

def initialize(data = {})
  @data = data

  @pdf = FillablePDF.new(self.class.template_path)
  @pdf.set_fields(
    self.class.flattened_fields(
      data,
      self.class.boxes_by_field_name,
      self.class.field_name_prefixes
    )
  )
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



2
3
4
# File 'app/pdf_forms/pdf_form.rb', line 2

def data
  @data
end

#pathObject (readonly)

Returns the value of attribute path.



2
3
4
# File 'app/pdf_forms/pdf_form.rb', line 2

def path
  @path
end

#pdfObject (readonly)

Returns the value of attribute pdf.



2
3
4
# File 'app/pdf_forms/pdf_form.rb', line 2

def pdf
  @pdf
end

Instance Method Details

#save_as(path:, read_only: true, signed_by: '') ⇒ Object



17
18
19
20
21
22
23
24
# File 'app/pdf_forms/pdf_form.rb', line 17

def save_as(path:, read_only: true, signed_by: '')
  flatten = signed_by.present? || read_only
  @pdf.save_as(path, flatten: flatten)
  @path = path

  sign(content: signed_by) if signed_by.present?
  @path
end

#to_file(read_only: true, signed_by: '') ⇒ Object



26
27
28
29
30
31
32
# File 'app/pdf_forms/pdf_form.rb', line 26

def to_file(read_only: true, signed_by: '')
  tempfile = Tempfile.new(["#{SecureRandom.uuid}_#{Time.now.to_i}", '.pdf'])
  save_as(path: tempfile.path, read_only: read_only, signed_by: signed_by)
  @path = tempfile.path
  tempfile.rewind
  tempfile
end