Class: OpenXml::Docx::Package
- Inherits:
-
Package
- Object
- Package
- OpenXml::Docx::Package
- Defined in:
- lib/openxml/docx/package.rb
Instance Attribute Summary collapse
-
#document ⇒ Object
readonly
Returns the value of attribute document.
-
#fonts ⇒ Object
readonly
Returns the value of attribute fonts.
-
#footers ⇒ Object
readonly
Returns the value of attribute footers.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#image_names ⇒ Object
readonly
Returns the value of attribute image_names.
-
#numbering ⇒ Object
readonly
Returns the value of attribute numbering.
-
#settings ⇒ Object
readonly
Returns the value of attribute settings.
-
#styles ⇒ Object
readonly
Returns the value of attribute styles.
Instance Method Summary collapse
- #add_footer(footer) ⇒ Object
- #add_header(header) ⇒ Object
- #embed_image(path: nil, content_type: nil, into_part: nil) ⇒ Object
- #embed_image_data(data: nil, content_type: nil, into_part: nil) ⇒ Object
- #embed_truetype_font(path: nil, name: nil) ⇒ Object
-
#initialize ⇒ Package
constructor
A new instance of Package.
Constructor Details
#initialize ⇒ Package
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
#document ⇒ Object (readonly)
Returns the value of attribute document.
7 8 9 |
# File 'lib/openxml/docx/package.rb', line 7 def document @document end |
#fonts ⇒ Object (readonly)
Returns the value of attribute fonts.
7 8 9 |
# File 'lib/openxml/docx/package.rb', line 7 def fonts @fonts end |
#footers ⇒ Object (readonly)
Returns the value of attribute footers.
7 8 9 |
# File 'lib/openxml/docx/package.rb', line 7 def @footers end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
7 8 9 |
# File 'lib/openxml/docx/package.rb', line 7 def headers @headers end |
#image_names ⇒ Object (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 |
#numbering ⇒ Object (readonly)
Returns the value of attribute numbering.
7 8 9 |
# File 'lib/openxml/docx/package.rb', line 7 def numbering @numbering end |
#settings ⇒ Object (readonly)
Returns the value of attribute settings.
7 8 9 |
# File 'lib/openxml/docx/package.rb', line 7 def settings @settings end |
#styles ⇒ Object (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
#add_footer(footer) ⇒ Object
112 113 114 115 116 117 118 119 120 |
# File 'lib/openxml/docx/package.rb', line 112 def () << = "footer#{.count}.xml" content_types.add_override "/word/#{}", TYPE_FOOTER add_part "word/#{}", add_part "word/_rels/#{}.rels", .relationships relationship = document.relationships.add_relationship REL_FOOTER, 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" content_types.add_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 (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? URI.open(path, "rb") do |source_image| (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 (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 (path: nil, name: nil) URI.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 = OpenXml::Docx::Elements::EmbedRegular.new .font_key = "{#{obfuscation_data[:key]}}" .relationship_id = font_relationship.id font_description << fonts << font_description end end |