阅读文本 / Reading text
This paper uses synthetic samples to demonstrate the processing method. The code has been verified using examples in an isolated environment; this does not guarantee effectiveness for all real files or products. Before processing important files, please retain the original copies and verify them against the boundaries and acceptance criteria outlined in this document.
when the automation script processes
report_final_v2.pdf
When the document title is written into the database, downstream translation or retrieval systems may receive a meaningless string. The PDF filename is determined by the operating system or the downloader, while the actual document title is stored inside the PDF itself.
/Info
In dictionaries or XMP metadata. These two are often inconsistent, and even the internal metadata itself may be empty, garbled, or filled with placeholder values written by faulty tools. Directly trusting
doc.metadata["title"]
is a common source of faults. The correct approach is to establish a hierarchical title-extraction strategy and clearly document the rationale for each rollback, ensuring that subsequent manual reviews or automated processing can trace the decision-making basis.
Typical scenarios of metadata missing or untrustworthy
The PDF specification permits metadata fields to be empty or entirely absent. In actual documents, we have observed three common types of issues: first, the generating tool fails to write any metadata at all,
title
field returns an empty string; second, metadata is overwritten by batch-processing tools with default values such as “Untitled,” “Microsoft Word,” or a string of UUIDs; third, encoding errors cause the title to appear as gibberish or truncated characters. Furthermore, some PDFs place the actual title in the main text on the first page rather than in the metadata section; in such cases, even if the metadata is non-empty, it may not correspond to the document’s intended identifier. These situations cannot be resolved through a single API call and require the design of explicit validation and fallback logic.
synthesis example: correct output and boundary input handling
The following is a set of
synthesis example
, used to illustrate the expected behavior. Note: These samples must be constructed by the reader and are not actual files.
normal input
: PDF internal
/Title
is "2024Annual Financial Audit Report," with the filename
audit_2024.pdf
. The expected output title is "2024 Annual Financial Audit Report," with the source tag as
metadata
。
boundary input 1
:
/Title
is an empty string, and the filename is
Q3_report_draft.pdf
. The expected output title is "Q3_report_draft" (without the file extension), with the source tag as
filename_fallback
。
boundary input 2
:
/Title
is "untitled document" (case-insensitive matching against the predefined list of invalid values), filename
scan_001.pdf
. The expected output title is "scan_001", with the source tag as
invalid_metadata_fallback
。
boundary input 3
:
/Title
Contains an illegal UTF-8 sequence; decoding failed. Expected to throw a clear exception or return a safe default value, and log it.
decode_error
, never silently use corrupted text.
The key is that each output must carry a source label, and the fallback rules are explicitly defined in the code, rather than relying on implicit assumptions.
PyMuPDF Metadata Extraction and Validation Implementation
The following Python example implements the above logic using PyMuPDF. Dependencies:
PyMuPDF>=1.24.0
(According to the official documentation’s inference on API stability, please lock to a specific version for actual use.) The code constructs a minimal PDF in memory to demonstrate the logic, without requiring external files. If you need to test with real documents, please replace
stream
is a local PDF byte stream, and ensures that the file is legally readable.
import pymupdf
import re
from typing import Dict, Any
# 合成示例:创建一个带元数据的PDF用于演示
def create_sample_pdf(title: str) -> bytes:
doc = pymupdf.open()
page = doc.new_page()
page.insert_text((72, 72), "Sample content")
if title is not None:
doc.set_metadata({"title": title})
return doc.tobytes()
INVALID_TITLES = {"untitled", "untitled document", "microsoft word", "", "document"}
def extract_title_with_fallback(pdf_bytes: bytes, filename: str) -> Dict[str, str]:
doc = None
try:
doc = pymupdf.open(stream=pdf_bytes)
meta = doc.metadata or {}
raw_title = (meta.get("title") or "").strip()
# 检查是否为无效占位符或纯空白
if not raw_title or raw_title.lower() in INVALID_TITLES:
fallback = re.sub(r'\.[^.]+$', '', filename)
source = "invalid_metadata_fallback" if raw_title else "filename_fallback"
return {"title": fallback, "source": source}
# 有效元数据标题
return {"title": raw_title, "source": "metadata"}
except UnicodeDecodeError:
raise ValueError("Metadata title contains invalid encoding")
finally:
if doc is not None:
doc.close()
# 验证逻辑(合成样本)
sample_ok = create_sample_pdf("2024年度财务审计报告")
result_ok = extract_title_with_fallback(sample_ok, "audit_2024.pdf")
assert result_ok == {"title": "2024年度财务审计报告", "source": "metadata"}
sample_empty = create_sample_pdf("")
result_empty = extract_title_with_fallback(sample_empty, "Q3_report_draft.pdf")
assert result_empty == {"title": "Q3_report_draft", "source": "filename_fallback"}
sample_invalid = create_sample_pdf("Untitled Document")
result_invalid = extract_title_with_fallback(sample_invalid, "scan_001.pdf")
assert result_invalid == {"title": "scan_001", "source": "invalid_metadata_fallback"}
This code only demonstrates title extraction and fallback logic; it does not include full features such as XMP parsing, multilingual title selection, or OCR-based fallback. In a production environment, validation rules must be extended, and logging should be added.
Acceptance Criteria and Manual Review Boundary
Observe the phenomenon
processing decision
Whether automation applies
metadata title is non-empty and not in the invalid list
uses a metadata header, with the tag source=metadata
is
metadata title is empty or matches an invalid pattern
falls back to the filename without the extension, marking the corresponding fallback type
is
metadata decoding failed or an exception was thrown
interrupt handling, log the error, and do not attempt to infer the content
No, manual verification of file integrity is required
The filename itself is meaningless (e.g., tmp123.pdf), and the metadata is invalid
automatically reverts and still executes, but the result should be labeled as low_confidence
No, manual confirmation or contextual supplementation is required
document is a scanned image and has no text layer
This logic does not apply; the homepage title must first be extracted via OCR
No, it belongs to a different processing pipeline
Note: This table applies only to
metadata title extraction
stage. The end-to-end document understanding performance still needs to be evaluated comprehensively, taking into account the main text content, table of contents structure, and other factors. Code demonstrations do not equate to production-level reliability.
Reference and Next Step
PyMuPDF Document.metadata Official documentation: https://pymupdf.readthedocs.io/en/latest/document.html#Document.metadata
PyMuPDF set_metadata method description: Same as on the previous page
set_metadata
entry
uses this checklist for document translation
first ensures that the page order, source locations, and text integrity have been thoroughly checked, then submits the same de-identified sample to the translation process. After translation, it is still necessary to verify that the headings, body text, and figure captions correspond correctly; one should not rely solely on whether the text flows smoothly.
OmniDoc (product entry point)
provides an entry point for translating multi-format documents, and the official website highlights its OCR, terminology database, and bilingual alignment features. For teams that wish to start processing directly from files, this is a path worth evaluating with real-world samples. Please use de-identified files that you are authorized to upload, and verify the results against the checklist provided in this document; we do not claim that OmniDoc utilizes the open-source libraries or internal algorithms referenced herein, nor do we substitute examples for product performance testing.
- 1 -
Updated 2026-10-08