Extraction · Ruby
Extract text from a PDF in Ruby
Pull the text out of any PDF from Ruby. rust-pdf walks the content stream, maps shown glyph codes back to Unicode through each font's ToUnicode map, and infers spaces and line breaks, including two-byte Type0 fonts and CJK.
Last updated: 2026-06-29
Why Ruby needs this
Prawn only writes PDFs, so reading one in Ruby has meant adding pdf-reader or piping files through a pdftotext process on the server. rust-pdf parses the content stream itself and hands the text straight back.
Reliable extraction is harder than it looks: codes in the stream are font-specific and must be mapped back through each ToUnicode CMap, and spacing has to be inferred from glyph positioning. rust-pdf does both, including two-byte Type0 and CJK, so you get the text a human reads, ready for search or data pipelines.
Call RustPdf.extract_text from a Sidekiq indexing worker or a Rails search action and you get a UTF-8 string ready for Elasticsearch or pg_search; the gem binds through Fiddle, so there is no external pdftotext process to spawn, manage or rescue per file.
- Maps glyph codes to Unicode via each font's ToUnicode map, including two-byte Type0 and CJK.
- Infers spaces from large negative adjustments and line breaks from text positioning.
- Pull raster images out too: JPEGs verbatim as .jpg, everything else as .png.
Extract text in Ruby with rust-pdf
Install with RubyGems, then call the same idiomatic API every rust-pdf binding shares. The snippet below is real Ruby code from the reference docs.
gem install rustpdf
data = File.binread("report.pdf")
puts RustPdf.extract_text(data)
n = RustPdf.extract_images_to_dir(data, "out_images/") # returns how many were written
puts "wrote #{n} image(s)"
This is part of the free tier in Ruby. No license required.
Full Ruby reference in the documentation.
Text extraction in Ruby: FAQ
Does it extract Unicode and CJK text in Ruby?
Yes. Each shown code is mapped back to Unicode through the font's ToUnicode CMap, including two-byte Type0 fonts, so Japanese, Greek, Arabic and other scripts come back correctly.
Can it extract scanned PDFs?
No. Extraction reads the text layer of a PDF. A scanned document is an image with no text layer, which needs OCR first. For PDFs that contain real text, extraction is exact.
Is text extraction free?
Yes. Text and image extraction are part of the free tier in Ruby. No license token is required.
Is extraction fast enough for a Sidekiq indexing job?
Yes. RustPdf.extract_text runs in-process through Fiddle with no external pdftotext subprocess, so a Sidekiq or ActiveJob worker can pull UTF-8 text and feed it straight into Elasticsearch or pg_search without spawning a CLI per file.
Turn your PDFs into searchable Ruby data
One Rust core, the same output across every language. Prototype for free, license the corporate features when you ship.