Convert HTML to word file ?

If you are using Rails:

in initializers/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

define set_header in application_controller.rb:

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

now define a show.msword.erb #you can use any template handler like haml etc.

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

Leave a Comment