2012-03-02 41 views
3

我是一个rails新手。有人可以告诉我是否有一种快速简便的方法来生成rtf文件供人们使用rails进行下载? 例如,如果我有“views/users/show.html.erb”视图通常输出到HTML,所以我想让人们能够下载为一个相同的RTF文件?在Rails 3.2中生成rtf文件

+0

发现这个gem https:/ /github.com/thechrisoshow/rtf – 2012-03-02 20:13:46

回答

7

ruby-rtf是你正在寻找的宝石。有RTF代的一些例子here

一下添加到初始化/ mime_types.rb:

Mime::Type.register "text/richtext", :rtf 

代码给你一个想法:

document = RTF::Document.new(RTF::Font.new(RTF::Font::ROMAN, 'Times New Roman')) 
document.paragraph do |p| 
    p << "This is the first sentence in the paragraph. " 
    p << "This is the second sentence in the paragraph. " 
    p << "And this is the third sentence in the paragraph." 
end 

send_file document, :type=>"text/richtext" 
+1

链接应该是https://rubygems.org/gems/ruby-rtf – 2013-02-18 09:06:57

+0

修复了链接。谢谢@AlexGhiculescu – ScottJShea 2013-02-18 17:44:46