pdftotext-fallback
它能做什么
这项技能是用 shell 中的 pdftotext 进行 PDF 文字提取的恢复方法。它适合 PDF 读取工具给出的结果无法使用的人。
适用场景
- ✓当读取 PDF 返回二进制或图片数据的时候。
- ✓当沙箱中的 Python PDF 提取失败或返回乱码的时候。
- ✓当我需要从 PDF 中获得可靠的文字,且一条简单命令就能完成的时候。
技能链接
https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/pdftotext-fallback/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“沙箱给出的 PDF 文字是乱码。请用 shell 中的 pdftotext 获取真正的文字。”
每周 5 个新技能,1 分钟读完。
工作原理
AI 在 shell 中运行 pdftotext,并把输出打印到屏幕上;处理复杂页面时使用版面选项,处理大文档时则写入文件。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
pdftotext Fallback for PDF Text Extraction
When to Use
Apply this pattern when:
read_fileon a PDF returns binary/image data instead of readable text- Python-based PDF extraction (PyMuPDF, pdfplumber, etc.) in
execute_code_sandboxfails or returns garbled content - You need reliable text extraction from PDF files as a recovery strategy
Why This Works
The pdftotext utility (from poppler-utils) is a mature, command-line tool that handles many PDF edge cases that confuse Python libraries or the read_file tool. It's pre-installed on most Linux systems and provides consistent, reliable text extraction.
Steps
Step 1: Detect the Failure
Recognize extraction failure when:
read_filereturns binary content, image data, or garbled text- Error messages indicate encoding issues or unsupported formats
- Python PDF libraries in sandbox fail with import errors or extraction failures
Step 2: Use pdftotext via run_shell
Extract to stdout (recommended for quick extraction):
pdftotext /path/to/file.pdf -
The - argument outputs directly to stdout for easy capture in your tool response.
Example:
run_shell("pdftotext document.pdf -")
Step 3: Handle Complex PDFs
Preserve layout (maintains original formatting):
pdftotext -layout /path/to/file.pdf -
Extract to a file (for large PDFs):
pdftotext /path/to/file.pdf /path/to/output.txt
Then read the output file with read_file.
Handle encoded text:
pdftotext -enc UTF-8 /path/to/file.pdf -
Step 4: Verify and Continue
- Check the extracted text for completeness
- If text is still garbled, the PDF may be image-based (scanned) - consider OCR tools
- Proceed with your task using the extracted text
Code Examples
Basic extraction:
# Simple text extraction
text = run_shell("pdftotext document.pdf -")
With error handling:
# Try extraction, check for success
result = run_shell("pdftotext document.pdf - && echo 'SUCCESS' || echo 'FAILED'")
Multi-page PDF with layout:
# Preserve tables and formatting
text = run_shell("pdftotext -layout document.pdf -")
Troubleshooting
| Issue | Solution |
|---|---|
pdftotext: command not found |
Install poppler-utils: apt-get install poppler-utils |
| Garbled/special characters | Try -enc UTF-8 or -enc ASCII7 |
| Missing formatting | Use -layout flag |
| Scanned PDF (no text) | Requires OCR (e.g., tesseract), not text extraction |
| Large PDF timeout | Extract to file instead of stdout |
Limitations
- Does not work on image-only (scanned) PDFs without embedded text
- May lose complex formatting, tables, or embedded objects
- Requires poppler-utils to be installed (common on Linux, may need installation on other systems)
Related Tools
pdfinfo: Get PDF metadata (pages, size, etc.)pdftoppm: Convert PDF pages to imagestesseract: OCR for scanned PDFs
常见问题
为什么认为 pdftotext 可靠?
指南将它描述为成熟的命令行工具,能处理许多 PDF 的特殊情况。
短横线有什么作用?
它把提取的文字发送到屏幕上,便于 AI 捕获。
处理大型 PDF 的选项是什么?
指南建议把结果提取到文件中,而不是输出到屏幕。
详情
作者的原始描述
Recover PDF text extraction when read_file returns binary data by using pdftotext via shell
repo: HKUDS/AgentPilot
热度
- GitHub 星标
- 7.8k
- 排名
- #12942
- 许可证
- 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帮你的智能体规划小型实验、阅读实验结果并记录发现,同时研究如何改进一个程序。