본문으로 건너뛰기
Skillpedia
PDF, Word, 슬라이드7.8k · #12918

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가 스킬을 읽고 지침을 따릅니다.

바로 붙여넣을 수 있는 메시지

Use this skill to help me: https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/pdf-download-extract-fallback-enhanced/SKILL.md Read the SKILL.md file at that link and follow its instructions for my task.
AI가 이런 스킬을 알아서 찾아 쓰길 원하시나요?무료 Skillpedia MCP를 설치하세요. 한 줄이면 작업마다 스킬 15,000+개 중에서 검색합니다.1분 만에 설치 →

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:

  1. Document the failure mode (network issue, corrupted file, access denied, etc.)
  2. Extract any partia
자주 묻는 질문

파일 종류는 어떻게 확인하나요?

file 명령이 다운로드된 파일이 PDF인지 오류 페이지인지 확인합니다.

파싱 전에 왜 확인하나요?

리디렉션이 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.

이 스킬이 유용한가요?

0

로그인 투표하려면 로그인하세요.

댓글

  • …

이 저장소의 다른 스킬

비슷한 스킬