Tools,

PDF to TXT: one command, and where it breaks

pdftotext file.pdf file.txt

It comes with Poppler: brew install poppler, poppler-utils on Linux, or a poppler-windows build (not tested).

Empty file.txt? The PDF is likely a scan: OCR it first.

Get the commands

Heat table of words in order, in percent, for 12 tool settings on 9 test PDFs. Most cells are 99.9 or 100. On 2 columns, pdftotext -layout, pypdf layout and pdfplumber fall to 56.4 or 56.5. Every text tool scores 0.0 on the scan and 52.0 on the half scan. OCRmyPDF and our Actor read the scan at 96.0.

From the shell: pdftotext or pdf2txt.py

pdftotext file.pdf file.txt
pdftotext -layout file.pdf file.txt
pdf2txt.py -o file.txt file.pdf

Or five lines of Python

With pypdf, saved as pypdf_txt.py:

import sys
from pypdf import PdfReader
pages = PdfReader(sys.argv[1]).pages
text = "\n".join(page.extract_text() for page in pages)
open(sys.argv[2], "w", encoding="utf-8").write(text)

With pdfplumber, saved as plumber_txt.py:

import sys
import pdfplumber
with pdfplumber.open(sys.argv[1]) as pdf:
    text = "\n".join(page.extract_text() for page in pdf.pages)
open(sys.argv[2], "w", encoding="utf-8").write(text)

Install both with pip install pypdf pdfplumber, then run python3 pypdf_txt.py file.pdf file.txt.

Six hard layouts, five ways to read them

Words in order, or hard spots right
PDF pdftotext -layout pdf2txt.py pypdf pdfplumber
Plain page 99.9% 99.9% 99.9% 99.9% 99.9%
2 columns 98.7% 56.4% 99.9% 99.9% 56.4%
fi and fl 22/22 22/22 8/22 1/22 22/22
Line-end hyphens 0/14 0/14 0/14 0/14 0/14
Heads, page numbers 0/20 0/20 0/20 0/20 0/20
Table 0/20 20/20 0/20 20/20 20/20
Turned page 99.9% 99.9% 99.9% 99.9% 99.9%

No text tool joined line-end hyphens or dropped heads. -layout and pdfplumber mixed the 2 columns, pdf2txt.py and pypdf kept fi and fl as one character, pdftotext and pdf2txt.py broke table rows. Not shown: pypdfium2 and PDFKit broke nowhere else, PyMuPDF on tables.

What each test PDF holds
  • Plain page: 10 book pages in one column, the control. Cell: words in order.
  • 2 columns: the same text in two columns, like a paper. Cell: words in order.
  • fi and fl: the same text in Palatino, which draws fi and fl as one glyph in 14 words. Cell: the 22 words with ff, fi or fl that came out as plain letters.
  • Line-end hyphens: justified pages with 14 words broken across two lines. Cell: breaks joined back into one word.
  • Heads, page numbers: a running head and a page number on each of 10 pages. Cell: the 20 left out of the text.
  • Table: a made-up table of 20 rows (item, qty, price). Cell: rows that came out on one line.
  • Turned page: 5 of the 10 pages stored sideways and turned upright by the PDF. Cell: words in order.

Scans: OCR first, then you get text

On our scanned PDF, each text tool wrote 0 words: its pages are pictures, with no text layer. OCR adds one:

ocrmypdf --sidecar out.txt in.pdf out.pdf

out.txt holds the text OCR read: 96.0% of the words on our scan.

PDF OCR to Markdown, our Apify Actor, checks each page for a text layer and OCRs only the pages that need it. For plain text, set "outputFormat": "text".

Half-scanned PDFs: add --skip-text

Typed pages and scans in one PDF? Every text tool got only the typed half, 52.0% of the words, with no warning. OCRmyPDF exits 6:

PriorOcrFoundError: page 1 already has text! - aborting (use --force-ocr or --mode force to force OCR; see also help for --skip-text, --redo-ocr, and --mode)

The error points to --skip-text: skip pages with text, OCR the rest:

ocrmypdf --skip-text in.pdf out.pdf
pdftotext out.pdf out.txt

I did not run this: our OCR time cap ran out. Use out.pdf, not a sidecar: with --skip-text, --sidecar leaves out typed pages. It writes "[OCR skipped on page(s) ...]" for them.

Our Actor got 98.8%: typed pages from the text layer, OCR on the scans only.

Pick by what the PDF holds

Your PDF Use
Typed, one column pdftotext
2 columns pdf2txt.py, or pdftotext without -layout
Tables pdftotext -layout, or pdfplumber
From Python pypdfium2, which broke least here; or pdfplumber on one column
A scan ocrmypdf --sidecar
Typed pages and scans Our Actor, or ocrmypdf --skip-text (not tested here), then pdftotext

What I did not test

How I tested it
  • Test PDFs: 10 proofread public domain book pages (1,955 words) made into 9 PDFs with LibreOffice, Chromium and qpdf: the 7 above, a scan of the pages, and a mixed one, 5 typed pages then 5 scanned.
  • Score: words in order, the share of true words a tool wrote in the right order, as in the figure. The other cells count the hard spots each line above names.
  • The 8 tools: pdftotext (default, -layout and -raw), pdf2txt.py, pypdf (default and layout mode), pdfplumber, pypdfium2, PyMuPDF, Apple PDFKit (the text Preview copies), and OCRmyPDF for scans. Each ran on its own, one PDF at a time.
  • Our Actor ran as build 0.1.139's own code on this Mac, text output, 1 worker.
  • OCR ran with 1 job and one 10 minute cap for all OCR runs. On the scan, OCRmyPDF finished out.txt, then the cap ran out before out.pdf.
  • Poppler 26.03.0, pdfminer.six 20260107, pypdf 6.20.1, pdfplumber 0.11.10, pypdfium2 5.14.0, PyMuPDF 2.1, OCRmyPDF 17.13.0, Tesseract 5.5.4, macOS 26.6.
  • Scripts and results stay in our private repo, in research/bench/pdf-to-txt-2026-11-24/.
Sources

Questions people ask

How do I convert a PDF to TXT?

Run pdftotext file.pdf file.txt. It comes with Poppler: brew install poppler, or poppler-utils on Linux. If file.txt comes out empty, the PDF is likely a scan: OCR it first.

Should I use pdftotext -layout?

Use it for tables, not for columns. It kept all 20 table rows on one line, where plain pdftotext kept 0. On 2 columns it printed both side by side: 56.4% of words in order, against 98.7% without it.

How do I convert a scanned PDF to TXT?

OCR it: ocrmypdf --sidecar out.txt in.pdf out.pdf writes the text to out.txt. On our scan it found 96.0% of the words. Or send the scan to our Apify Actor, which reads each page and returns its text.

How do I get text from a PDF with some scanned pages?

Run ocrmypdf --skip-text in.pdf out.pdf, then pdftotext out.pdf out.txt. We did not test this pair. With its defaults, OCRmyPDF stops when a page already has text.