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.
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
-
By default pdftotext undoes columns and hyphens to give reading
order, its manual says.
-layoutkeeps the page layout. Our 14 line-end hyphens (U+2010, from Chromium) stayed split either way. -
pdf2txt.py comes with pdfminer.six:
pip install pdfminer.six. -
One page only, here page 3:
pdftotext -f 3 -l 3 file.pdf file.txt.
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
| 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".
- Scanned pages: $0.006 each. Digital pages: free. Plus a $0.00005 start fee a run at the default 1 GB.
- On Apify's free plan, it stops at 20 pages a run and 50 a month.
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
- Windows and Linux: everything ran on one Mac.
- Real scanner files and real papers: I made all 9 test PDFs.
- Apache Tika, Adobe Acrobat and online converters.
- Password protected PDFs, forms, and languages other than English.
- Text that comes out as gibberish or (cid:x): PDF text gibberish.
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
-
Poppler
(pdftotext), and its manual page for
-layout - pdfminer.six (pdf2txt.py)
- pypdf docs
- pdfplumber
- OCRmyPDF docs
- We make the Apify Actor, and none of the other tools here.
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.
Related guides
- Scanned PDF to text: 3 free ways, tested (2026)
- OCRmyPDF output to text: --sidecar, tested
- pypdf extract text is empty: it is a scan (tested, 2026)
- pdfplumber extract_tables on tables without lines, tested
- Count words in a PDF: 7 tools tested, why a scan says 0
- OCRmyPDF "page already has text": which flag to use