Skip to content

Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.

fix: improve PDF page import handling to skip failed pages and log warnings

myhloli0cfc6c3d4ed5
diff --git a/mineru/cli/common.py b/mineru/cli/common.py
index 445003f2..27dcbad5 100644
--- a/mineru/cli/common.py
+++ b/mineru/cli/common.py
@@ -51,11 +51,16 @@ def convert_pdf_bytes_to_bytes_by_pypdfium2(pdf_bytes, start_page_id=0, end_page
try:
end_page_id = get_end_page_id(end_page_id, len(pdf))
- # 选择要导入的页面索引
- page_indices = list(range(start_page_id, end_page_id + 1))
-
- # 从原PDF导入页面到新PDF
- output_pdf.import_pages(pdf, page_indices)
+ # 逐页导入,失败则跳过
+ output_index = 0
+ for page_index in range(start_page_id, end_page_id + 1):
+ try:
+ output_pdf.import_pages(pdf, pages=[page_index])
+ output_index += 1
+ except Exception as page_error:
+ output_pdf.del_page(output_index)
+ logger.warning(f"Failed to import page {page_index}: {page_error}, skipping this page.")
+ continue
# 将新PDF保存到内存缓冲区
output_buffer = io.BytesIO()
@@ -66,7 +71,6 @@ def convert_pdf_bytes_to_bytes_by_pypdfium2(pdf_bytes, start_page_id=0, end_page
except Exception as e:
logger.warning(f"Error in converting PDF bytes: {e}, Using original PDF bytes.")
output_bytes = pdf_bytes
-
pdf.close()
output_pdf.close()
return output_bytes