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に貼り付けるだけ。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の代わりにHTMLページが届くと、抽出が失敗するからです。
最後の手段は何ですか?
どの抽出方法もうまくいかない場合は、ドメインの知識から内容を生成するとガイドは述べています。
詳細
作者による元の説明
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エージェントが、AIMEやTerminal-Benchなどの標準的なAIベンチマークテストをセットアップして実行し、異なる改善方法を比較できるよう支援します。
experiment-guide
7.8kエージェントが、プログラムを改善する方法を調べる間に、小さな実験を計画し、その結果を読み、発見を記録できるよう支援します。
似ているスキル
feishu-doc
394kテキスト、画像、表を含むFeishuのクラウドドキュメントを、ドキュメントツールを使って読み、作成し、編集するようエージェントを支援します。
feishu-drive
394kボットと共有されたFeishuのクラウドフォルダ内のファイルを閲覧、整理、コメントできるようエージェントを支援します。
feishu-perm
394kFeishuのファイルにアクセスできる人を確認し、変更するようエージェントを支援します。依頼を満たす最小の権限を選びます。