ruby-on-rails – 将HTML转换为word文件?
发布时间:2020-12-17 01:25:16 所属栏目:百科 来源:网络整理
导读:如何转换word文件中的 ruby文件,即(docx文件).对于pdf,我们大虾宝石.但是有没有word文件的宝石.我试图在word文件中转换我的html文件,以便它也可以为用户编辑.在那种情况下该怎么办?我打算在word文件中转换该文件.会有可能吗? 解决方法 如果您使用Rails:
如何转换word文件中的
ruby文件,即(docx文件).对于pdf,我们大虾宝石.但是有没有word文件的宝石.我试图在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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |