Chuyển đến nội dung
Skillpedia
PDF, Word và slide7.8k · #12931

pdf-read-file-fallback

Skill này làm gì

Skill này là phương án dự phòng cho các PDF mà trình đọc tích hợp trả về dưới dạng hình ảnh hoặc dữ liệu nhị phân. Dành cho người cần đọc chữ bên trong PDF mà AI không đọc bình thường được.

Khi nào nên dùng

  • ✓Khi đọc một PDF trả về hình ảnh hoặc dữ liệu nhị phân thay vì văn bản.
  • ✓Khi PDF là tài liệu quét hoặc có định dạng phức tạp.
  • ✓Khi tôi cần văn bản từ một PDF ở dạng đọc được.

Liên kết skill

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

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/pdf-read-file-fallback/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“AI chỉ nhận được dữ liệu nhị phân từ PDF của tôi. Hãy lấy văn bản ra bằng một công cụ dòng lệnh.”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

AI thử đọc tệp bình thường trước, kiểm tra xem kết quả có dùng được không, và nếu không thì chạy pdftotext với tùy chọn layout để lấy văn bản.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

PDF Text Extraction Fallback

When to Use

Use this pattern when read_file with filetype="pdf" returns binary image data instead of extractable text content. This commonly occurs with PDFs that contain scanned images or complex formatting.

Steps

1. Attempt Primary Extraction

First, try using read_file:

result = read_file(file_path="document.pdf", filetype="pdf")

Important: Use filetype (not file_type) - incorrect parameter naming will cause execution failures.

2. Detect Binary/Image Data

Check if the result contains unusable content:

# Indicators of binary/image data:
# - Contains null bytes: '\x00'
# - Very short or empty
# - Contains image markers (PNG/JPEG headers)
# - Unreadable character sequences

if not result or len(result) < 50 or '\x00' in str(result):
    # Proceed to fallback

3. Use pdftotext via run_shell

Extract text using the pdftotext command-line tool:

shell_result = run_shell(command="pdftotext -layout document.pdf -")
text_content = shell_result.stdout

The - flag outputs to stdout for easy capture. The -layout flag preserves original formatting.

4. Handle pdftotext Unavailable

If pdftotext is not installed, try Python-based extraction:

result = execute_code_sandbox(code="""
import pdfplumber
text = ''
with pdfplumber.open('document.pdf') as pdf:
    for page in pdf.pages:
        extracted = page.extract_text()
        if extracted:
            text += extracted + '\\n'
print(text)
""")

Complete Example

file_path = "report.pdf"

# Primary attempt
result = read_file(file_path=file_path, filetype="pdf")

# Validate and fallback if needed
if not result or len(str(result)) < 100 or '\x00' in str(result):
    # Fallback to pdftotext
    shell_result = run_shell(command=f"pdftotext -layout {file_path} -")
    text_content = shell_result.stdout
    
    # If pdftotext fails, try Python extraction
    if not text_content or len(text_content) < 50:
        code_result = execute_code_sandbox(code=f"""
import pdfplumber
text = ''
with pdfplumber.open('{file_path}') as pdf:
    for page in pdf.pages:
        extracted = page.extract_text()
        if extracted:
            text += extracted + '\\n'
print(text)
""")
        text_content = code_result

Notes

  • pdftotext is part of the poppler-utils package on most Linux systems
  • For scanned/image-only PDFs, consider OCR tools (tesseract) instead
  • Always validate parameter names against tool documentation (filetype vs file_type)
Câu hỏi thường gặp

Tùy chọn layout làm gì?

Nó giữ khoảng cách và cấu trúc gốc của trang càng nhiều càng tốt.

Nếu thiếu pdftotext thì sao?

Hướng dẫn gợi ý một thư viện Python, như pdfplumber, làm phương án dự phòng.

Dữ liệu nhị phân được nhận ra thế nào?

Hướng dẫn chú ý các dấu hiệu như byte rỗng, kết quả rất ngắn hoặc tiêu đề hình ảnh.

Chi tiết

Mô tả gốc của tác giả

Extract text from PDFs using pdftotext when read_file returns binary data

repo: HKUDS/AgentPilot

Độ phổ biến

Sao GitHub
7.8k
Vị trí xếp hạng
#12931
Giấy phép
MIT
Cập nhật
1/11/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự