LivePDF

Blog › Converting & extracting

How to extract text from a PDF (and when you can't)

· 7 min read

Every day, people try to extract text from a PDF by selecting it, copying, and pasting into a document. Sometimes that works. Sometimes it produces character soup, interleaved paragraphs, or one 40,000-character line, and nothing in the moment tells you which outcome is coming. The same goes for every other way of pulling words out — export buttons, command-line tools, scripts.

The difference is not luck. It is what is inside the file, and every method from the clipboard to the command line runs into the same physics. Here are the four routes worth knowing — how to extract the words out of any born-digital PDF, and how to tell when a file's text simply cannot be recovered — ranked from fastest to most controllable, with the failure mode of each.

Route 1: Copy and paste (fast, fragile)

For a born-digital, single-column document, copy-paste is the correct tool. Select, Ctrl+C, paste, done. Nothing faster exists, and the quality is usually fine: paragraphs arrive as paragraphs and quotes survive the trip.

It degrades quickly, though. PDF stores no line breaks; every line is a row of independently positioned glyphs, and your reader reconstructs lines from geometry before handing anything to the clipboard. Multi-column pages paste as interleaved strips. Hyphenation and ligatures leak through. And some files paste as outright garbage, for reasons worth a section of their own.

Use it for a paragraph or two from a simple document. Skip it for anything with columns, tables, or volume.

Why the clipboard sometimes hands you garbage

A PDF does not contain text the way a text file does. The content stream paints glyph IDs at coordinates using embedded font programs, and that is all rendering needs: the font turns ID 47 into the correct shape, and the page looks perfect on screen.

Copying is the reverse trip, and it needs a map back. The PDF specification (ISO 32000-1, section 9.10.2) defines the lookup order a reader must try: first the font's ToUnicode CMap, which maps character codes to Unicode values; then standard encodings resolved through glyph names; then CID-keyed font CMaps. If every method fails, the spec is blunt: "If these methods fail to produce a Unicode value, there is no way to determine what the character code represents."

So a file can render beautifully and copy as #$4½&'! because whoever produced it embedded subset fonts without ToUnicode maps. This is a documented, long-standing failure class, not an exotic one. The canonical Stack Overflow thread on garbage copy-paste has been accumulating reports for over a decade, an answer that quotes the spec section-by-section explains the mechanism, and Ghostscript tracks the issue as bug 707369, titled "In the absence of a ToUnicode CMap copy/paste from PDF files is unreliable". To diagnose a specific file, poppler's pdffonts utility lists every font with emb sub uni columns; a missing uni flag is the tell.

One related trap: ligatures. A font may draw "fi" as a single ligature glyph whose ToUnicode entry maps to U+FB01, the ligature codepoint, rather than the two letters f and i. Extraction technically succeeded, and searching the result for "file" now fails anyway.

Route 2: Your reader's export function

An export uses the same extraction machinery as the clipboard but adds line-grouping and reading-order logic, so whole-document output comes out cleaner than a paste. Most full-featured readers have one. LivePDF, for instance, exports to plain text (alongside HTML, RTF, and .docx) entirely in the browser, which matters if the document is confidential: nothing is uploaded anywhere.

Keep expectations calibrated, though. Export cannot beat the ToUnicode problem described above, because nobody can. If the font has no map back to Unicode, export delivers the same garbage with tidier spacing. Export is the right move when you want a whole document as text without the file leaving your machine. For one paragraph, copy-paste still wins.

Route 3: pdftotext on the command line

The reference tool is poppler's pdftotext, free on every platform, and the flag worth memorizing is -layout:

pdftotext -layout report.pdf report.txt

-layout tries to preserve the visual arrangement of text on the page, which keeps columns as columns and lines up tables far better than the default reading-order mode. Useful companions: -raw dumps text in content-stream order (occasionally the right answer when the geometric guess scrambles a strangely built file), and -f / -l extract a page range, so -f 5 -l 5 gets page 5 only.

The man page is also refreshingly honest about limits: "Some PDF files contain fonts whose encodings have been mangled beyond recognition. There is no way (short of OCR) to extract text from these files." When the encoding is gone, no tool will find it; switch to OCR.

For volume work, a folder of reports, a year of invoices, the CLI is the right answer. A one-line shell loop beats any amount of open-click-export-save.

Route 4: Programmatic extraction

When you need control rather than a file, use a library. In Python, pdfplumber exposes per-word coordinates, which is what you want for tables and column logic, and PyMuPDF's get_text() is fast for bulk text. Plenty of pipelines just shell out to pdftotext, which is a respectable choice: same battle-tested extractor, less glue code to maintain. This route pays off when you need per-page output, column crops, or text feeding into search and analysis, One more failure mode matters here.

Scanned PDFs: there is no text in there

A scanned page is a photograph of a page, embedded as a full-page image. It contains zero text operators. pdftotext returns an empty file, Ctrl+F finds nothing, and no amount of selecting will help, because the text you want does not exist in the file yet.

The fix is OCR, which recognizes shapes in the image and writes a new, invisible text layer over them. LivePDF runs its OCR on-device, so scans of confidential documents stay local; note that it is English-only. If you are starting from rough scans, clean them up first: deskewing and contrast fixes give the recognizer less to trip over, which is its own topic in editing scanned PDFs. The end-to-end workflow is covered in how to OCR a PDF.

One habit worth forming: after OCR, you are extracting that invisible text layer, not the visible image. Invisible layers are just one example of content a PDF carries that you cannot see. Metadata, attachments, and scripts ride along too, which matters when extracted text is headed somewhere sensitive; see what PDFs hide.

Reading order is a guess, and two columns break it

Reading order is reconstructed from position, not stored. A PDF has no paragraph objects and no stored reading order (tagged PDF can carry one, but tagging is optional and frequently absent). Every extractor reconstructs order from position: collect text fragments, group them into lines by vertical position, then read the lines top to bottom and each line left to right. Call it a y-then-x sort.

For one column of text, the guess is indistinguishable from the truth. For two columns, it is a trap. The first line of the left column and the first line of the right column sit at nearly the same height, so they merge into one output line. Page by page you get left-1, right-1, left-2, right-2: two readable columns shredded into alternating strips. Headers, footers, and footnotes land wherever their height puts them.

Knowing that order is derived from position tells you the fix also comes from position. pdftotext -layout already helps because it preserves horizontal positions instead of reflowing. The surgical version is a crop: pdftotext accepts -x -y -W -H for a crop box's origin and size, so with a page range you can extract the left half of page 12 in one pass and the right half in another, then concatenate. In code, the equivalent is filtering fragments by x-coordinate. And when whole-document order is hopeless, extract per page and assemble the output yourself, because page order is the one ordering fact the file actually stores.

Extract the text, or convert the document?

Last decision: extraction or conversion. Extraction gives you the words in approximate reading order and discards the document: fonts, columns, tables, and structure all thrown away. Conversion to Word tries to reconstruct that structure, which is a harder, lossier job with different failure modes, covered honestly in PDF to Word.

The rule of thumb: need the words for quoting, searching, or analysis → extract. Need to keep editing the document, with its formatting and real tables → convert. Extraction answers "what does it say". Conversion attempts "how was it built". Confusing the two is how people end up reformatting a thirty-page interleaved column paste at midnight.

Word count: 1429

FAQ

Why does copy-paste from a PDF give me gibberish?

The fonts in the file lack a ToUnicode map, so the reader has no reliable way to turn glyph IDs back into Unicode characters. The page still renders fine because rendering only needs the glyph IDs, not their meaning. Try pdftotext as a second opinion; if the encoding is mangled there too, OCR is the only remaining route.

How do I extract text from a scanned PDF?

You cannot extract what does not exist. A scanned page is an image of text with no text layer in the file. Run OCR first to add an invisible, searchable text layer, then copy, export, or run pdftotext on the result.

Should I extract the text or convert the PDF to Word?

Extract if you need the words for quoting, searching, or analysis. Convert if you need to keep editing the document with its structure intact. Extraction discards layout on purpose; conversion tries to rebuild it and can guess wrong.