Class: PDFUtilities::DatestampPdf

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

Overview

add a watermark datestamp to an existing pdf

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path, append_to_stamp: nil) ⇒ DatestampPdf

prepare to datestamp an existing pdf document

Parameters:

  • file_path (String)
  • append_to_stamp (String) (defaults to: nil)

    text to append to the stamp



17
18
19
20
# File 'lib/pdf_utilities/datestamp_pdf.rb', line 17

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

Instance Attribute Details

#append_to_stampObject (readonly, private)

Returns the value of attribute append_to_stamp.



55
56
57
# File 'lib/pdf_utilities/datestamp_pdf.rb', line 55

def append_to_stamp
  @append_to_stamp
end

#file_pathObject (readonly, private)

Returns the value of attribute file_path.



55
56
57
# File 'lib/pdf_utilities/datestamp_pdf.rb', line 55

def file_path
  @file_path
end

#multistampObject (readonly, private)

Returns the value of attribute multistamp.



55
56
57
# File 'lib/pdf_utilities/datestamp_pdf.rb', line 55

def multistamp
  @multistamp
end

#page_numberObject (readonly, private)

Returns the value of attribute page_number.



55
56
57
# File 'lib/pdf_utilities/datestamp_pdf.rb', line 55

def page_number
  @page_number
end

#sizeObject (readonly, private)

Returns the value of attribute size.



55
56
57
# File 'lib/pdf_utilities/datestamp_pdf.rb', line 55

def size
  @size
end

#stamp_pathObject (readonly, private)

Returns the value of attribute stamp_path.



55
56
57
# File 'lib/pdf_utilities/datestamp_pdf.rb', line 55

def stamp_path
  @stamp_path
end

#stamped_pdfObject (readonly, private)

Returns the value of attribute stamped_pdf.



55
56
57
# File 'lib/pdf_utilities/datestamp_pdf.rb', line 55

def stamped_pdf
  @stamped_pdf
end

#templateObject (readonly, private)

Returns the value of attribute template.



55
56
57
# File 'lib/pdf_utilities/datestamp_pdf.rb', line 55

def template
  @template
end

#textObject (readonly, private)

Returns the value of attribute text.



55
56
57
# File 'lib/pdf_utilities/datestamp_pdf.rb', line 55

def text
  @text
end

#text_onlyObject (readonly, private)

Returns the value of attribute text_only.



55
56
57
# File 'lib/pdf_utilities/datestamp_pdf.rb', line 55

def text_only
  @text_only
end

#xObject (readonly, private)

Returns the value of attribute x.



55
56
57
# File 'lib/pdf_utilities/datestamp_pdf.rb', line 55

def x
  @x
end

#yObject (readonly, private)

Returns the value of attribute y.



55
56
57
# File 'lib/pdf_utilities/datestamp_pdf.rb', line 55

def y
  @y
end

Instance Method Details

#default_settingsObject (private)

See Also:



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/pdf_utilities/datestamp_pdf.rb', line 59

def default_settings
  {
    text: 'VA.gov',
    x: 5,
    y: 5,
    text_only: false,
    size: 10,
    timestamp: Time.zone.now,
    page_number: nil,
    template: nil,
    multistamp: false
  }.freeze
end

#generate_stampObject (private)

generate the stamp/background pdf



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/pdf_utilities/datestamp_pdf.rb', line 85

def generate_stamp
  @stamp_path = Common::FileHelpers.random_file_path
  Prawn::Document.generate(stamp_path, margin: [0, 0]) do |pdf|
    if page_number.present? && template.present?
      reader = PDF::Reader.new(template)
      page_number.times do
        pdf.start_new_page
      end
      (pdf.draw_text stamp_text, at: [x, y], size:)
      (pdf.draw_text timestamp.strftime('%Y-%m-%d %I:%M %p %Z'), at: [x, y - 12], size:)
      (reader.page_count - page_number).times do
        pdf.start_new_page
      end
    else
      pdf.draw_text stamp_text, at: [x, y], size:
    end
  end

  stamp_path
end

#run(settings) ⇒ String

create a datestamped pdf copy of ‘file_path`

Parameters:

  • settings (Hash)

    options for generating the datestamp

Options Hash (settings):

  • :text (String)

    the stamp text

  • :x (String)

    stamp x coordinate; default 5

  • :y (String)

    stamp y coordinate; default 5

  • :text_only (String)

    only stamp the provided text, no timestamp; default false

  • :size (String)

    font size; default 10

  • :timestamp (String)

    the timestamp to include; default Time.zone.now

  • :page_number (String)

    on which page to place the stamp; default nil

  • :template (String)

    another pdf on which to base the stamped pdf; default nil

  • :multistamp (String)

    apply stamped pdf page to corresponding input pdf; default false

Returns:

  • (String)

    path to generated stamped pdf



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/pdf_utilities/datestamp_pdf.rb', line 37

def run(settings)
  settings = default_settings.merge(settings)
  settings.each do |key, value|
    instance_variable_set("@#{key}", value)
  end

  generate_stamp
  stamp_pdf
rescue => e
  Rails.logger.error "Failed to generate datestamp file: #{e.message}"
  Common::FileHelpers.delete_file_if_exists(stamped_pdf)
  raise
ensure
  Common::FileHelpers.delete_file_if_exists(stamp_path)
end

#stamp_pdfObject (private)

combine the input and background pdfs into the stamped_pdf



123
124
125
126
127
128
129
130
131
132
# File 'lib/pdf_utilities/datestamp_pdf.rb', line 123

def stamp_pdf
  @stamped_pdf = "#{Common::FileHelpers.random_file_path}.pdf"
  if multistamp
    PDFUtilities::PDFTK.multistamp(file_path, stamp_path, stamped_pdf)
  else
    PDFUtilities::PDFTK.stamp(file_path, stamp_path, stamped_pdf)
  end

  stamped_pdf
end

#stamp_textObject (private)

create the stamp text to be used



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/pdf_utilities/datestamp_pdf.rb', line 107

def stamp_text
  stamp = text
  unless text_only
    stamp += if File.basename(file_path) == 'vba_40_10007-stamped.pdf'
               " #{I18n.l(timestamp4010007, format: :pdf_stamp4010007)}"
             else
               " #{I18n.l(timestamp, format: :pdf_stamp_utc)}"
             end
    stamp += ". #{append_to_stamp}" if append_to_stamp
  end

  stamp
end

#timestampObject (private)

reader for timestamp, ensure there is always a value



74
75
76
# File 'lib/pdf_utilities/datestamp_pdf.rb', line 74

def timestamp
  @timestamp ||= Time.zone.now
end

#timestamp4010007Object (private)

format timestamp as :pdf_stamp4010007



79
80
81
# File 'lib/pdf_utilities/datestamp_pdf.rb', line 79

def timestamp4010007
  Date.strptime(timestamp.strftime('%m/%d/%Y'), '%m/%d/%Y')
end