pdf-extraction-fallback-80956b
它能做什么
这项技能涵盖从官方或受保护的网站下载 PDF 并读取其文字,同时检查下载是否失败。它适合从网上收集手册或法规文件的人。
适用场景
- ✓当从政府或公司网站下载的 PDF 只是一个很小或损坏的文件的时候。
- ✓当网站可能阻止自动下载,或用脚本加载内容的时候。
- ✓当我需要从网上下载的 PDF 中可靠地获取文字的时候。
技能链接
https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/pdf-extraction-fallback-80956b/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请从这个机构网站下载手册 PDF,并提取其中的文字。如果下载的文件看起来有问题,请换一种方法。”
每周 5 个新技能,1 分钟读完。
工作原理
AI 使用 curl 下载文件,检查其大小以及是否包含错误页面;如果文件有效,再用备用工具提取文字。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
PDF Extraction Fallback Workflow
This skill provides a robust, multi-layered approach to downloading and extracting text from PDF documents when sources may be protected, corrupted, or inaccessible via standard methods.
When to Use
- Downloading regulatory documents, handbooks, or official PDFs from government/enterprise websites
- Sources that may have JavaScript protection, CORS restrictions, or dynamic content
- When initial PDF downloads produce suspiciously small files or error content
- Any scenario requiring reliable text extraction from potentially problematic PDF sources
Core Workflow
Step 1: Download with Validation
# Download PDF with size check
curl -L -o output.pdf "https://example.com/document.pdf"
# Early failure detection: check file size
file_size=$(stat -c%s output.pdf 2>/dev/null || stat -f%z output.pdf)
if [ "$file_size" -lt 1000 ]; then
echo "WARNING: File size ($file_size bytes) suggests failed download or error page"
# Check for HTML/JavaScript error content
if head -c 500 output.pdf | grep -qi "<html\|<script\|error\|access denied"; then
echo "FAILURE: File contains error message, not PDF content"
rm output.pdf
# Proceed to alternative download method
fi
fi
Step 2: Sequential Extraction Fallbacks
Try extraction methods in order, moving to next on failure:
Fallback 1: pdftotext (command-line)
if command -v pdftotext &> /dev/null; then
pdftotext output.pdf output.txt
if [ -s output.txt ] && [ $(wc -c < output.txt) -gt 100 ]; then
echo "SUCCESS: pdftotext extraction"
exit 0
fi
fi
Fallback 2: PyMuPDF (fitz)
import fitz # PyMuPDF
def extract_with_pymupdf(pdf_path):
try:
doc = fitz.open(pdf_path)
text = ""
for page in doc:
text += page.get_text()
doc.close()
if len(text.strip()) > 100:
return text
return None
except Exception as e:
print(f"PyMuPDF failed: {e}")
return None
Fallback 3: pdfplumber
import pdfplumber
def extract_with_pdfplumber(pdf_path):
try:
text = ""
with pdfplumber.open(pdf_path) as pdf:
for page in pdf.pages:
page_text = page.extract_text()
if page_text:
text += page_text + "\n"
if len(text.strip()) > 100:
return text
return None
except Exception as e:
print(f"pdfplumber failed: {e}")
return None
Step 3: Content Sanity Validation
After any extraction method succeeds:
def validate_extracted_text(text, min_length=100):
"""Validate extracted content is meaningful"""
if not text or len(text.strip()) < min_length:
return False
# Check for common error patterns
error_patterns = [
"access denied", "permission denied", "error",
"javascript", "<html", "<script", "404", "403"
]
text_lower = text.lower()[:500] # Check first 500 chars
for pattern in error_patterns:
if pattern in text_lower:
return False
return True
Complete Workflow Script
#!/usr/bin/env python3
"""
Robust PDF extraction with multiple fallbacks
"""
import subprocess
import os
import sys
def download_pdf(url, output_path):
"""Download PDF with validation"""
subprocess.run(["curl", "-L", "-o", output_path, url], check=True)
# Validate download
if not os.path.exists(output_path):
return False
file_size = os.path.getsize(output_path)
if file_size < 1000:
with open(output_path, 'r', errors='ignore') as f:
content = f.read(500).lower()
if any(x in content for x in ['<html', '<script', 'error', 'denied']):
os.remove(output_path)
return False
return True
def extract_text(pdf_path):
"""Try multiple extraction methods"""
# Method 1: pdftotext
try:
result = subprocess.run(
["pdftotext", pdf_path, "-"],
capture_output=True, text=True, timeout=60
)
if result.stdout and len(result.stdout.strip()) > 100:
return result.stdout
except:
pass
# Method 2: PyMuPDF
try:
import fitz
doc = fitz.open(pdf_path)
text = "".join(page.get_text() for page in doc)
doc.close()
if len(text.strip()) >
常见问题
如何发现下载失败?
指南会检查文件是否非常小,或是否包含 HTML 或“拒绝访问”之类的信息。
有问题的文件会怎样处理?
指南会删除它,并尝试其他的下载方式。
首先使用哪个工具提取文字?
指南的第一选择是 shell 中的 pdftotext。
详情
作者的原始描述
Multi-fallback PDF download and text extraction with early failure detection
repo: HKUDS/AgentPilot
热度
- GitHub 星标
- 7.8k
- 排名
- #12922
- 许可证
- 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帮你的智能体规划小型实验、阅读实验结果并记录发现,同时研究如何改进一个程序。