开发者

In Ruby on Rails, how can I convert html to word?

开发者 https://www.devze.com 2022-12-13 16:38 出处:网络
开发者_开发问答how can I convert html to word thanks.I have created a Ruby html to word gem that should help you do just that. You can check it out at https://github.com/nickfrandsen/htmltoword - You
开发者_开发问答

how can I convert html to word

thanks.


I have created a Ruby html to word gem that should help you do just that. You can check it out at https://github.com/nickfrandsen/htmltoword - You simply pass it a html string and it will create a corresponding word docx file.

def show
  respond_to do |format|
    format.docx do
      file = Htmltoword::Document.create params[:docx_html_source], "file_name.docx"
      send_file file.path, :disposition => "attachment"
    end
  end
end

Hope you find it helpful.


I am not aware of any solution which does this, i.e. convert HTML to Word format. If you literally mean that, you will have to parse the HTML document first using something like Nokogiri. If you mean you want to output data persisted in your model objects, there is obviously no need to parse HTML! As far as outputting to Word, I'm afraid it looks as if you will have to directly interface with a running instance of Microsoft Word via OLE!

A quick google search for win32ole ruby word will get you started:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/241606

Good luck!


I agree with CodeJoust that it is better to generate a PDF. However, if you really need to generate a Word document then you can do the following:

If your server is a Windows machine, you can install Office in it and use ruby's OLE binding to generate the Word document into the public folder and then deliver the file in the response.

To use ruby's OLE binding, see the "Programming Ruby" ebook that comes with the one-click ruby installer for Windows. You may have to use custom logic to convert from HTML to Word unless you can find a function in the OLE api of Word to do that.


http://prawn.majesticseacreature.com/

You could allow the user to download a PDF or a .html file, but there aren't any helpful ruby libraries to do that. You're better off generating a 'printable and downloadable' version, without much styling, and/or a pdf version using a library like prawn.

You could always generate a simple .rtf file, I think word'll be pretty happy reading that...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号