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

robust-pdf-read

它能做什么

这项技能展示 AI 智能体如何借助 pdftotext 工具,从 PDF 中提取可读的文字。它适合 PDF 读取结果为无法识别字符的人。

适用场景

  • ✓当文件读取器对 PDF 返回原始二进制、base64 图片数据或乱码字符的时候。
  • ✓当你需要以纯文本形式获得 PDF 中的文字,以便使用的时候。
  • ✓当你希望尽可能保留 PDF 文字的原始版式的时候。

技能链接

https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/robust-pdf-read/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

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

可以这样问 AI

“请读取我的租约 PDF 并给我文字。文件读取器只显示乱码字符。”

每周 5 个新技能,1 分钟读完。

工作原理

智能体会检查普通读取器的输出是否包含二进制或乱码内容。如果发现,它会运行带版面选项且不含分页符的 pdftotext,将文字输出到标准输出,以便阅读。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Robust PDF Text Extraction

Problem

Standard file reading tools (e.g., read_file) often fail to extract text from PDF documents. Instead of returning parsed text, they may return:

  • Raw binary data
  • Base64 encoded images
  • Garbled characters or null bytes

This occurs because PDFs are complex binary formats, not plain text files. Attempts to parse them using general-purpose Python libraries (like PyMuPDF) in sandboxed environments may also fail due to missing dependencies or environment restrictions.

Solution

Use the pdftotext command-line utility (part of poppler-utils) via run_shell. This tool is commonly pre-installed in Linux environments and reliably extracts text content from PDFs.

Procedure

1. Detect Extraction Failure

When attempting to read a PDF:

  • Check the content returned by read_file.
  • If the content contains null bytes (\x00), appears as base64, or is clearly binary/garbled, assume standard reading has failed.

2. Execute pdftotext

Run the following shell command using run_shell:

pdftotext -layout -nopgbrk <file_path> -
  • -layout: Maintains the physical layout of the text (optional but recommended).
  • -nopgbrk: Prevents inserting form feed characters between pages.
  • -: Outputs content to stdout instead of creating a new file.

3. Parse Output

Capture the stdout from the shell command. This string is the extracted text.

Example Usage

Scenario: You need to read document.pdf.

Step 1: Attempt standard read

content = read_file("document.pdf")
if "\x00" in content or not content.strip():
    # Fallback needed
    pass

Step 2: Fallback to shell

result = run_shell("pdftotext -layout -nopgbrk document.pdf -")
text = result.stdout

Prerequisites

  • The environment must have pdftotext installed (usually via poppler-utils).
  • If pdftotext is not found, attempt to install it (apt-get install poppler-utils) if permissions allow, or notify the user.

Benefits

  • Reliability: Bypasses Python library dependency issues in sandboxes.
  • Speed: Command-line tools are often faster than loading heavy Python libraries.
  • Compatibility: Works consistently across most Linux-based agent environments.
常见问题

智能体如何发现读取失败?

它会查找空字节、类似 base64 的数据,或明显的乱码文字。

pdftotext 的选项有什么作用?

layout 选项保留页面版式,nopgbrk 选项去除分页符。

这需要特别安装吗?

这项技能说明 pdftotext 是 poppler-utils 的一部分,在 Linux 系统上通常已经安装。

详情

作者的原始描述

Reliably extract text from PDFs using pdftotext when standard file reading fails.

repo: HKUDS/AgentPilot

热度

GitHub 星标
7.8k
排名
#12977
许可证
MIT
更新时间
2026/11/1

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能