pdf-reader — Ruby gem

PDF To Doc coversion with the help of pdf-reader gem

require ‘rubygems’
require ‘pdf/reader’
class PageTextReceiver
attr_accessor :content

def initialize
@content = []
end

  1. Called when page parsing starts

def begin_page(arg = nil)
@content << “”
end

  1. record text that is drawn on the page

def show_text(string, *params)
@content.last << string.strip
end

  1. there’s a few text callbacks, so make sure we process them all

alias :super_show_text :show_text
alias :move_to_next_line_and_show_text :show_text
alias :set_spacing_next_line_show_text :show_text

  1. this final text callback takes slightly different arguments

def show_text_with_positioning(*params)
params = params.first
params.each { |str| show_text(str) if str.kind_of?(String)}
end
end

receiver = PageTextReceiver.new
pdf = PDF::Reader.file(“c:\\user.pdf”, receiver)
file=File.open(“test.doc”,”w”)

file.puts receiver.content.inspect
file.close

Advertisements

One thought on “pdf-reader — Ruby gem

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s