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.



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

def initialize
  super

  rels.add_relationship REL_DOCUMENT, "/word/document.xml"
  @doc_rels = OpenXml::Parts::Rels.new
  @font_rels = OpenXml::Parts::Rels.new
  @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 = []

  doc_rels.add_relationship REL_STYLES, "styles.xml"
  doc_rels.add_relationship REL_SETTINGS, "settings.xml"
  doc_rels.add_relationship REL_FONT_TABLE, "fontTable.xml"

  add_part "word/_rels/document.xml.rels", doc_rels
  add_part "word/_rels/fontTable.xml.rels", font_rels
  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

#doc_relsObject (readonly)

Returns the value of attribute doc_rels.



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

def doc_rels
  @doc_rels
end

#documentObject (readonly)

Returns the value of attribute document.



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

def document
  @document
end

#font_relsObject (readonly)

Returns the value of attribute font_rels.



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

def font_rels
  @font_rels
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

#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



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

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
  relationship = doc_rels.add_relationship REL_FOOTER, footer_name
  relationship.id
end

#add_header(header) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/openxml/docx/package.rb', line 66

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
  relationship = doc_rels.add_relationship REL_HEADER, header_name
  relationship.id
end

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



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

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 = font_rels.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