pdf-read-file-fallback
它能做什么
这项技能是针对内置读取器返回图片或二进制数据的 PDF 的备用方案。它适合需要读取 AI 无法正常读取的 PDF 中文字的人。
适用场景
- ✓当读取 PDF 返回的是图片或二进制数据而非文字的时候。
- ✓当 PDF 是扫描文档或格式复杂的时候。
- ✓当我需要以可读形式获取 PDF 中的文字的时候。
技能链接
https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/pdf-read-file-fallback/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“AI 只能从我的 PDF 获得二进制数据。请用命令行工具把文字提取出来。”
每周 5 个新技能,1 分钟读完。
工作原理
AI 会先尝试正常的文件读取,检查结果是否可用;如果不可用,就用带版面选项的 pdftotext 获取文字。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
PDF Text Extraction Fallback
When to Use
Use this pattern when read_file with filetype="pdf" returns binary image data instead of extractable text content. This commonly occurs with PDFs that contain scanned images or complex formatting.
Steps
1. Attempt Primary Extraction
First, try using read_file:
result = read_file(file_path="document.pdf", filetype="pdf")
Important: Use filetype (not file_type) - incorrect parameter naming will cause execution failures.
2. Detect Binary/Image Data
Check if the result contains unusable content:
# Indicators of binary/image data:
# - Contains null bytes: '\x00'
# - Very short or empty
# - Contains image markers (PNG/JPEG headers)
# - Unreadable character sequences
if not result or len(result) < 50 or '\x00' in str(result):
# Proceed to fallback
3. Use pdftotext via run_shell
Extract text using the pdftotext command-line tool:
shell_result = run_shell(command="pdftotext -layout document.pdf -")
text_content = shell_result.stdout
The - flag outputs to stdout for easy capture. The -layout flag preserves original formatting.
4. Handle pdftotext Unavailable
If pdftotext is not installed, try Python-based extraction:
result = execute_code_sandbox(code="""
import pdfplumber
text = ''
with pdfplumber.open('document.pdf') as pdf:
for page in pdf.pages:
extracted = page.extract_text()
if extracted:
text += extracted + '\\n'
print(text)
""")
Complete Example
file_path = "report.pdf"
# Primary attempt
result = read_file(file_path=file_path, filetype="pdf")
# Validate and fallback if needed
if not result or len(str(result)) < 100 or '\x00' in str(result):
# Fallback to pdftotext
shell_result = run_shell(command=f"pdftotext -layout {file_path} -")
text_content = shell_result.stdout
# If pdftotext fails, try Python extraction
if not text_content or len(text_content) < 50:
code_result = execute_code_sandbox(code=f"""
import pdfplumber
text = ''
with pdfplumber.open('{file_path}') as pdf:
for page in pdf.pages:
extracted = page.extract_text()
if extracted:
text += extracted + '\\n'
print(text)
""")
text_content = code_result
Notes
pdftotextis part of thepoppler-utilspackage on most Linux systems- For scanned/image-only PDFs, consider OCR tools (tesseract) instead
- Always validate parameter names against tool documentation (
filetypevsfile_type)
常见问题
版面选项有什么作用?
它尽可能保留页面原有的间距和结构。
如果没有 pdftotext 怎么办?
指南建议使用 Python 库作为备用,例如 pdfplumber。
如何识别二进制输出?
指南提到的迹象包括空字节、结果极短或出现图片文件头。
详情
作者的原始描述
Extract text from PDFs using pdftotext when read_file returns binary data
repo: HKUDS/AgentPilot
热度
- GitHub 星标
- 7.8k
- 排名
- #12931
- 许可证
- MIT
- 更新时间
- 2026/11/1
评论
- …
同一仓库的更多技能
agentpilot
7.8k帮你的智能体准备任务并运行 AgentPilot 循环,逐步改进项目:循环会提出修改、测试修改并为其打分。
openspace
7.8k帮你的智能体设置 OpenSpace,这是一个用于存储和获取智能体技能的工具,并通过 MCP 将其连接到你的 AI 应用。
rsi-arena
7.8k帮你的智能体设置并运行标准 AI 基准测试,例如 AIME 和 Terminal-Bench,用来比较不同的改进方法。
experiment-guide
7.8k帮你的智能体规划小型实验、阅读实验结果并记录发现,同时研究如何改进一个程序。