Extract All Text From a PDF and Keep the Reading Order
Copy-paste from a PDF gives broken lines, jumbled columns or strange characters. Here is how to extract text from a PDF properly — with pdftotext -layout, OCR for scans, and a browser-based route when you just need the words.
You select a paragraph in a PDF, hit Ctrl+C, paste — and get a line break after every line, two columns zipped together into nonsense, or words like first and équipe. The PDF isn't broken. It simply never stored a paragraph; it stored glyphs at coordinates. Anything that looks like reading order has to be reconstructed by the tool you use.
This guide shows you how to extract text from a PDF reliably: how to tell which kind of PDF you have, which pdftotext flag preserves columns, when you need OCR instead, and how to kill encoding junk. Both the command line and the no-install browser route are covered.
Step 1: Is your PDF selectable or scanned?
Everything downstream depends on this. Open the file and try to select a sentence with your mouse.
- Text appears highlighted word by word → the PDF has a real text layer. Extraction is a parsing job and will be near-perfect.
- You get a blue rectangle over the whole page, or nothing → it's a scan: a photo of a page wrapped in PDF. There is no text to extract yet. You need OCR first.
- Some pages select, some don't → a mixed file, typical of a thesis with scanned appendices or a signed contract. Run OCR on the whole document; good OCR tools skip pages that already have text.
A quick size check helps too. A 40-page text PDF is often 300–900 KB. A 40-page scan at 300 dpi is usually 8–60 MB. If the file is suspiciously heavy for its page count, assume images.
Why columns and lines come out jumbled
Inside a PDF, text is a sequence of drawing instructions: place this glyph run at x=72, y=634 in this font. The order of those instructions follows how the layout engine emitted them, not how a human reads. On a two-column academic paper, a naive extractor walks the content stream and can interleave a line from the left column with a line from the right, giving you sentences that switch topic mid-clause.
Hard line breaks are the second symptom. Each visual line is its own text object, so every line ends with a newline and justified paragraphs arrive as ragged 60-character fragments — painful if you're feeding the output to a word counter, a diff or an LLM.
Good extractors fix this by clustering glyphs into lines, lines into blocks, and blocks into a reading order based on geometry. That is exactly what the -layout flag turns on.
Hands-on: pdftotext -layout vs raw
pdftotext ships with Poppler (brew install poppler, apt install poppler-utils, or the Xpdf tools on Windows). The baseline command:
pdftotext -layout annual-report-2023.pdf annual-report-2023.txt-layout reproduces the physical page with spaces, so columns stay side by side and tables stay aligned — ideal for reading, grepping and anything table-shaped. Compare the three modes on the same page:
-layout— preserves horizontal position. Two columns remain two columns, separated by runs of spaces. Best default for papers and reports.-raw— emits text in content-stream order, no geometry. Fast, and occasionally the only mode that gets a weird generated PDF right.- No flag — Poppler's own reading-order heuristic: one column of flowing text, columns merged in reading sequence. Best when you want prose to feed into another tool.
Useful extras you'll reach for constantly:
# pages 3–12 only, forced UTF-8 output
pdftotext -layout -f 3 -l 12 -enc UTF-8 thesis.pdf chapter2.txt
# pipe straight into grep, no temp file
pdftotext -layout invoice-0042.pdf - | grep -i "total due"
# keep page breaks out of the output
pdftotext -layout -nopgbrk minutes.pdf minutes.txtOpen the result and skim the first page. If columns are interleaved, drop -layout. If words are glued together, add it back. Thirty seconds of comparison beats an hour of cleanup.
The browser route: no install, no command line
If you're on a locked-down laptop, on a phone, or you just need one file done now, use the PDF to TXT converter: drop the PDF in, get a plain .txt back with the layout-preserving extraction already applied. It's the same reconstruction logic, without Homebrew or apt.
It's worth keeping bookmarked for the one-off cases: a 2 MB conference paper you want to quote, a scanned-free government form, a 300-page report you need to search. The wider document converter hub handles the neighbouring jobs — DOCX, EPUB, images — when TXT isn't the destination you need.
Scanned PDFs: add a text layer with OCR
No extractor can read pixels. For scans, run OCR first — ocrmypdf is the practical choice because it keeps the original page images and adds an invisible text layer underneath:
ocrmypdf --language eng --deskew --rotate-pages scan-1987.pdf scan-1987-ocr.pdf
pdftotext -layout scan-1987-ocr.pdf scan-1987.txtTips that matter for accuracy: scan or export at 300 dpi (150 dpi loses small type, 600 dpi mostly wastes time), use --deskew for photographed or crooked pages, and pass the right language pack — --language deu+eng for a German paper with English references. OCR output always needs a proofread pass; expect confusions like rn → m and 0 → O in older print.
Fixing garbled characters and encoding junk
If the text came out but looks wrong, the cause is almost always one of four things:
- Ligatures:
fi,fl,ffarrive as single glyphs, sofindbecomesfindand plain-text search misses it. A quicksedor a find-and-replace fixes the five common ones. - Mojibake (
é,—): UTF-8 bytes read as Latin-1. Force the encoding withpdftotext -enc UTF-8and open the file as UTF-8 in your editor, not in a legacy spreadsheet import. - Missing or broken ToUnicode map: subset-embedded fonts with no character map give you boxes, blanks or random letters. Nothing can parse that — OCR the pages instead, e.g.
ocrmypdf --force-ocr --redo-ocr-style reprocessing. - Hyphenation: justified text splits words across lines as
inter-\nnational. Join them with a regex likes/-\n//gafter you're sure the hyphen isn't a real compound.
A 60-second decision path
- Can you select the text? No →
ocrmypdffirst, then extract. - One file, no tooling installed? → use the online pdf to txt converter and move on.
- Columns or tables that must stay aligned? →
pdftotext -layout. - Prose you'll feed into another program? →
pdftotextwith no flag, then strip hyphenation. - Weird characters in the output? → add
-enc UTF-8; if glyphs are still broken, the font map is unusable — OCR it. - Batch of 200 files? → loop
pdftotext -layoutin a shell script and spot-check five results.
Extraction is never fully automatic, but with the right mode chosen in the first minute, cleanup usually drops from hours to a couple of regexes.
FAQ
Why does copied PDF text have a line break after every line? Each visual line is a separate text object in the file. Extract with a tool that reflows paragraphs, or remove single newlines while keeping double ones.
How do I extract text from a scanned PDF? Run OCR to create a text layer (ocrmypdf --language eng input.pdf output.pdf), then extract from the OCR'd file.
Does extracting text keep images and tables? No. TXT is plain characters only. -layout keeps table alignment with spaces, but you lose borders, merged cells and all images.
Can I extract text from a password-protected PDF? Only if you can open it. With the user password you can supply it to the tool (pdftotext -upw yourpassword file.pdf); without it, extraction is not possible.
TXT, Markdown or DOCX? Choose TXT for search, grep, diffs and LLM input; DOCX when you need to keep editing formatted content. Convert once to the format you'll actually work in.
