HowDoesAIDoThat?

Questions / practical answer

Why is text missing when I give AI a PDF?

Check whether your PDF contains usable text, extract it page by page and add OCR for scans before sending it to AI.

By HowDoesAIDoThatSources checked 12 October 2026

Check what the AI receives

A PDF can display readable words while storing them as an image. A text extractor reads the stored text layer; OCR recognises words in scanned images. If a passage disappears before it reaches the model, changing the prompt will not bring it back.

Some AI apps also read page images. For a text-extraction route, use the original PDF and the passage the answer missed.

1. Copy a sentence from the page

Select an ordinary sentence in your PDF reader and paste it into a plain-text editor. Compare it with the page. Repeat on a table, a scanned-looking page and the passage you need.

A document can mix selectable text with image-only pages or images containing text. Even an invisible OCR layer may contain mistakes. pypdf explains the different page types.

If nothing copies, inspect the page: scans, unusual fonts and drawn letter shapes can all need a different approach.

2. Save the extracted text

Keep the original and use a separate working folder with a small document you are entitled to process. You need Python; pypdf's installation guide specifies Python 3.9 or newer. These Windows commands create an isolated environment:

python -m venv .venv
.\.venv\Scripts\python.exe -m pip install pypdf

On macOS/Linux, create it with python3 and use .venv/bin/python afterwards. See Python's environment guide.

Save this script as extract_pdf.py:

import sys
from pathlib import Path
from pypdf import PdfReader

source = Path(sys.argv[1])
reader = PdfReader(source)
if reader.is_encrypted:
    raise SystemExit("Use an authorised, unlocked copy.")

parts = []
for number, page in enumerate(reader.pages, start=1):
    text = page.extract_text() or ""
    count = len(text.strip())
    print(f"Page {number}: {count} extracted characters")
    if not text.strip():
        print("No text detected: inspect this page.")
    parts.append(f"--- Page {number} ---\n{text}")

destination = source.with_suffix(".extracted.txt")
with destination.open("x", encoding="utf-8") as output:
    output.write("\n\n".join(parts))
print(f"Saved {destination}")

Run it from the same folder, replacing the filename:

.\.venv\Scripts\python.exe extract_pdf.py "input.pdf"

It saves input.extracted.txt as UTF-8, with a marker for each page. It refuses to overwrite an existing export, so use a differently named input copy for a second attempt. The pypdf page API and Python path API cover these operations.

For an encrypted PDF, the script stops. Use a permitted unlocked export or follow the decryption instructions with the appropriate password.

3. Compare the saved text with the PDF

Open the .txt in a text editor and find the relevant page marker. Check the passage, names, dates and amounts. Pay particular attention to tables, columns and footnotes: extraction can change their order or lose the relationship between labels and values.

For layout-heavy pages, extraction_mode="layout" is another representation to try. Keep each version separately and compare it with the original. If parsing fails, keep the error and package version. Try a small document before a large one, as decompressed page content can use considerable memory.

Why identical-looking pages can behave differently

We tested two original one-page PDFs on 12 October 2026 using Python 3.12.14 and pypdf 6.10.0. They rendered identically, but their extracted text differed:

  • Text-layer PDF: 222 raw characters, including HDAI-DEMO-014 and GBP 42.00.
  • Image-only PDF: zero characters.

The script above saved the reference and amount from the text-layer sample. Its image-only export contained only the page marker. That is why checking the input is useful before changing the AI model.

4. Add OCR for scans

OCRmyPDF is a free local option. Follow its platform installation guide, including dependencies and language packs. Installing pypdf alone does not add OCR.

Once available, check the version and run this for an English document:

ocrmypdf --version
ocrmypdf --mode skip -l eng --output-type pdf input.pdf searchable.pdf

In version 17 and later, skip mode leaves pages with existing text alone. Reopen searchable.pdf, then run the extraction script on it and compare the newly recognised words with the scans.

A page with a selectable footer can still contain an image-only body. For that case, review redo mode on a copy. Avoid force mode unless you need it: it rasterises existing content and can discard structure.

For a mixed document, extract from the resulting PDF rather than relying only on an OCR text sidecar. The cookbook explains that a sidecar omits text from pages that already had text.

5. Give the model the checked passage

If the passage is missing or garbled, fix the extraction first. If it is correct, check the app's ingestion, retrieval and context limits. A fresh question with the relevant passage can help narrow the problem.

Keep the original, extracted text, OCR copy and script together. Local extraction keeps this step on your computer. A paid document service can simplify OCR or layout handling; check its export and data policy, then confirm the passages you need survived.

Sources

Primary references checked 12 October 2026.