parthnagori / omnidocx Goto Github PK
View Code? Open in Web Editor NEWA Ruby Gem to merge docx files, write images to a docx file and perform pattern replacements in a word document
License: MIT License
A Ruby Gem to merge docx files, write images to a docx file and perform pattern replacements in a word document
License: MIT License
The Omnidocx::Docx.merge_documents
method is static, but it sets instance variables. As a result, running in a threaded environment you will end up sharing values and writing between files. You can recreate this pretty easily by setting debugger breakpoints after instance variables are set, trigger merging documents on multiple threads, and you'll see data written between files incorrectly.
def self.merge_documents(documents_to_merge = [], final_path, page_break)
temp_file = Tempfile.new('docxedit-')
documents_to_merge_count = documents_to_merge.count
if documents_to_merge_count < 2
return "Pass atleast two documents to be merged" #minimum two documents required to merge
end
# FIXME: _These should not be instance variables_
@main_document_zip = Zip::File.new(documents_to_merge.first)
@main_document_xml = Nokogiri::XML(@main_document_zip.read(DOCUMENT_FILE_PATH))
@main_body = @main_document_xml.xpath("//w:body")
@rel_doc = ""
@cont_type_doc = ""
@style_doc = ""
doc_cnt = 0
#cnt variable to construct relationship ids, taken a high value 100 to avoid duplication
cnt = 100
tbl_cnt = 10
#hash to store information about the media files and their corresponding new names
media_hash = {}
#rid_hash to store relationship information
rid_hash = {}
#table hash to store information if any tables present
table_hash = {}
#head_foot_media hash to store if any media files present in header/footer
head_foot_media = {}
#a counter for docPr element in the main document body
docPr_id = 100
#array to store content type information about media extensions
default_extensions = []
#array to store override content type information
override_partnames = []
#array to store information about additional content types other than the ones present in the first(main) document
additional_cont_type_entries = []
# prepare initial set of data from first document
@main_document_zip.entries.each do |zip_entrie|
in_stream = zip_entrie.get_input_stream.read
I am simply merging 2 documents, out of which of which the first document of array does not have any header and footer, at that time i am getting this exception NoMethodError: undefined method 'attributes' for nil:NilClass
on line 323
in omnidocx.rb
@parthnagori can you release 0.1.4, version change was merged in with #8
omnidocx was resolved to 0.1.4, which depends on
rubyzip (>= 1.1.6, ~> 1.1)
Would be nice to move the dependency of ruby zip to a grather version
In def self.merge_documents
, when attaching a new Relationship node (new_rel_node = "<Relationship Id=#{new_id} Type=#{node["Type"]} Target=#{target_val} />"
), Nokogiri will attempt to fix the XML and in the process lowercase all of the attributes, resulting in a corrupt word/_rels/document.xml.rels
(e.g. <relationship id="rId101" type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" target="media/image101.png"/>
).
Proposed solution would be to either properly escape new_rel_node
, e.g. new_rel_node = "<Relationship Id=\"#{new_id}\" Type=\"#{node["Type"]}\" Target=\"#{target_val}\" />"
, or to use Nokogiri::XML::Node.new
to properly create the node
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.