Skip to content

Instantly share code, notes, and snippets.

@liangzan
Created December 11, 2009 08:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liangzan/254065 to your computer and use it in GitHub Desktop.
Save liangzan/254065 to your computer and use it in GitHub Desktop.
Before('@pdf') do
$pdf_file_path = File.join(RAILS_ROOT, 'tmp', "temp.pdf")
end
After('@pdf') do
File.delete($pdf_file_path) if File.exist?($pdf_file_path)
end
#http://github.com/yob/pdf-reader/blob/master/examples/rspec.rb
class PageTextReceiver
attr_accessor :content
def initialize
@content = []
end
# Called when page parsing starts
def begin_page(arg = nil)
@content << ""
end
# record text that is drawn on the page
def show_text(string, *params)
@content.last << string.strip
end
# 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
# 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
module FileHelpers
def pdf_reader
receiver = PageTextReceiver.new
PDF::Reader.file($pdf_file_path, receiver)
receiver
end
end
World(FileHelpers)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment