跳到正文
Skillpedia
PDF、Word 与幻灯片7.8k · #12934

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。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/pdf-text-extraction-fallback/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 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 (not file_type) for the read_file function
  • 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

  1. Always validate the filetype parameter spelling before troubleshooting
  2. Check both stdout and stderr from pdftotext
  3. For multi-page PDFs, pdftotext preserves page breaks with form feeds
  4. 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

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能