How to extract text from a PDF (and when you can't)
Whether you can extract text from a PDF at all depends on one thing: does the file have a text layer? A PDF exported from Word, Google Docs, LaTeX, or a browser contains real, selectable text, and pulling it out is fast and accurate. A PDF that is a scan or photo of a page contains only an image, no text, and plain extraction returns nothing useful; recovering that text needs OCR (optical character recognition), a fundamentally different and error-prone process. The quick test: open the PDF and try to select text with your cursor. If you can highlight words, you have a text layer and PDFMoka's PDF to text converter will extract it in your browser without uploading the file. If you cannot select anything, it is a scan and you need OCR (which PDFMoka does not do). Here is the full picture.
The one test that tells you which situation you're in
Before trying any tool, do this: open the PDF in any viewer and try to select text with your mouse.
- Text highlights and you can copy it → the PDF has a text layer. Extraction will work well.
- Nothing selects, or you can only draw a box over an image → the page is an image. Plain extraction will return empty or garbage; you need OCR.
This 5-second check saves you from trying extractor after extractor on a file that fundamentally has no text to extract.
When there's a text layer: extraction is easy
For digitally-created PDFs, the text is already inside the file as character data, tools just read it out. Options:
-
Browser tool, no upload: drop the file into PDFMoka's PDF to text converter. It uses the PDF.js engine to read the text layer entirely in your browser, so a confidential document is never sent to a server.
-
Command line (
pdftotext): part of the poppler-utils package, fast and reliable for text-layer PDFs.# macOS: brew install poppler ; Debian/Ubuntu: sudo apt-get install poppler-utils pdftotext input.pdf output.txt -
Python (
pypdf) for scripting:from pypdf import PdfReader reader = PdfReader("input.pdf") text = "\n".join(page.extract_text() or "" for page in reader.pages)
verify against current docs:
pdftotext(poppler) andpypdfare current and their basic invocations are stable, but confirm the install command and API on your system before relying on them.pypdfis the maintained successor to the olderPyPDF2, if you see PyPDF2 in old tutorials, prefer pypdf.
When it's a scan: why plain extraction fails, and what OCR involves
A scanned PDF is a picture of text. There are no characters in the file, only pixels, so a text extractor has nothing to read and returns an empty string. The only way to get text is OCR, which analyzes the image and guesses the characters from their shapes.
Be realistic about OCR before you reach for it:
- It is not perfect. Accuracy depends on scan quality, resolution (aim for 300 DPI or higher), fonts, and languages. Expect errors, especially on low-quality scans, handwriting, or unusual fonts.
- It is slower and heavier than text-layer extraction (seconds per page vs milliseconds).
- Common OCR options: the open-source Tesseract engine (local, free), or cloud services like AWS Textract and Google Document AI for higher accuracy on complex documents.
PDFMoka's converter reads the text layer only; it does not perform OCR. So for a scanned document, it will not help, and we would rather tell you that up front than return an empty file. Use a Tesseract-based tool or a cloud OCR service for scans.
a sneaky edge case worth knowing: some scanners embed a hidden, low-quality OCR text layer underneath the scanned image. Such a file looks like it has selectable text, but the text is often garbled. If extraction gives you nonsense from a document that is visibly a scan, that hidden layer is why, and you are better off re-running proper OCR on the images.
The other honest limitation: layout
Even with a clean text layer, extraction is not magic on complex pages. Plain text extractors read characters in roughly their stored order, which means multi-column layouts, tables, and sidebars often come out scrambled, two columns can interleave line-by-line, and table cells can flatten into a run-on line. This is not a bug in any one tool; it is inherent to pulling linear text out of a 2D layout. For a simple report or article the output is clean; for a multi-column academic paper or a table-heavy document, expect to do some cleanup, or use a layout-aware tool if structure matters.
Quick decision guide
- Can you select the text? → Yes: extract it in your browser, or use
pdftotext/pypdf. - Can't select anything (it's a scan)? → You need OCR (Tesseract or a cloud service); a text-layer extractor won't work.
- Text is there but comes out scrambled? → Multi-column or table layout; use a layout-aware extractor or clean up manually.
- Need the reverse (data into a PDF)? → See CSV to PDF or text to PDF.
If your specific goal is pulling tabular data out, see convert a PDF table to CSV, which has the same text-layer-only caveat.
References
- Mozilla PDF.js (
getTextContenttext extraction): https://mozilla.github.io/pdf.js/ - poppler
pdftotext(utility for text-layer extraction): https://poppler.freedesktop.org/ - pypdf documentation (extract_text): https://pypdf.readthedocs.io/en/stable/