How to extract text from PDFs (command line, Python and API)

Most PDFs already contain their text. Here is how to get it out, free, and what to do when a PDF is only a scanned image.

1. Command line: pdftotext (Poppler)

Install Poppler (apt install poppler-utils on Debian/Ubuntu, brew install poppler on macOS), then:

pdftotext report.pdf report.txt          # whole document
pdftotext -layout report.pdf -           # keep columns and tables aligned, print to the terminal
pdftotext -f 3 -l 5 report.pdf pages.txt # only pages 3 to 5
pdfinfo report.pdf                       # title, author, dates, page count

-layout keeps text in its visual position, which helps with tables and two-column layouts. Without it, text comes out in reading order, which is usually better for search or an LLM.

2. Python: pypdf

from pypdf import PdfReader   # pip install pypdf

reader = PdfReader("report.pdf")
print(len(reader.pages), reader.metadata)
text = "\n\n".join(page.extract_text() or "" for page in reader.pages)

pypdf is pure Python (no system packages), which makes it easy to use in serverless functions. For a PDF at a URL, download it first (urllib.request or requests) and pass the bytes in a BytesIO.

3. Scanned PDFs: you need OCR

If every tool above returns empty or near-empty text, the PDF is probably a scan: pictures of pages, no text layer. Extraction cannot help, only OCR (optical character recognition) can, for example Tesseract-based tools. A quick test: if you cannot select text in a PDF viewer, it is a scan.

Common problems

Hundreds of PDFs by URL

Our PDF Extractor & Parser on Apify takes a list of PDF URLs and returns full text or text per page, plus title, author, dates, page and word counts and a scanned-PDF flag, as JSON, CSV or Excel. It is meant for document-to-text steps in search and LLM / RAG pipelines and is billed per PDF. It does not OCR scans; it flags them.

Frequently asked questions

What is the best free PDF to text converter?

pdftotext (Poppler) on the command line and pypdf in Python are both free and accurate for PDFs that contain text. Try both on a difficult file: they handle odd layouts differently.

Why is the extracted text empty?

The PDF is most likely a scan with no text layer. Use OCR.

How do I keep tables when converting a PDF to text?

Use pdftotext -layout to keep the columns aligned. For real table structure (rows and cells) you need a table-extraction tool, since plain text has no cells.

Is there a free PDF to text API?

You can wrap pdftotext or pypdf in a small function yourself. Hosted APIs save you the hosting and scaling; most, including ours, charge per document.

Written with AI assistance. The pdftotext, pdfinfo and pypdf commands were run on Linux against a sample PDF on 2026-10-01. The Apify tool is ours; the APIs are free to call directly.