首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何直接使用pandoc在Sinatra应用程序中生成docx文件?

问如何直接使用pandoc在Sinatra应用程序中生成docx文件?
EN

Stack Overflow用户
提问于 2012-11-07 19:54:46
回答 1查看 993关注 0票数 1

我有一个Sinatra应用程序,需要提供微软Word格式的可下载报告。我创建报告的方法是使用ERB生成内容,然后将生成的HTML转换为docx。潘多克似乎是实现这一目标的最佳工具,但我的实现涉及到生成一些临时文件,这些文件让人感觉很混乱。

是否有更直接的方法生成docx文件并将其发送给用户?

我知道PandocRuby是存在的,但我无法完全实现我的目的。下面是我当前实现的一个示例:

代码语言:javascript
复制
  #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
EN

回答 1

Stack Overflow用户

发布于 2013-05-29 01:34:22

最近对pandoc的更新增加了对管道二进制输出到标准输出的支持。这能解决你的问题吗?

我没有任何使用Sinatra的经验,我也没有尝试使用pandoc来输送二进制输出,但是类似于

代码语言:javascript
复制
puts PandocRuby.convert(input, :from => :html, :to => :docx)

也许能起作用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13277168

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档