在 Ruby on Rails 中,如何将 html 转换为 word?

发布于 2024-08-13 15:34:21 字数 36 浏览 1 评论 0原文

我怎样才能把html转换成word

谢谢。

how can I convert html to word

thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

指尖上得阳光 2024-08-20 15:34:21

我创建了一个 Ruby html 到 word gem ,应该可以帮助您做到这一点。您可以在 https://github.com/nickfrandsen/htmltoword 查看 - 您只需将其传递给html 字符串,它将创建一个相应的 word docx 文件。

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

希望您觉得它有帮助。

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.

瞳孔里扚悲伤 2024-08-20 15:34:21

我不知道有任何解决方案可以做到这一点,即将 HTML 转换为 Word 格式。如果您的字面意思是这样,您将必须首先使用 Nokogiri 之类的工具解析 HTML 文档。如果您的意思是要输出保存在模型对象中的数据,那么显然不需要解析 HTML!至于输出到 Word,恐怕您似乎必须通过 OLE 直接与正在运行的 Microsoft Word 实例进行交互!

快速 Google 搜索 win32ole ruby​​ word 将帮助您入门:

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

祝你好运!

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!

梅倚清风 2024-08-20 15:34:21

我同意 CodeJoust 的观点,最好生成 PDF。但是,如果你确实需要生成Word文档,那么你可以这样做:

如果你的服务器是Windows机器,你可以在其中安装Office,并使用ruby的OLE绑定将Word文档生成到公用文件夹中,然后将其传递到公共文件夹中。文件在响应中。

要使用 ruby​​ 的 OLE 绑定,请参阅适用于 Windows 的一键式 ruby​​ 安装程序随附的“Programming Ruby”电子书。您可能必须使用自定义逻辑从 HTML 转换为 Word,除非您可以在 Word 的 OLE api 中找到执行此操作的函数。

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.

往事风中埋 2024-08-20 15:34:21

http://prawn.majesticeacreature.com/

您可以允许用户下载 PDF 或 .html文件,但没有任何有用的 ruby​​ 库可以做到这一点。你最好生成一个“可打印和可下载”的版本,没有太多的样式,和/或使用像虾这样的库生成 pdf 版本。

你总是可以生成一个简单的 .rtf 文件,我想 word 会很高兴阅读...

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文