#!/usr/bin/env ruby

require 'rubygems'
$LOAD_PATH.unshift(File.dirname(__FILE__) + "/../lib")

require 'pdf/reader'

class PageTextReceiver
  attr_accessor :content

  # Called when page parsing starts
  def end_page(arg = nil)
    if @content
      puts @content
      @content = nil
      puts
    end
  end

  def show_text(string, *params)
    @content = "" if @content.nil?
    @content << string
  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

  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

if ARGV.empty?
  PDF::Reader.new.parse($stdin, receiver)
else
  PDF::Reader.file(ARGV[0], receiver)
end
