docling icon indicating copy to clipboard operation
docling copied to clipboard

Scanned PDF text turns into meaningless letters

Open Yuanyi1104 opened this issue 5 months ago • 8 comments

Bug

When using docling to parse scanned PDFs, it was found that some paragraphs turned into meaningless letters

Steps to reproduce

The document parsing engine I set up in OpenWebUI is Docling. When interacting with the large language model, I uploaded a scanned PDF, and after parsing, I found that some paragraphs contained meaningless strings of letters like 'n sns s s a n'. The original text of my file is as follows:

Image The result after docling parsing is as follows. Image

Version

docling​ 2.63.0 Python​ 3.12

Yuanyi1104 avatar Jun 09 '26 09:06 Yuanyi1104

This is a known problem area in Docling, particularly with scanned PDFs containing Chinese text. The garbled output like "n sns s s a n" typically happens because either:

  1. The PDF has a corrupted/non-standard text layer that Docling extracts directly instead of running OCR, or
  2. The OCR engine isn't configured with the correct language for Chinese.

Here's what to try:

1. Enable force_full_page_ocr — this forces Docling to OCR the entire page and ignore any embedded (potentially garbled) text layer:

from docling.document_converter import DocumentConverter, PdfFormatOption
from docling.datamodel.pipeline_options import PdfPipelineOptions, EasyOcrOptions
from docling.datamodel.base_models import InputFormat

pipeline_options = PdfPipelineOptions(
    do_ocr=True,
    ocr_options=EasyOcrOptions(
        lang=["ch_sim", "en"],
        force_full_page_ocr=True,
        use_gpu=True
    )
)

converter = DocumentConverter(
    format_options={InputFormat.PDF: PdfFormatOption(pipeline_options=pipeline_options)}
)
result = converter.convert("your_document.pdf")

2. Consider Tesseract as an alternative — some users report better results with Tesseract for CJK text [1]:

from docling.datamodel.pipeline_options import TesseractCliOcrOptions

pipeline_options = PdfPipelineOptions(
    do_ocr=True,
    ocr_options=TesseractCliOcrOptions(
        lang=["chi_sim", "eng"],
        force_full_page_ocr=True
    )
)

(Requires chi_sim.traineddata to be installed for Tesseract.)

Why this happens: Docling uses a bitmap coverage threshold to decide whether to run OCR. If the scanned PDF has an embedded (but corrupt) text layer, Docling may extract that garbage text instead of running OCR [2] [3]. Setting force_full_page_ocr=True bypasses this and processes pages purely via OCR.

Since you're using OpenWebUI with Docling as a parsing engine, you may need to check whether OpenWebUI allows you to configure these pipeline options. If not, you might need to run Docling directly or via docling-serve with custom configuration.

Related issues: #748, #2963, #3451.

To reply, just mention @dosu.


Docs are dead. Just use Dosu.

Leave Feedback Ask Dosu about docling Share Dosu with your team

dosubot[bot] avatar Jun 09 '26 09:06 dosubot[bot]

@dosu The document I uploaded is an English scan, and OpenWebUI provides settings to pass parameters to docling-serve. My parameters are as follows: { "table_mode": "accurate", "ocr_engine": "rapidocr", "do_ocr": true, "ocr_lang": "english, chinese", "enrich_picture_classes": "true", "pdf_backend": "dlparse_v4", "device": "cuda" }

Yuanyi1104 avatar Jun 09 '26 09:06 Yuanyi1104

Thanks for sharing your configuration. The issue is very likely caused by RapidOCR's ocr_lang parameter being non-functional in Docling — it's a confirmed bug. Regardless of what you pass in ocr_lang, RapidOCR always loads the default Chinese PP-OCRv4 models [1]. This means your English scanned PDF is being processed by Chinese recognition models, which produces the meaningless letter output you're seeing.

Additionally, RapidOCR has a known word-merging issue with English/Latin text when used through Docling's pipeline, where spaces between words are removed [2].

Recommended fix — switch to EasyOCR or Tesseract:

{
  "table_mode": "accurate",
  "ocr_engine": "easyocr",
  "do_ocr": true,
  "ocr_lang": ["en"],
  "pdf_backend": "dlparse_v4",
  "device": "cuda"
}

Or if you have Tesseract installed:

{
  "table_mode": "accurate",
  "ocr_engine": "tesseract",
  "do_ocr": true,
  "ocr_lang": ["eng"],
  "pdf_backend": "dlparse_v4",
  "device": "cuda"
}

You may also want to enable force_full_page_ocr since your document is a scan — this ensures OCR processes the entire page rather than relying on any embedded (potentially corrupt) text layer. In docling-serve, this needs to be passed via ocr_custom_config [3]:

{
  "ocr_custom_config": {
    "force_full_page_ocr": true
  }
}

In summary: The root cause is that RapidOCR in Docling ignores the language setting and always uses Chinese models. Switching to EasyOCR or Tesseract with the correct English language code should resolve the garbled text.

To reply, just mention @dosu.


Share context across your team and agents. Try Dosu.

Leave Feedback Learn about docling with Dosu Add Dosu to your team

dosubot[bot] avatar Jun 09 '26 09:06 dosubot[bot]

@dosu If I want to switch to easyocr, but I also want Chinese recognition, or recognition in other languages like Japanese or Korean, how should I set my parameters?

Yuanyi1104 avatar Jun 09 '26 10:06 Yuanyi1104

EasyOCR uses ISO 639-1 two-letter language codes and supports 80+ languages [1]. Here's how to configure your OpenWebUI parameters for different language combinations:

English + Chinese:

{
  "table_mode": "accurate",
  "ocr_engine": "easyocr",
  "do_ocr": true,
  "ocr_lang": ["en", "ch_sim"],
  "pdf_backend": "dlparse_v4",
  "device": "cuda"
}

English + Japanese:

{
  "ocr_lang": ["en", "ja"]
}

English + Korean:

{
  "ocr_lang": ["en", "ko"]
}

English + Chinese + Japanese + Korean:

{
  "ocr_lang": ["en", "ch_sim", "ja", "ko"]
}

The ocr_lang parameter in docling-serve accepts either a list format (["en", "ch_sim"]) or a comma-separated string ("en, ch_sim") — both are parsed correctly [2].

A few notes:

  • EasyOCR performs much better with GPU enabled (~9s/page vs ~70s/page on CPU), so your "device": "cuda" setting is good [3].
  • You may also want to add "force_full_page_ocr": true (or pass it via ocr_custom_config) since your document is a scan — this ensures the entire page is OCR'd rather than relying on any embedded text layer.
  • The more languages you add, the slower and potentially less accurate OCR becomes, so only include languages you actually need.

To reply, just mention @dosu.


Docs are dead. Just use Dosu.

Leave Feedback Ask Dosu about docling Share Dosu with your team

dosubot[bot] avatar Jun 09 '26 10:06 dosubot[bot]

Hi @Yuanyi1104 , I’d like to investigate this issue.

I’ll try to reproduce the garbled OCR output with RapidOCR and compare it against EasyOCR/Tesseract using the same scanned English PDF. I’ll also check whether ocr_lang and force_full_page_ocr are correctly passed through docling-serve.

If I find a clear root cause in Docling or docling-serve, I’ll share the result before opening a PR.

sriharan0804 avatar Jun 16 '26 16:06 sriharan0804

This looks like RapidOCR's direction classifier. It rotates some long upright lines by 180° before recognition, and they get read upside down:

use_cls=False: 'message fatigue and (mis)perceptions that messages were only for'
default      : 'm  ms ss s smss  mng mssnr'

The classifier Docling selects (OCRVersion.PPOCRV4 in rapid_ocr_model.py, the old v2.0 model) gets every line squeezed to 192x48 px. A full-width line of body text is about 30 times wider than high, so the verdict on it is close to random. Cause, numbers and a fix for that model are in the issue I wrote RapidAI/RapidOCR#744.

In Docling 2.132.0, on four pages of a CC BY article rasterised to an image-only PDF (216 dpi), with the Markdown scored against the text layer of the original pages:

RapidOcrOptions words found in the Markdown
default 90.7 % (200 missed)
use_cls=False 94.2 % (125 missed)
rapidocr_params={"Cls.ocr_version": OCRVersion.PPOCRV5} (onnxruntime) 94.2 % (125 missed)

The first two rows are the same on the torch backend.

Two workarounds today:

  • RapidOcrOptions(use_cls=False) when the pages are upright (the advice given in RapidAI/RapidOCR#409).
  • RapidOcrOptions(rapidocr_params={"Cls.ocr_version": OCRVersion.PPOCRV5}) on onnxruntime, openvino or paddle. The newer classifier does not have the problem and still catches upside-down lines. RapidOCR has no such model for torch.

Docling could select the PP-OCRv5 classifier itself on those backends. The version is hard-coded in two places, _rapidocr_artifacts() and RapidOcrModel.__init__, and the artifacts_path flow would also have to pass Cls.ocr_version, because RapidOCR derives the classifier input shape from it. I can open a PR if that sounds right.

@Yuanyi1104 does use_cls=False remove the garbage on your scan?

Reproducer (pip install docling onnxruntime)
import re
import urllib.request
from collections import Counter
from pathlib import Path

import pypdfium2 as pdfium
from docling.datamodel.base_models import InputFormat
from docling.datamodel.pipeline_options import PdfPipelineOptions, RapidOcrOptions
from docling.document_converter import DocumentConverter, PdfFormatOption
from rapidocr.utils.typings import OCRVersion

# CC BY article from the PMC Open Access subset (doi:10.1080/17441692.2021.1923776)
URL = "https://pmc-oa-opendata.s3.amazonaws.com/PMC9487863.1/PMC9487863.1.pdf"
WORD = re.compile(r"\w+")

source = Path("PMC9487863.pdf")
if not source.exists():
    urllib.request.urlretrieve(URL, source)
pdf = pdfium.PdfDocument(source)
reference, images = Counter(), []
for index in range(4):
    text = re.sub(chr(0xFFFE) + r"\s*", "", pdf[index].get_textpage().get_text_range())  # U+FFFE: line-end hyphen
    reference.update(w.lower() for w in WORD.findall(text))
    images.append(pdf[index].render(scale=3).to_pil().convert("RGB"))
scan = Path("PMC9487863_image_only.pdf")
images[0].save(scan, save_all=True, append_images=images[1:], resolution=216)

RUNS = {
    "default": RapidOcrOptions(),
    "use_cls=False": RapidOcrOptions(use_cls=False),
    "Cls.ocr_version=PP-OCRv5": RapidOcrOptions(rapidocr_params={"Cls.ocr_version": OCRVersion.PPOCRV5}),
}
n_ref = sum(reference.values())
print("| RapidOcrOptions | words of the original found in the Markdown |")
print("|---|---:|")
for name, ocr_options in RUNS.items():
    options = PdfPipelineOptions()
    options.ocr_options = ocr_options
    converter = DocumentConverter(format_options={InputFormat.PDF: PdfFormatOption(pipeline_options=options)})
    markdown = converter.convert(scan).document.export_to_markdown()
    Path(f"out_{name.split('=')[-1]}.md").write_text(markdown, encoding="utf-8")
    found = sum((reference & Counter(w.lower() for w in WORD.findall(markdown))).values())
    print(f"| {name} | {found / n_ref:.1%} ({n_ref - found} of {n_ref} missed) |", flush=True)
| RapidOcrOptions | words of the original found in the Markdown |
|---|---:|
| default | 90.7% (200 of 2142 missed) |
| use_cls=False | 94.2% (125 of 2142 missed) |
| Cls.ocr_version=PP-OCRv5 | 94.2% (125 of 2142 missed) |

The 125 words missed in the best case are mostly running headers, the copyright footer and the author block of the first page.

theofarouk avatar Oct 01 '26 21:10 theofarouk

Classification labels added: priority:medium, pdf.

These labels are advisory. Maintainers can correct them. The workflow preserves these corrections.

github-actions[bot] avatar Nov 16 '26 15:11 github-actions[bot]