Complete Ruby examples for common PDF to Text tasks. See the Ruby SDK guide for installation and result handling.
You can run the examples below with the displayed API credentials. Update the input URLs and filenames as needed. Each Ruby example can be copied and run independently.
Choose file, in-memory, or output-stream handling for the text result. Stream examples use a binary file stream as their destination.
The memory variants return binary Ruby Strings containing UTF-8 bytes. Set their encoding to UTF-8 before using them as text.
Upload an existing invoice.pdf file.
require 'pdfcrowd' client = Pdfcrowd::PdfToTextClient.new('demo', 'demo') client.convertFileToFile('invoice.pdf', 'invoice.txt')
require 'pdfcrowd' client = Pdfcrowd::PdfToTextClient.new('demo', 'demo') text_bytes = client.convertFile('invoice.pdf') # binary String puts text_bytes.bytesize
require 'pdfcrowd' client = Pdfcrowd::PdfToTextClient.new('demo', 'demo') File.open('invoice.txt', 'wb') do |output_stream| client.convertFileToStream('invoice.pdf', output_stream) end
Fetch a PDF from its URL.
require 'pdfcrowd' client = Pdfcrowd::PdfToTextClient.new('demo', 'demo') client.convertUrlToFile('https://your-server.com/invoice.pdf', 'invoice.txt')
require 'pdfcrowd' client = Pdfcrowd::PdfToTextClient.new('demo', 'demo') text_bytes = client.convertUrl('https://your-server.com/invoice.pdf') # binary String puts text_bytes.bytesize
require 'pdfcrowd' client = Pdfcrowd::PdfToTextClient.new('demo', 'demo') File.open('invoice.txt', 'wb') do |output_stream| client.convertUrlToStream( 'https://your-server.com/invoice.pdf', output_stream ) end
Read invoice.pdf as binary data. In an application, pass the bytes you already hold in memory instead.
require 'pdfcrowd' client = Pdfcrowd::PdfToTextClient.new('demo', 'demo') pdf_data_1 = File.binread('invoice.pdf') # binary String client.convertRawDataToFile(pdf_data_1, 'invoice.txt')
require 'pdfcrowd' client = Pdfcrowd::PdfToTextClient.new('demo', 'demo') pdf_data_1 = File.binread('invoice.pdf') # binary String text_bytes = client.convertRawData(pdf_data_1) # binary String puts text_bytes.bytesize
require 'pdfcrowd' client = Pdfcrowd::PdfToTextClient.new('demo', 'demo') File.open('invoice.txt', 'wb') do |output_stream| pdf_data_1 = File.binread('invoice.pdf') # binary String client.convertRawDataToStream(pdf_data_1, output_stream) end
Open invoice.pdf as a binary input stream and save the result to invoice.txt.
require 'pdfcrowd' client = Pdfcrowd::PdfToTextClient.new('demo', 'demo') File.open('invoice.pdf', 'rb') do |input_stream| client.convertStreamToFile(input_stream, 'invoice.txt') end
Convert a PDF URL, interpret the returned bytes as UTF-8, validate the encoding, and print the resulting string.
require 'pdfcrowd' client = Pdfcrowd::PdfToTextClient.new('demo', 'demo') text = client.convertUrl('https://your-server.com/invoice.pdf') # binary String text.force_encoding(Encoding::UTF_8) # UTF-8 String raise EncodingError, 'Invalid UTF-8 text' unless text.valid_encoding? puts text
These examples upload extraction.pdf, a three-page document with a two-column article, wrapped paragraphs, and invoice fields.
Extract pages 1 and 3 of extraction.pdf into excerpt.txt.
require 'pdfcrowd' client = Pdfcrowd::PdfToTextClient.new('demo', 'demo') client.setPrintPageRange('1,3') client.convertFileToFile('extraction.pdf', 'excerpt.txt')
Extract the article on page 1 into article.txt without spacing that recreates the PDF's columns. For tables, the default layout mode usually preserves column alignment better.
require 'pdfcrowd' client = Pdfcrowd::PdfToTextClient.new('demo', 'demo') client.setNoLayout(true) client.setPrintPageRange('1') client.convertFileToFile('extraction.pdf', 'article.txt')
Extract page 2 into paragraphs.txt, joining wrapped lines with paragraph detection and removing line-end hyphens. For example, infor- followed by mation becomes information. Paragraph detection depends on the document's layout.
require 'pdfcrowd' client = Pdfcrowd::PdfToTextClient.new('demo', 'demo') client.setParagraphMode('bounding-box') client.setPrintPageRange('2') client.setRemoveHyphenation(true) client.convertFileToFile('extraction.pdf', 'paragraphs.txt')
Crop away the headers and footers on every page, or extract only the customer, invoice, and reference fields on page 3.
The coordinates below match a 612 × 792 point (US Letter) page. Measure X and Y from the top-left corner; 72 points equal one inch. Adjust the regions for your PDF.
require 'pdfcrowd' client = Pdfcrowd::PdfToTextClient.new('demo', 'demo') client.setCropAreaHeight(648) client.setCropAreaWidth(540) client.setCropAreaX(36) client.setCropAreaY(72) client.convertFileToFile('extraction.pdf', 'body.txt')
require 'pdfcrowd' client = Pdfcrowd::PdfToTextClient.new('demo', 'demo') client.setCropAreaHeight(70) client.setCropAreaWidth(300) client.setCropAreaX(36) client.setCropAreaY(290) client.setPrintPageRange('3') client.convertFileToFile('extraction.pdf', 'fields.txt')
The default output has no page separators. Insert form-feed characters (\f) for processing text by page, or add a visible --- PAGE BREAK --- marker on its own line.
require 'pdfcrowd' client = Pdfcrowd::PdfToTextClient.new('demo', 'demo') client.setPageBreakMode('default') client.convertFileToFile('extraction.pdf', 'pages.txt')
require 'pdfcrowd' client = Pdfcrowd::PdfToTextClient.new('demo', 'demo') client.setCustomPageBreak(' --- PAGE BREAK --- ') client.setPageBreakMode('custom') client.convertFileToFile('extraction.pdf', 'separated.txt')