Class: PDFUtilities::DatestampPdf
- Inherits:
-
Object
- Object
- PDFUtilities::DatestampPdf
- Defined in:
- lib/pdf_utilities/datestamp_pdf.rb
Overview
add a watermark datestamp to an existing pdf
Instance Attribute Summary collapse
-
#append_to_stamp ⇒ Object
readonly
private
Returns the value of attribute append_to_stamp.
-
#file_path ⇒ Object
readonly
private
Returns the value of attribute file_path.
-
#multistamp ⇒ Object
readonly
private
Returns the value of attribute multistamp.
-
#page_number ⇒ Object
readonly
private
Returns the value of attribute page_number.
-
#size ⇒ Object
readonly
private
Returns the value of attribute size.
-
#stamp_path ⇒ Object
readonly
private
Returns the value of attribute stamp_path.
-
#stamped_pdf ⇒ Object
readonly
private
Returns the value of attribute stamped_pdf.
-
#template ⇒ Object
readonly
private
Returns the value of attribute template.
-
#text ⇒ Object
readonly
private
Returns the value of attribute text.
-
#text_only ⇒ Object
readonly
private
Returns the value of attribute text_only.
-
#x ⇒ Object
readonly
private
Returns the value of attribute x.
-
#y ⇒ Object
readonly
private
Returns the value of attribute y.
Instance Method Summary collapse
- #default_settings ⇒ Object private
-
#generate_stamp ⇒ Object
private
generate the stamp/background pdf.
-
#initialize(file_path, append_to_stamp: nil) ⇒ DatestampPdf
constructor
prepare to datestamp an existing pdf document.
-
#run(settings) ⇒ String
create a datestamped pdf copy of ‘file_path`.
-
#stamp_pdf ⇒ Object
private
combine the input and background pdfs into the stamped_pdf.
-
#stamp_text ⇒ Object
private
create the stamp text to be used.
-
#timestamp ⇒ Object
private
reader for timestamp, ensure there is always a value.
-
#timestamp4010007 ⇒ Object
private
format timestamp as :pdf_stamp4010007.
Constructor Details
#initialize(file_path, append_to_stamp: nil) ⇒ DatestampPdf
prepare to datestamp an existing pdf document
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_stamp ⇒ Object (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_path ⇒ Object (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 |
#multistamp ⇒ Object (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_number ⇒ Object (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 |
#size ⇒ Object (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_path ⇒ Object (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_pdf ⇒ Object (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 |
#template ⇒ Object (readonly, private)
Returns the value of attribute template.
55 56 57 |
# File 'lib/pdf_utilities/datestamp_pdf.rb', line 55 def template @template end |
#text ⇒ Object (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_only ⇒ Object (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 |
#x ⇒ Object (readonly, private)
Returns the value of attribute x.
55 56 57 |
# File 'lib/pdf_utilities/datestamp_pdf.rb', line 55 def x @x end |
#y ⇒ Object (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_settings ⇒ Object (private)
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_stamp ⇒ Object (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 .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`
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.}" Common::FileHelpers.delete_file_if_exists(stamped_pdf) raise ensure Common::FileHelpers.delete_file_if_exists(stamp_path) end |
#stamp_pdf ⇒ Object (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_text ⇒ Object (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(, format: :pdf_stamp4010007)}" else " #{I18n.l(, format: :pdf_stamp_utc)}" end stamp += ". #{append_to_stamp}" if append_to_stamp end stamp end |
#timestamp ⇒ Object (private)
reader for timestamp, ensure there is always a value
74 75 76 |
# File 'lib/pdf_utilities/datestamp_pdf.rb', line 74 def @timestamp ||= Time.zone.now end |
#timestamp4010007 ⇒ Object (private)
format timestamp as :pdf_stamp4010007
79 80 81 |
# File 'lib/pdf_utilities/datestamp_pdf.rb', line 79 def Date.strptime(.strftime('%m/%d/%Y'), '%m/%d/%Y') end |