我有一个Sinatra应用程序,需要提供微软Word格式的可下载报告。我创建报告的方法是使用ERB生成内容,然后将生成的HTML转换为docx。潘多克似乎是实现这一目标的最佳工具,但我的实现涉及到生成一些临时文件,这些文件让人感觉很混乱。
是否有更直接的方法生成docx文件并将其发送给用户?
我知道PandocRuby是存在的,但我无法完全实现我的目的。下面是我当前实现的一个示例:
#setting up the docx mime type
configure do
mime_type :docx, 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
end
# route to generate the report
get '/report/:name' do
content_type :docx
input = erb :report, :layout=>false #get the HTML content for the input file
now = Time.now.to_i.to_s #create a unique file name
input_path = File.join('tmp', now+'.txt')
f = File.new(input_path, "w+")
f.write(input.to_s) #write HTML to the input to the file
f.close()
output_path = File.join('tmp', now+'.docx') # create a unique output file
system "pandoc -f html -t docx -o #{output_path} #{input_path}" # convert the input file to docs
send_file output_path
end发布于 2013-05-29 01:34:22
最近对pandoc的更新增加了对管道二进制输出到标准输出的支持。这能解决你的问题吗?
我没有任何使用Sinatra的经验,我也没有尝试使用pandoc来输送二进制输出,但是类似于
puts PandocRuby.convert(input, :from => :html, :to => :docx)也许能起作用。
https://stackoverflow.com/questions/13277168
复制相似问题