Class: Tabula::TextStripper
- Inherits:
-
Object
- Object
- Tabula::TextStripper
- Defined in:
- lib/tabula/pdf/text_stripper.rb
Overview
Extracts text elements from PDF pages using pdf-reader. Uses pdf-reader's PageTextReceiver for proper font encoding and CMap handling.
Instance Attribute Summary collapse
-
#min_char_height ⇒ Object
readonly
Returns the value of attribute min_char_height.
-
#min_char_width ⇒ Object
readonly
Returns the value of attribute min_char_width.
Instance Method Summary collapse
-
#extract ⇒ Array<TextElement>
Extract text elements from the page.
-
#initialize(page) ⇒ TextStripper
constructor
A new instance of TextStripper.
Constructor Details
#initialize(page) ⇒ TextStripper
Returns a new instance of TextStripper.
10 11 12 13 14 15 |
# File 'lib/tabula/pdf/text_stripper.rb', line 10 def initialize(page) @page = page @text_elements = [] @min_char_width = Float::INFINITY @min_char_height = Float::INFINITY end |
Instance Attribute Details
#min_char_height ⇒ Object (readonly)
Returns the value of attribute min_char_height.
80 81 82 |
# File 'lib/tabula/pdf/text_stripper.rb', line 80 def min_char_height @min_char_height end |
#min_char_width ⇒ Object (readonly)
Returns the value of attribute min_char_width.
80 81 82 |
# File 'lib/tabula/pdf/text_stripper.rb', line 80 def min_char_width @min_char_width end |
Instance Method Details
#extract ⇒ Array<TextElement>
Extract text elements from the page
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/tabula/pdf/text_stripper.rb', line 19 def extract # Use pdf-reader's PageTextReceiver for proper font encoding receiver = PDF::Reader::PageTextReceiver.new receiver.page = @page @page.walk(receiver) # Get merged text runs for readable output # merge: true combines adjacent characters into words/phrases runs = receiver.runs( merge: true, skip_zero_width: true, skip_overlapping: true ) # Get page dimensions and rotation rotation = @page.attributes[:Rotate] || 0 runs.each do |run| next if run.text.nil? || run.text.empty? next unless printable?(run.text) # pdf-reader already applies rotation transformation # For rotated pages, y coordinates are negative # For non-rotated pages, we need to flip from bottom-origin to top-origin if [90, 270].include?(rotation) # Rotated pages: y is negative, convert to positive top = run.y.abs else # Non-rotated pages: convert from bottom-origin to top-origin page_height = calculate_page_height top = page_height - run.y end left = run.x width = run.width height = run.font_size # Detect text direction from Unicode character properties direction = rtl_text?(run.text) ? TextElement::DIRECTION_RTL : TextElement::DIRECTION_LTR element = TextElement.new( top: top, left: left, width: width, height: height, text: run.text, font_name: nil, # pdf-reader doesn't expose font name in runs font_size: run.font_size, width_of_space: estimate_space_width(run), direction: direction ) @text_elements << element @min_char_width = [@min_char_width, width].min if width.positive? @min_char_height = [@min_char_height, height].min if height.positive? end @text_elements end |