Most "PDF to text" pipelines make one of two mistakes: they OCR every page (slow, and worse than the text the PDF already contains), or they only read the text layer (and return nothing for scans). Mixed Chinese/English/Japanese pages add a third: Latin-only OCR models turn CJK into noise.

This is the routing I ended up with for a small open-model OCR tool. All numbers below are from real runs.

1. Read the text layer first, OCR only the pages that need it

pypdfium2 opens each page and returns its text layer in a few milliseconds. If a page has enough real characters, that text is the answer: it is exact, and on a born-digital arXiv paper it took 0.03 s per page. Only pages with no usable text layer (scans, photos saved as PDF) are rendered to an image and recognised.

import pypdfium2 as pdfium pdf = pdfium.PdfDocument(path) for i, page in enumerate(pdf): text = page.get_textpage().get_text_range().strip() if len(text) >= 20: # real text layer: use it yield i + 1, "text-layer", text else: # scan: render and OCR img = page.render(scale=2.0).to_numpy() # BGR is what the OCR model expects yield i + 1, "ocr", ocr(img) 

On a scanned 2-page PDF, recognition took 3.6 s per page on half a CPU core. That 100x gap is why the text-layer check matters.

2. One model for CJK + Latin, separate models for other scripts

PaddleOCR's PP-OCRv6 small model (run through RapidOCR / ONNX Runtime) reads Simplified and Traditional Chinese, Japanese, English and 45+ Latin-script languages in the same line. A Hong Kong receipt like this:

星光茶餐廳 Starlight Café 凍檸茶 Iced Lemon Tea HK$ 24.00 叉烧飯 BBQ Pork Rice HK$ 52.00 抹茶ラテMatcha Latte HK$ 36.00 

came back in reading order with an average line confidence of 0.974. Two honest flaws in that output: 叉燒飯 came out with the simplified 烧, and the space before "Matcha" was dropped. Korean, Cyrillic, Arabic, Devanagari, Thai and Greek need their own PP-OCRv5 models, so the script is a parameter instead of a guess.

3. Three things that cost me an hour each

  • Lazy model loading. RapidOCR loads models on the first call. Warm it up at build time with an image that contains text; a blank image skips the recognition model and the first real request pays for it.
  • Channel order. Feeding RGB instead of BGR silently lost a red line of text. Convert once, at the edge.
  • Right-to-left scripts need the rapidocr[rtl] extra, or Arabic comes back reversed.

4. Cost

On Apify's platform the compute for one recognised page is about USD 0.0002–0.0004. Text-layer pages are almost free. Files never leave the run: no outside OCR or AI API sees them.

Try it

I packaged this as an Apify Actor: Image & PDF to Text OCR. The form is pre-filled with the receipt above (one page, USD 0.003); Google Drive, Dropbox and OneDrive links work. It also runs as a tool for AI agents through the Apify MCP server.

If you only need the routing idea, the snippet above is the whole trick: text layer first, OCR the rest, pick the model by script.

Disclosure: I built the tool. This article was drafted with AI assistance; every number comes from logged runs.