# Handwritten PDF to text with RapidOCR, for Windows, Linux or a Mac. Tested on a Mac, Nov 2026.
# Install: pip install rapidocr onnxruntime pypdfium2
# Run: python -X utf8 pdf2text.py notes.pdf > notes.txt
import sys

import numpy as np
import pypdfium2 as pdfium
from rapidocr import RapidOCR

engine = RapidOCR()
for n, page in enumerate(pdfium.PdfDocument(sys.argv[1])):
    image = np.asarray(page.render(scale=300 / 72).to_pil().convert("RGB"))
    if n:
        print("\f")  # a form feed between pages
    print("\n".join(engine(image).txts or ()))
