wyc
wyc

Reputation: 55283

Grabbing the text from .doc, .docx, and rtf files and displaying them into a text field (Rails)

I have a Post model:

  create_table "posts", :force => true do |t|
    t.string   "title"
    t.string   "content"
    t.integer  "user_id"
    t.datetime "created_at",                            :null => false
    t.datetime "updated_at",                            :null => false
    t.integer  "comments_count",     :default => 0,     :null => false
    t.boolean  "published",          :default => false
    t.datetime "published_at"
    t.boolean  "draft",              :default => false
  end

And this is its form:

<%= form_for(@post, :html => { :multipart => true }) do |f| %>
  <%= render 'shared/error_messages' %>
  <div class="field">
    <%= f.label :title %>
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :content %>
    <%= f.text_area :content %>
  </div>

  <div class="field">
    <%= f.label :draft %>
    <%= f.check_box :draft %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

I'm looking for a way to grab the text from .doc, .docx, and rtf files and displaying them into the content text field (so the user doesn't have to open his file, copy, and paste the text into the form).

Any suggestions?

(Is there any gem, text editor, or jQuery plugin that accomplishes this?)?

EDIT:

Got stuck here:

post.rb:

class Post < ActiveRecord::Base
  require 'docx'
  .
  .
  .  
  def read_docx
    d = Docx::Document.open(self.document)
    d.each_paragraph do |p|
      puts d
    end
  end
end

posts_controller.rb:

class PostsController < ApplicationController
  before_filter :authenticate_user!, :except => [:show, :index]
  .
  .
  .
  def create
    @user = current_user
    @post = @user.posts.new(params[:post])
    @doc_text = (no idea what to do here)

    if @post.save
      redirect_to @post, notice: 'post was successfully created.'
    else
      render action: "new"
    end
  end

  def edit
    @post = Post.find(params[:id])
  end
  .
  .
  .

posts/new.html.erb:

<%= form_for(@post, :html => { :multipart => true }) do |f| %>
  <%= render 'shared/error_messages' %>
  <div class="field">
    <%= f.label :title %>
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :content %>
    <%= f.text_area :content, :value => @doc_text %>
  </div>
  .
  .
  .

I already made Paperclip upload docx files

I created a new field called :document

Upvotes: 1

Views: 1897

Answers (1)

Lucas Nogueira
Lucas Nogueira

Reputation: 541

Well I just tried docx gem and it works fine. You can get 2 examples on its github page. Sadly it does not work for doc files.

For them you can use this gem here. There is some example on the github page, but if you want to get the whole content of the doc file just do like this:

require 'msworddoc-extractor'

MSWordDoc::Extractor.load('sample.doc') do |doc|
  puts doc.whole_contents
end

There are other methods you can call for doc, like document or header. Again, check the github page.

For rtf you can go with this gem as well.

Now, to pass it inside the content is easy. Just definy how you will get the data from the files, like an external lib called on the controller:

@doc_text = Parser.doc("file.doc")
@docx_text = Parser.docx("file.docx")
@rtf_text = Parser.rtf("file.rtf")

Or just get the values directly or by any method you have in mind. To show it on view you just have to add :value option like this:

<%= f.text_area :content, :value => @doc_text %> 
#Where @doc_text is the data from file

Upvotes: 3

Related Questions