pdf-extract-ordered-fallback
它能做什么
这项技能为从网络获取 PDF 文字设定了明确的工具使用顺序。适合 PDF 任务因重定向、文件损坏或工具缺失而失败的人。
适用场景
- ✓当 PDF 链接发生重定向,或返回网页而不是文件时。
- ✓当我需要在读取前确认 PDF 确实是真正的文件时。
- ✓当我希望使用固定的方法顺序,避免 AI 随意尝试各种工具时。
技能链接
https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/pdf-download-extract-fallback-enhanced/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请从这个链接下载 PDF 并提取其文字。请按固定顺序使用工具,并告诉我哪一个成功了。”
每周 5 个新技能,1 分钟读完。
工作原理
AI 用类似浏览器的请求下载文件,用 file 命令检查其类型,然后依次尝试简单读取、pdftotext 和 PyMuPDF,直到提取出文字。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
PDF Download and Extract with Ordered Fallback
This skill provides a robust workflow for acquiring PDF documents from web sources and extracting their text content, with a clearly ordered sequence of tool invocations to maximize success rate.
Overview
When working with PDFs from web sources, encounters with JavaScript redirects, corrupted files, missing tools, or inaccessible content are common. This workflow ensures maximum success rate through a严格 ordered fallback sequence that prioritizes shell-based tools over Python sandbox execution.
Ordered Tool Chain Summary
| Step | Tool | Method | Priority |
|---|---|---|---|
| 0 | read_file | Direct PDF text extraction | First attempt |
| 1 | run_shell | pdftotext command | Primary fallback (if Step 0 returns binary/fails) |
| 2 | execute_code_sandbox | PyMuPDF Python library | Secondary fallback (if Step 1 fails) |
| 3 | Domain knowledge | Manual content generation | Last resort |
Key principle: Always try shell tools (run_shell) before Python sandbox (execute_code_sandbox) when both are viable options. Shell execution is more reliable in constrained environments.
Step-by-Step Instructions
Step 0: Initial Extraction Attempt with read_file Tool
First, attempt to extract PDF text using the read_file tool. This is the simplest approach and handles many PDFs correctly:
read_file filetype="pdf" file_path="path/to/document.pdf"
Expected outcomes:
- Success: Returns extracted text content - proceed to use this directly
- Binary/Image data returned: The tool failed to extract text; file content is raw binary or image data
- Immediate action: Proceed to Step 1 (run_shell with pdftotext)
- Error returned: Tool failed entirely; proceed to Step 1 (run_shell with pdftotext)
Critical: If read_file returns binary data (PNG/JPEG headers, raw PDF bytes), do NOT attempt to parse it manually. Immediately switch to shell-based pdftotext.
Step 1: Download PDF with Browser User-Agent
Many PDF hosting sites use JavaScript-based redirects or block automated requests. Use curl with a realistic browser user-agent:
curl -L -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" -o output.pdf "URL_HERE"
Key flags:
-L: Follow redirects-A: Set user-agent header to mimic a real browser-o: Specify output filename
Step 2: Verify File Type Before Parsing
Always validate the downloaded file is actually a PDF before attempting extraction:
file output.pdf
Expected output should contain "PDF document". If not:
- The URL may have redirected to an HTML error page
- The file may be corrupted
- Access may be blocked
Step 3: Primary Shell Extraction with pdftotext via run_shell
This step takes priority over Python-based extraction. If Step 0 failed or if you're working with a newly downloaded PDF, use run_shell with pdftotext before attempting any Python libraries:
pdftotext downloaded.pdf extracted.txt
Execute via run_shell:
run_shell command="pdftotext downloaded.pdf extracted.txt"
If pdftotext is not available, install it first:
# Debian/Ubuntu
apt-get update && apt-get install -y poppler-utils
# macOS
brew install poppler
# RHEL/CentOS
yum install -y poppler-utils
Why shell-first? Shell-based pdftotext is more reliable, faster, and avoids sandbox execution issues that can affect Python code execution in constrained environments.
Step 4: Secondary Python Fallback with PyMuPDF via execute_code_sandbox
Only if run_shell with pdftotext fails or is unavailable, fall back to Python's PyMuPDF library via execute_code_sandbox:
import fitz # PyMuPDF
doc = fitz.open("downloaded.pdf")
text = ""
for page in doc:
text += page.get_text()
doc.close()
with open("extracted.txt", "w") as f:
f.write(text)
Execute within execute_code_sandbox:
execute_code_sandbox code="<Python code above>"
Install if needed:
pip install pymupdf
Note: Some environments may experience execute_code_sandbox failures (unknown errors). This is why shell-based extraction (Step 3) must be attempted first.
Step 5: Graceful Degradation to Domain Knowledge
If the PDF cannot be accessed or extracted after all attempts:
- Document the failure mode (network issue, corrupted file, access denied, etc.)
- Extract any partia
常见问题
文件类型怎样检查?
file 命令可以确认下载的是 PDF 还是错误页面。
为什么要在解析前检查?
重定向可能返回 HTML 页面而不是 PDF,会导致提取失败。
最后的办法是什么?
指南说,当所有提取方法都无效时,就根据领域知识生成内容。
详情
作者的原始描述
PDF extraction with ordered tool chain: read_file, then run_shell/pdftotext, then execute_code_sandbox/PyMuPDF
repo: HKUDS/AgentPilot
热度
- GitHub 星标
- 7.8k
- 排名
- #12918
- 许可证
- 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帮你的智能体规划小型实验、阅读实验结果并记录发现,同时研究如何改进一个程序。