Class: DocxTemplater::DocxCreator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template_path, data, escape_html = true) ⇒ DocxCreator

Returns a new instance of DocxCreator.



7
8
9
10
# File 'lib/docx_templater/docx_creator.rb', line 7

def initialize(template_path, data, escape_html = true)
  @template_path = template_path
  @template_processor = TemplateProcessor.new(data, escape_html)
end

Instance Attribute Details

#template_pathObject (readonly)

Returns the value of attribute template_path.



5
6
7
# File 'lib/docx_templater/docx_creator.rb', line 5

def template_path
  @template_path
end

#template_processorObject (readonly)

Returns the value of attribute template_processor.



5
6
7
# File 'lib/docx_templater/docx_creator.rb', line 5

def template_processor
  @template_processor
end

Instance Method Details

#generate_docx_bytesObject



18
19
20
21
22
23
24
25
26
# File 'lib/docx_templater/docx_creator.rb', line 18

def generate_docx_bytes
  Zip::OutputStream.write_buffer(StringIO.new) do |out|
    Zip::File.open(template_path).each do |entry|
      entry_name = entry.name
      out.put_next_entry(entry_name)
      out.write(copy_or_template(entry_name, entry.get_input_stream.read))
    end
  end
end

#generate_docx_file(file_name = "output_#{Time.now.strftime('%Y-%m-%d_%H%M')}.docx") ⇒ Object



12
13
14
15
16
# File 'lib/docx_templater/docx_creator.rb', line 12

def generate_docx_file(file_name = "output_#{Time.now.strftime('%Y-%m-%d_%H%M')}.docx")
  File.open(file_name, 'w') do |f|
    f.write(generate_docx_bytes.string)
  end
end