Tools,

Scanned PDF to JSON: there is no text yet

0

words in PyMuPDF's JSON of our scan

Get the script

A scan gives you empty JSON

A scan is a picture. Parsers read the text layer, and a scan has none.

Our 10-page scan, as is
Call Words JSON
PyMuPDF get_text("json") 0 8.7 MB
The same, text flags only 0 0.7 KB
pdfplumber extract_words() 0 []

The 8.7 MB is the 10 page pictures, as base64. Check each page first: if page.get_text().strip() is empty, it needs OCR.

The script, part 1: OCR words

brew install tesseract
pip install pymupdf pytesseract
"""Scanned PDF to JSON: each page's text, and each word with its box."""

import json
import sys

import pymupdf
import pytesseract
from PIL import Image


def ocr_words(page, dpi=300):
    pix = page.get_pixmap(dpi=dpi)
    img = Image.frombytes("RGB", (pix.width, pix.height), pix.samples)
    d = pytesseract.image_to_data(img, output_type=pytesseract.Output.DICT)
    k = 72 / dpi  # pixels to PDF points
    for i, text in enumerate(d["text"]):
        if text.strip():  # skip page, block, paragraph and line rows
            x, y, w, h = d["left"][i], d["top"][i], d["width"][i], d["height"][i]
            yield {
                "text": text,
                "conf": float(d["conf"][i]),
                "box": [round(v * k, 1) for v in (x, y, x + w, y + h)],
                "line": [d["block_num"][i], d["par_num"][i], d["line_num"][i]],
            }

Part 2: one JSON file

Pages with a text layer skip OCR. Or download scan_to_json.py.

def page_entry(page):
    if page.get_text().strip():  # a text layer: read it, no OCR
        source = "text layer"
        words = [
            {"text": w[4], "box": [round(v, 1) for v in w[:4]], "line": [w[5], w[6]]}
            for w in page.get_text("words")
        ]
    else:
        source, words = "ocr", list(ocr_words(page))
    lines = {}
    for w in words:
        lines.setdefault(tuple(w["line"]), []).append(w["text"])
    text = "\n".join(" ".join(ws) for ws in lines.values())
    return {"page": page.number + 1, "source": source, "text": text, "words": words}


with pymupdf.open(sys.argv[1]) as doc:
    pages = [page_entry(p) for p in doc]
with open(sys.argv[2], "w", encoding="utf-8") as f:
    json.dump({"file": sys.argv[1], "pages": pages}, f, ensure_ascii=False, indent=1)

1,876 words, with boxes

python scan_to_json.py scan.pdf scan.json
scan_to_json.py, words found of 1,955
Input Words Time
The scan (OCR) 1,876 46 s
Same pages with a text layer 1,876 1 s

The first word of page 1, in scan.json:

{"text": "86", "conf": 96.0, "box": [54.0, 91.7, 70.6, 104.2], "line": [1, 1, 1]}

Seven paths, about the same words

Words found of 1,955, and time
Path Words Time
Our script 1,876 46 s
PyMuPDF's own OCR 1,873 15 s
Tesseract tsv, to JSON 1,877 36 s
OCRmyPDF, then PyMuPDF 1,876 37 s
OCRmyPDF, then pdfplumber 1,867 38 s
Docling export_to_dict() 1,854 53 s
Our Actor's code 1,877 45 s

The spread is 23 words. Pick a path by what its JSON holds, not by the score.

What each JSON holds

Fields and size for our 10 pages
Path Holds Size
Our script Words, boxes, confidence 370 KB
PyMuPDF's own OCR Lines, spans, boxes 1.8 MB
Tesseract tsv Words, boxes, confidence, empty rows 480 KB
pdfplumber Words, boxes 476 KB
Docling Paragraphs, labels, boxes 47 KB
Our Actor Page text, page confidence 24 KB

Tesseract has no JSON output

tesseract page.jpg out json

This exits with code 0, prints "read_params_file: Can't open json" and writes plain text to out.txt. Ask for tsv instead, or call pytesseract:

d = pytesseract.image_to_data(img, output_type=pytesseract.Output.DICT)

On our page 1 the dict had 245 rows. Only 212 had text. The others are rows for the page, blocks, paragraphs and lines, with conf -1. Skip rows with no text.

Two more traps

Docling picked its OCR engine itself: RapidOCR, since ocrmac was not installed. Most text items had one box. Paragraphs that ran across pages had more.

Same 10 pages, one scorer

The scan is 10 proofread pages from two public domain books on Wikisource: 1,955 words. The score is true words found in order, in the text read back out of each JSON.

How we ran it
  • A Mac with Apple M5 Max, Python 3.13.11, Tesseract 5.5.4 from Homebrew.
  • PyMuPDF 2.1, pdfplumber 0.11.10, pytesseract 0.3.13, Docling 2.150.1, OCRmyPDF 17.14.1.
  • scan.pdf holds the 10 JPEGs at 300 dpi, 1920 pixels wide. Tesseract alone on them: 1,878 words in 38 s.
  • OCRmyPDF ran with --jobs 2 and took 37 s. PyMuPDF and pdfplumber read its PDF in under 1 s.
  • PyMuPDF's own OCR: get_textpage_ocr(full=True, dpi=300), then get_text("json"). It used about one core: 14.6 s of CPU in 15.2 s.
  • Docling ran with its defaults: layout on the Mac GPU, OCR by RapidOCR.
  • Times: one run at a time, at low priority, while other jobs ran. Tesseract runs had a 2 thread limit. Docling used its default 4 threads.
  • Licenses: PyMuPDF AGPL-3.0, pdfplumber and Docling MIT, pytesseract and Tesseract Apache-2.0, OCRmyPDF MPL-2.0. Our Actor ships Tesseract, pytesseract and pdfplumber, not PyMuPDF or Docling.

Scanned pages to JSON, no install

Run it yourself. Our Apify Actor returns one JSON item per file: each page's text and, for OCR pages, a mean confidence. $0.006 a scanned page. Its code found 1,877 of these 1,955 words.

Try the Actor

Or send us the scan. Tack, our AI, does the job for $29, up to 50 pages. First page free.

Get one page free

Questions people ask

Why is the JSON of my scanned PDF empty?

A scanned page is a picture with no text layer. PyMuPDF and pdfplumber read text layers, so they find nothing. Run OCR first.

Can Tesseract output JSON?

No. Tesseract 5.5.4 has no json output. Use tsv, or pytesseract's image_to_data with Output.DICT, then json.dump.

Why is PyMuPDF's JSON so big?

get_text("json") puts each image in the JSON as base64. Pass flags=pymupdf.TEXTFLAGS_TEXT to leave the images out.

Does Docling's JSON have word boxes?

Not in our run. Its 67 text items were paragraphs, headers, footers and list items. 64 had one box, measured from the bottom of the page.

Which path was fastest?

PyMuPDF's own OCR: 15 s for our 10 pages. Its JSON has boxes but no word confidence.