module ODFWriter::Images

Images: replace images

Constants

IMAGE_DIR_NAME

constants

Public Instance Methods

avoid_duplicate_image_names(content) click to toggle source

avoid_duplicate_image_names

newer versions of LibreOffice can't open files with duplicates image names

# File lib/odf_writer/images.rb, line 78
def avoid_duplicate_image_names(content)

  nodes = content.xpath("//draw:frame[@draw:name]")
  
  nodes.each_with_index do |node, i|
    node.attribute('name').value = "pic_#{i}"
  end
  
end
find_image_name_matches(content) click to toggle source

find_image_name_matches

# File lib/odf_writer/images.rb, line 43
def find_image_name_matches(content)

  @images.each_pair do |image_name, img_data|
    if node = content.xpath("//draw:frame[@draw:name='#{image_name}']/draw:image").first
      placeholder_path = node.attribute('href').value
      @image_names_replacements[path] = ::File.join(IMAGE_DIR_NAME, ::File.basename(placeholder_path))
    end
  end
  
end
include_image_files(file) click to toggle source

include_image_files

# File lib/odf_writer/images.rb, line 59
def include_image_files(file)

  return if @images.empty?
  
  @image_names_replacements.each_pair do |path, template_image|
  
    file.output_stream.put_next_entry(template_image)
    file.output_stream.write ::File.read(path)
    
  end
  
end