将 HTML 转换为 word 文件? [英] Convert HTML to word file ?

查看:55
本文介绍了将 HTML 转换为 word 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将 ruby​​ 文件转换为 word 文件,即(docx 文件).对于pdf,我们大虾gem.但是有没有word文件的gem.我正在尝试将我的 html 文件转换为 word 文件,以便用户也可以对其进行编辑.这种情况应该怎么办?我打算将该文件转换为 word 文件.有没有可能.

How to convert ruby file in word file i.e (docx file). For pdf, we prawn gem. But is there any gem for word file. I am trying to convert my html file in word file so that it can be editable for user too. What should do in that case ? I was planning to convert that file in word file. Will it be possible or not.

推荐答案

如果你使用 Rails:

在初始化程序/mime_types.rb 中:

Mime::Type.register 'application/vnd.ms-word', :msword 

在您的控制器中:

说你想导出表演动作:

in your controller:

say you want to export show action:

def show
  @item = Item.find params[:id]
  respond_to do |format|
    format.html # show.html.erb
    format.xml { render :xml => @item }
    format.msword { set_header('msword', "#{@item.title}.doc") }
    format.pdf do
        render :pdf => 'Coming soon...', :layout => false
    end
  end
 end

在 application_controller.rb 中定义 set_header:

def set_header(p_type, filename)
  case p_type
    when 'xls'
     headers['Content-Type'] = "application/vnd.ms-excel; charset=UTF-8'"
     headers['Content-Disposition'] = "attachment; filename="#{filename}""
     headers['Cache-Control'] = ''

    when 'msword'
     headers['Content-Type'] = "application/vnd.ms-word; charset=UTF-8"
     headers['Content-Disposition'] = "attachment; filename="#{filename}""
     headers['Cache-Control'] = ''

   end
 end

现在定义一个 show.msword.erb #你可以使用任何模板处理程序,如 haml 等.

YOUR HTML HERE TO EXPORT TO DOC
AS LIKE NORMAL ERB TEMPLATE

这篇关于将 HTML 转换为 word 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆