Sunil Jose
Sunil Jose

Reputation: 325

Parse plain text from rtf text in ruby

I have an rtf text shown like below,

{\rtf1\ansi\deff0 {\fonttbl {\f0 Courier;}}
{\colortbl;\red0\green0\blue0;\red255\green0\blue0;}
This line is the default color\line
\cf2
\tab This line is red and has a tab before it\line
\cf1
\page This line is the default color and the first line on page 2
}

I want to parse plain text from rtf text. Is there any plugins or any other solutions available in ruby, to parse plain text from rtf text?

Upvotes: 0

Views: 903

Answers (1)

Macro
Macro

Reputation: 306

You can use this Ruby-rtf Gem

require 'ruby-rtf'

data = File.read('./rich.rtf')
parser = RubyRTF::Parser.new
parsed_text = parser.parse(data).sections.map do |val|
  val[:text]
end.join(' ')

Upvotes: 4

Related Questions