Class: DocxTemplater

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

Overview

Use .docx as reusable templates

Example: buffer = DocxTemplater.replace_file_with_content(‘path/to/mydocument.docx’,

{
  :client_email1 => '[email protected]',
  :client_phone1 => '555-555-5555',
})

# In Rails you can send a word document via send_data send_data buffer.string, :filename => ‘REPC.docx’ # Or save the output to a word file File.open(“path/to/mydocument.docx”, “wb”) {|f| f.write(buffer.string) }

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ DocxTemplater

Returns a new instance of DocxTemplater.



21
22
23
# File 'lib/docx_templater.rb', line 21

def initialize(opts = {})
  @options = opts
end

Instance Method Details

#entry_requires_replacement?(entry) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/docx_templater.rb', line 42

def entry_requires_replacement?(entry)
  entry.ftype != :directory && entry.name =~ /document|header|footer/
end

#generate_tags_for(*args) ⇒ Object



38
39
40
# File 'lib/docx_templater.rb', line 38

def generate_tags_for(*args)
  Docx::ArgumentCombiner.new(*args).attributes
end

#replace_file_with_content(file_path, data_provider) ⇒ Object



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

def replace_file_with_content(file_path, data_provider)
  # Rubyzip doesn't save it right unless saved like this: https://gist.github.com/e7d2855435654e1ebc52
  zf = Zip::File.new(file_path) # Put original file name here

  buffer = Zip::OutputStream.write_buffer do |out|
    zf.entries.each do |e|
      process_entry(e, out, data_provider)
    end
  end
  # You can save this buffer or send it with rails via send_data
  return buffer
end