Module: Pdfrb::Task::ExtractText

Defined in:
lib/pdfrb/task/extract_text.rb

Overview

Extract plain text from a document by walking each page's content stream. Decodes glyph codes back to Unicode using:

1. The font's /ToUnicode CMap (preferred — gives proper
 Unicode for CIDFont / embedded TTF fonts).
2. The font's /Encoding + AdobeStandardEncoding glyph names
 (fallback for Type1 standard fonts).
3. ASCII passthrough (last resort).

Tracks the text matrix to insert line breaks at vertical moves and approximate paragraph breaks.

Defined Under Namespace

Classes: TextCollector

Class Method Summary collapse

Class Method Details

.call(document) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/pdfrb/task/extract_text.rb', line 19

def call(document)
  results = []
  document.pages.each do |page|
    extractor = TextCollector.new(document, page)
    begin
      extractor.process(page.decoded_content)
    rescue StandardError
      # Malformed content stream — best effort, skip.
    end
    text = extractor.text
    block_given? ? yield(page, text) : (results << text)
  end
  block_given? ? document : results
end