Class: OpenXml::Docx::Package

Inherits:
Package
  • Object
show all
Defined in:
lib/openxml/docx/package.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePackage


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/openxml/docx/package.rb', line 27

def initialize
  super

  rels.add_relationship REL_DOCUMENT, "/word/document.xml"
  @settings = OpenXml::Docx::Parts::Settings.new
  @styles = OpenXml::Docx::Parts::Styles.new
  @fonts = OpenXml::Docx::Parts::Fonts.new
  @document = OpenXml::Docx::Parts::Document.new
  @headers = []
  @footers = []
  @image_names = []

  document.relationships.add_relationship REL_STYLES, "styles.xml"
  document.relationships.add_relationship REL_SETTINGS, "settings.xml"
  document.relationships.add_relationship REL_FONT_TABLE, "fontTable.xml"

  add_part "word/_rels/document.xml.rels", document.relationships
  add_part "word/_rels/fontTable.xml.rels", fonts.relationships
  add_part "word/document.xml", document
  add_part "word/settings.xml", settings
  add_part "word/styles.xml", styles
  add_part "word/fontTable.xml", fonts
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document


6
7
8
# File 'lib/openxml/docx/package.rb', line 6

def document
  @document
end

#fontsObject (readonly)

Returns the value of attribute fonts


6
7
8
# File 'lib/openxml/docx/package.rb', line 6

def fonts
  @fonts
end

#footersObject (readonly)

Returns the value of attribute footers


6
7
8
# File 'lib/openxml/docx/package.rb', line 6

def footers
  @footers
end

#headersObject (readonly)

Returns the value of attribute headers


6
7
8
# File 'lib/openxml/docx/package.rb', line 6

def headers
  @headers
end

#image_namesObject (readonly)

Returns the value of attribute image_names


6
7
8
# File 'lib/openxml/docx/package.rb', line 6

def image_names
  @image_names
end

#settingsObject (readonly)

Returns the value of attribute settings


6
7
8
# File 'lib/openxml/docx/package.rb', line 6

def settings
  @settings
end

#stylesObject (readonly)

Returns the value of attribute styles


6
7
8
# File 'lib/openxml/docx/package.rb', line 6

def styles
  @styles
end

Instance Method Details


100
101
102
103
104
105
106
107
108
# File 'lib/openxml/docx/package.rb', line 100

def add_footer(footer)
  footers << footer
  footer_name = "footer#{footers.count}.xml"
  Package.content_types { override "/word/#{footer_name}", TYPE_FOOTER }
  add_part "word/#{footer_name}", footer
  add_part "word/_rels/#{footer_name}.rels", footer.relationships
  relationship = document.relationships.add_relationship REL_FOOTER, footer_name
  relationship.id
end

#add_header(header) ⇒ Object


90
91
92
93
94
95
96
97
98
# File 'lib/openxml/docx/package.rb', line 90

def add_header(header)
  headers << header
  header_name = "header#{headers.count}.xml"
  Package.content_types { override "/word/#{header_name}", TYPE_HEADER }
  add_part "word/#{header_name}", header
  add_part "word/_rels/#{header_name}.rels", header.relationships
  relationship = document.relationships.add_relationship REL_HEADER, header_name
  relationship.id
end

#embed_image(path: nil, content_type: nil, into_part: nil) ⇒ Object


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/openxml/docx/package.rb', line 69

def embed_image(path: nil, content_type: nil, into_part: nil)
  return if path.nil?
  into_part = document unless into_part.respond_to?(:relationships)

  extension_match = path.match(/\.(?<extension>[^\.]+)$/)
  content_type ||= extension_match[:extension] if extension_match
  return if content_type.nil?

  content_type = "jpeg" if content_type == "jpg"
  content_type = content_type.to_sym

  File.open(path, "rb") do |source_image|
    destination_image_name = "image#{image_names.count + 1}.#{content_type}"
    add_part "word/media/#{destination_image_name}", OpenXml::Parts::UnparsedPart.new(source_image.read)
    image_names << destination_image_name

    image_relationship = into_part.relationships.add_relationship REL_IMAGE, "media/#{destination_image_name}"
    image_relationship.id
  end
end

#embed_truetype_font(path: nil, name: nil) ⇒ Object


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/openxml/docx/package.rb', line 51

def embed_truetype_font(path: nil, name: nil)
  File.open(path, "rb") do |source_font|
    obfuscation_data = obfuscate_font source_font
    data = obfuscation_data[:bytes] << source_font.read
    destination_font_name = "font#{fonts.fonts.count + 1}.odttf"
    add_part "word/fonts/#{destination_font_name}", OpenXml::Parts::UnparsedPart.new(data)
    font_relationship = fonts.relationships.add_relationship REL_FONT, "fonts/#{destination_font_name}"

    font_description = OpenXml::Docx::Elements::Font.new
    font_description.font_name = name
    embed_tag = OpenXml::Docx::Elements::EmbedRegular.new
    embed_tag.font_key = "{#{obfuscation_data[:key]}}"
    embed_tag.relationship_id = font_relationship.id
    font_description << embed_tag
    fonts << font_description
  end
end