feat(docreader): support forcing PDF scanned-page OCR parsing
This commit is contained in:
@@ -115,6 +115,13 @@ MIN_CHART_REGION_AREA_RATIO = _env_float("DOCREADER_PDF_MIN_CHART_REGION_AREA",
|
||||
MAX_CHART_REGION_AREA_RATIO = _env_float("DOCREADER_PDF_MAX_CHART_REGION_AREA", 0.42)
|
||||
MAX_FIGURE_HEIGHT_RATIO = _env_float("DOCREADER_PDF_MAX_FIGURE_HEIGHT_RATIO", 0.38)
|
||||
|
||||
# --- Force scanned mode ------------------------------------------------------
|
||||
# When True, ALL PDF pages are rendered as images and routed through OCR/VLM,
|
||||
# bypassing the automatic text/scanned page classification. Useful for PDFs
|
||||
# with a low-quality or misleading text layer (web-print, scanned, image-heavy).
|
||||
# Can be overridden per-upload via parser_engine_overrides.pdf_force_scanned.
|
||||
FORCE_SCANNED_PDF = _env_bool("DOCREADER_PDF_FORCE_SCANNED", False)
|
||||
|
||||
# pdfium / Adobe text layers often emit U+FFFE for missing hyphenation or ligatures.
|
||||
_PDF_ARTIFACT_RE = re.compile(r"[\u00ad\u200b-\u200f\ufeff\ufffe\uffff]")
|
||||
_PDF_ARTIFACT_JOIN_RE = re.compile(r"(\w)[\u00ad\ufffe](\w)")
|
||||
@@ -1354,9 +1361,45 @@ class PDFParser(BaseParser):
|
||||
|
||||
Hybrid documents interleave both in reading order. On any unexpected error
|
||||
the parser falls back to rendering all pages as images (safe last resort).
|
||||
|
||||
Force-scanned mode (``pdf_force_scanned=true`` override or
|
||||
``DOCREADER_PDF_FORCE_SCANNED=true`` env) skips classification and
|
||||
renders every page as an image.
|
||||
"""
|
||||
|
||||
def __init__(self, file_name: str = "", file_type=None, **kwargs):
|
||||
# Capture per-upload override before BaseParser consumes kwargs.
|
||||
raw = kwargs.pop("pdf_force_scanned", None)
|
||||
super().__init__(file_name=file_name, file_type=file_type, **kwargs)
|
||||
# Priority: per-upload override > global env > default (False).
|
||||
if raw is not None:
|
||||
self._force_scanned = str(raw).strip().lower() in {
|
||||
"1", "true", "yes", "y", "on",
|
||||
}
|
||||
else:
|
||||
self._force_scanned = FORCE_SCANNED_PDF
|
||||
|
||||
def parse_into_text(self, content: bytes) -> Document:
|
||||
# Force-scanned short-circuit: render every page as an image.
|
||||
if self._force_scanned:
|
||||
logger.info(
|
||||
"PDFParser: force scanned mode enabled for %s",
|
||||
self.file_name,
|
||||
)
|
||||
doc = PDFScannedParser(
|
||||
file_name=self.file_name, file_type=self.file_type
|
||||
).parse_into_text(content)
|
||||
|
||||
# Align metadata fields with automatic scanned route
|
||||
page_count = doc.metadata.get("page_count", 0)
|
||||
doc.metadata.update({
|
||||
"scanned_page_count": page_count,
|
||||
"text_page_count": 0,
|
||||
"embedded_image_count": 0,
|
||||
"vector_figure_count": 0,
|
||||
})
|
||||
return doc
|
||||
|
||||
try:
|
||||
return self._route(content)
|
||||
except Exception:
|
||||
|
||||
Reference in New Issue
Block a user