将HTML转换为word文件?
如何转换word文件中的ruby文件即(docx文件)。 对于pdf,我们大虾gem。 但是有没有word文件的gem。 我试图在word文件中转换我的html文件,以便它也可以为用户编辑。 那个案子该怎么办? 我打算在word文件中转换该文件。 是否有可能。
如果您使用Rails:
在initializers / mime_types.rb中:
Mime::Type.register 'application/vnd.ms-word', :msword
在你的控制器中:
说你要导出show动作:
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
使用htmltoword gem。 https://github.com/nickfrandsen/htmltoword
它自2015年11月以来尚未更新,但运作良好。