|
|
@@ -4,6 +4,7 @@
|
|
|
|
|
|
import json
|
|
|
import os
|
|
|
+import shutil
|
|
|
import subprocess
|
|
|
import sys
|
|
|
import tempfile
|
|
|
@@ -1637,9 +1638,24 @@ def fallback_parse_with_paddleocr(
|
|
|
else:
|
|
|
logger.warning("[PaddleOCR备用] 从PDF提取图片失败(可能是PDF文件损坏或缺少必要的库)")
|
|
|
elif file_type in ['png', 'jpeg', 'jpg']:
|
|
|
- # 文件是图片,直接使用
|
|
|
+ # 文件内容是图片,但路径可能为 .pdf(上游保存时扩展名错误),
|
|
|
+ # 若直接传给 doc_parser 会按扩展名用 PDFium 打开导致 Data format error,
|
|
|
+ # 故复制为正确扩展名的临时文件再调用。
|
|
|
image_path = input_file
|
|
|
- logger.info(f"[PaddleOCR备用] 检测到图片文件({file_type}): {image_path}")
|
|
|
+ ext = os.path.splitext(input_file)[1].lower()
|
|
|
+ if ext == ".pdf" or ext not in (".png", ".jpg", ".jpeg"):
|
|
|
+ dest_dir = output_dir or os.path.dirname(input_file)
|
|
|
+ safe_ext = ".png" if file_type == "png" else (".jpg" if file_type in ("jpeg", "jpg") else ".png")
|
|
|
+ tmp_image = os.path.join(dest_dir, f"paddleocr_fallback_input_{int(time.time() * 1000)}{safe_ext}")
|
|
|
+ try:
|
|
|
+ os.makedirs(dest_dir, exist_ok=True)
|
|
|
+ shutil.copy2(input_file, tmp_image)
|
|
|
+ image_path = tmp_image
|
|
|
+ logger.info(f"[PaddleOCR备用] 检测到图片文件({file_type})但扩展名为 {ext},已复制为 {image_path} 再解析")
|
|
|
+ except Exception as e:
|
|
|
+ logger.warning(f"[PaddleOCR备用] 复制图片到临时文件失败,仍用原路径: {e}")
|
|
|
+ else:
|
|
|
+ logger.info(f"[PaddleOCR备用] 检测到图片文件({file_type}): {image_path}")
|
|
|
else:
|
|
|
# 文件类型未知,尝试按PDF处理(可能是PDF但没有正确识别)
|
|
|
logger.debug(f"[PaddleOCR备用] input_file类型未知({file_type}),尝试按PDF处理: {input_file}")
|