Skip to content

Instantly share code, notes, and snippets.

@yan13to
Last active January 29, 2020 09:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yan13to/275ac636027d1e2d72b05da3084ad152 to your computer and use it in GitHub Desktop.
Save yan13to/275ac636027d1e2d72b05da3084ad152 to your computer and use it in GitHub Desktop.
module AsposeExpoter
Rjb::load(File.join(Rails.root, '/vendor/lib/Aspose.Words.jdk16.jar'), jvmargs=['-Djava.awt.headless=true'])
Rjb::import('com.aspose.words.License').new._invoke('setLicense', 'Ljava.io.InputStream;', Rjb::import('java.io.FileInputStream').new(Rjb::import('java.io.File').new(File.join(Rails.root, '/vendor/lib/Aspose.Words.lic'))))
TempFileRoot = '/tmp'
def to_data_xml_file
fn = File.join(TempFileRoot, "data-xml_#{id}_#{Time.now.to_i}.xml")
open(fn,'w') do |f|
f.write to_xml
end
fn
end
def to_word_xml_data(style_name = layout_class)
xslt = XML::XSLT.new
xslt.xml = to_xml
xslt.xsl = xsl_template_path(style_name)
if block_given?
yield(xslt)
else
xslt.serve
end
end
def to_word_xml(style_name = layout_class)
fn = File.join(TempFileRoot, "#{self.class.to_s.underscore}-#{id}-#{style_name}-#{rand(99999)}.xml")
to_word_xml_data(style_name) do |xslt|
xslt.save(fn)
end
fn
end
def combine_to_generate_docx(new_docx_path, style_name = layout_class)
FileUtils.copy(docx_template_path(style_name), new_docx_path)
Zip::Archive.open(new_docx_path, Zip::CREATE) do |zipfile|
zipfile.add_or_replace_buffer('word/document.xml', to_word_xml_data(style_name))
end
new_docx_path
end
def load_aspose_doc_and_xml(style_name = layout_class)
word_xml_fn = to_word_xml(style_name)
return nil unless File.exists?(word_xml_fn)
aspose_doc = Rjb::import('com.aspose.words.Document').new(word_xml_fn)
yield(aspose_doc)
end
def convert_and_save_to(fn, style_name = layout_class)
load_aspose_doc_and_xml(style_name) do |doc|
doc.save(fn)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment