Class: CentralMail::DatestampPdf

Inherits:
Object
  • Object
show all
Defined in:
lib/central_mail/datestamp_pdf.rb

Instance Method Summary collapse

Constructor Details

#initialize(file_path, append_to_stamp: nil) ⇒ DatestampPdf

Returns a new instance of DatestampPdf.



8
9
10
11
# File 'lib/central_mail/datestamp_pdf.rb', line 8

def initialize(file_path, append_to_stamp: nil)
  @file_path = file_path
  @append_to_stamp = append_to_stamp
end

Instance Method Details

#generate_stamp(stamp_path, text, x, y, text_only, size = 10, timestamp = nil) ⇒ Object

rubocop:disable Metrics/ParameterLists



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/central_mail/datestamp_pdf.rb', line 23

def generate_stamp(stamp_path, text, x, y, text_only, size = 10, timestamp = nil)
  timestamp ||= Time.zone.now
  unless text_only
    text += " #{I18n.l(timestamp, format: :pdf_stamp)}"
    text += ". #{@append_to_stamp}" if @append_to_stamp
  end

  Prawn::Document.generate(stamp_path, margin: [0, 0]) do |pdf|
    pdf.draw_text text, at: [x, y], size:
  end
rescue => e
  Rails.logger.error "Failed to generate datestamp file: #{e.message}"
  raise
end

#run(settings) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/central_mail/datestamp_pdf.rb', line 13

def run(settings)
  stamp_path = Common::FileHelpers.random_file_path
  generate_stamp(stamp_path, settings[:text], settings[:x], settings[:y], settings[:text_only], settings[:size],
                 settings[:timestamp])
  stamp(@file_path, stamp_path)
ensure
  Common::FileHelpers.delete_file_if_exists(stamp_path) if defined?(stamp_path)
end

#stamp(file_path, stamp_path) ⇒ Object

rubocop:enable Metrics/ParameterLists



39
40
41
42
43
44
45
46
47
# File 'lib/central_mail/datestamp_pdf.rb', line 39

def stamp(file_path, stamp_path)
  out_path = "#{Common::FileHelpers.random_file_path}.pdf"
  PdfFill::Filler::PDF_FORMS.stamp(file_path, stamp_path, out_path)
  File.delete(file_path)
  out_path
rescue
  Common::FileHelpers.delete_file_if_exists(out_path)
  raise
end