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

Returns a new instance of Package.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/openxml/docx/package.rb', line 30

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
  @numbering = OpenXml::Docx::Parts::Numbering.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"
  document.relationships.add_relationship REL_NUMBERING, "numbering.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
  add_part "word/numbering.xml", numbering
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



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

def document
  @document
end

#fontsObject (readonly)

Returns the value of attribute fonts.



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

def fonts
  @fonts
end

#footersObject (readonly)

Returns the value of attribute footers.



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

def footers
  @footers
end

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

#image_namesObject (readonly)

Returns the value of attribute image_names.



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

def image_names
  @image_names
end

#numberingObject (readonly)

Returns the value of attribute numbering.



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

def numbering
  @numbering
end

#settingsObject (readonly)

Returns the value of attribute settings.



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

def settings
  @settings
end

#stylesObject (readonly)

Returns the value of attribute styles.



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

def styles
  @styles
end

Instance Method Details



112
113
114
115
116
117
118
119
120
# File 'lib/openxml/docx/package.rb', line 112

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



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

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



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/openxml/docx/package.rb', line 75

def embed_image(path: nil, content_type: nil, into_part: nil)
  return if path.nil?

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

  open(path, "rb") do |source_image|
    embed_image_data(data: source_image.read, content_type: content_type, into_part: into_part)
  end
end

#embed_image_data(data: nil, content_type: nil, into_part: nil) ⇒ Object



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

def embed_image_data(data: nil, content_type: nil, into_part: nil)
  return if data.nil? || content_type.nil?
  into_part = document unless into_part.respond_to?(:relationships)

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

  destination_image_name = "image#{image_names.count + 1}.#{content_type}"
  add_part "word/media/#{destination_image_name}", OpenXml::Parts::UnparsedPart.new(data)
  image_names << destination_image_name

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

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



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/openxml/docx/package.rb', line 57

def embed_truetype_font(path: nil, name: nil)
  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