pdf-text-extraction-fallback
它能做什么
这项技能说明,当内置文件读取器失败时,如何使用 pdftotext 提取 PDF 文字。它适合处理含图片或复杂格式 PDF 的人。
适用场景
- ✓当读取 PDF 返回二进制或图片数据的时候。
- ✓当 PDF 包含嵌入图片或复杂格式的时候。
- ✓当我需要从 PDF 中获得可读的文字,用于完成任务的时候。
技能链接
https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/pdf-text-extraction-fallback/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“文件读取器返回了我 PDF 的二进制数据。请检查调用参数,并用 pdftotext 提取文字。”
每周 5 个新技能,1 分钟读完。
工作原理
AI 会确认读取调用使用了正确的参数名,检测二进制输出,然后运行 pdftotext,将文字打印到标准输出。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
PDF Text Extraction Fallback
When to Use This Skill
Use this skill when read_file with filetype="pdf" returns binary/image data instead of readable text content. This is a common issue with PDF files that contain embedded images or complex formatting.
Steps
1. Validate Parameters First
Before attempting extraction, ensure you're using the correct parameter name:
- Use
filetype(notfile_type) for theread_filefunction - Incorrect parameter names can cause silent failures
# Correct
read_file(filetype="pdf", file_path="document.pdf")
# Incorrect - may fail silently
read_file(file_type="pdf", file_path="document.pdf")
2. Detect Binary Data Issue
After calling read_file, check if the result contains:
- Garbled/binary characters
- Image data representations (e.g.,
b'...'byte strings with non-text content) - Unreadable or corrupted-looking content
If yes, proceed with the pdftotext workaround.
3. Extract Text via pdftotext
Use run_shell to call pdftotext, which extracts text directly from PDF files:
# Extract text to stdout
result = run_shell(command="pdftotext /path/to/document.pdf -")
text_content = result.stdout
The - flag tells pdftotext to output to stdout instead of creating a file.
4. Handle Output and Errors
result = run_shell(command="pdftotext /path/to/document.pdf -")
if result.stderr:
# Check for errors like "pdftotext not found"
# May need to install poppler-utils
pass
text_content = result.stdout
# text_content now contains the extracted text
Example Workflow
# Step 1: Try normal read with correct parameters
content = read_file(filetype="pdf", file_path="reference.pdf")
# Step 2: Check if content is readable
if not content or looks_like_binary(content):
# Step 3: Fall back to pdftotext
result = run_shell(command="pdftotext reference.pdf -")
text_content = result.stdout
# Step 4: Verify extraction succeeded
if result.stderr:
# Handle error (e.g., install pdftotext)
pass
Installation Notes
pdftotext is part of the poppler-utils package:
- Debian/Ubuntu:
apt-get install poppler-utils - macOS:
brew install poppler - Many Linux environments: Pre-installed
Alternative: Output to File
If stdout approach has issues, output to a temporary file:
run_shell(command="pdftotext /path/to/document.pdf /tmp/output.txt")
text_content = read_file(filetype="txt", file_path="/tmp/output.txt")
Best Practices
- Always validate the
filetypeparameter spelling before troubleshooting - Check both stdout and stderr from pdftotext
- For multi-page PDFs, pdftotext preserves page breaks with form feeds
- This method works better for text-based PDFs than image-scanned PDFs
常见问题
为什么参数名称很重要?
指南说,参数名错误可能导致静默失败。
如何识别二进制输出?
它会查找结果中的二进制字符或图片数据标记。
命令中的短横线有什么作用?
它告诉 pdftotext 把文字打印到屏幕上,而不是输出到文件。
详情
作者的原始描述
Extract text from PDFs using pdftotext when read_file returns binary data
repo: HKUDS/AgentPilot
热度
- GitHub 星标
- 7.8k
- 排名
- #12934
- 许可证
- 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帮你的智能体规划小型实验、阅读实验结果并记录发现,同时研究如何改进一个程序。