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

pdf-text-extraction-fallback

Skill này làm gì

Skill này giải thích cách trích xuất văn bản PDF bằng pdftotext khi trình đọc tệp tích hợp thất bại. Dành cho người làm việc với PDF có hình ảnh hoặc định dạng phức tạp.

Khi nào nên dùng

  • ✓Khi đọc một PDF trả về dữ liệu nhị phân hoặc hình ảnh.
  • ✓Khi PDF có kèm hình ảnh nhúng hoặc định dạng phức tạp.
  • ✓Khi tôi cần văn bản đọc được từ PDF để dùng trong tác vụ.

Liên kết skill

https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/pdf-text-extraction-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-text-extraction-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

“Trình đọc tệp trả về dữ liệu nhị phân cho PDF của tôi. Hãy kiểm tra các tham số gọi và trích xuất văn bản bằng pdftotext.”

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

Cách hoạt động

AI đảm bảo lệnh đọc dùng đúng tên tham số, phát hiện đầu ra nhị phân, rồi chạy pdftotext để in văn bản ra đầu ra chuẩ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 This Skill

Use this skill when read_file with filetype="pdf" returns binary/image data instead of readable text content. This is a common issue with PDF files that contain embedded images or complex formatting.

Steps

1. Validate Parameters First

Before attempting extraction, ensure you're using the correct parameter name:

  • Use filetype (not file_type) for the read_file function
  • Incorrect parameter names can cause silent failures
# Correct
read_file(filetype="pdf", file_path="document.pdf")

# Incorrect - may fail silently
read_file(file_type="pdf", file_path="document.pdf")

2. Detect Binary Data Issue

After calling read_file, check if the result contains:

  • Garbled/binary characters
  • Image data representations (e.g., b'...' byte strings with non-text content)
  • Unreadable or corrupted-looking content

If yes, proceed with the pdftotext workaround.

3. Extract Text via pdftotext

Use run_shell to call pdftotext, which extracts text directly from PDF files:

# Extract text to stdout
result = run_shell(command="pdftotext /path/to/document.pdf -")
text_content = result.stdout

The - flag tells pdftotext to output to stdout instead of creating a file.

4. Handle Output and Errors

result = run_shell(command="pdftotext /path/to/document.pdf -")

if result.stderr:
    # Check for errors like "pdftotext not found"
    # May need to install poppler-utils
    pass

text_content = result.stdout
# text_content now contains the extracted text

Example Workflow

# Step 1: Try normal read with correct parameters
content = read_file(filetype="pdf", file_path="reference.pdf")

# Step 2: Check if content is readable
if not content or looks_like_binary(content):
    # Step 3: Fall back to pdftotext
    result = run_shell(command="pdftotext reference.pdf -")
    text_content = result.stdout
    
    # Step 4: Verify extraction succeeded
    if result.stderr:
        # Handle error (e.g., install pdftotext)
        pass

Installation Notes

pdftotext is part of the poppler-utils package:

  • Debian/Ubuntu: apt-get install poppler-utils
  • macOS: brew install poppler
  • Many Linux environments: Pre-installed

Alternative: Output to File

If stdout approach has issues, output to a temporary file:

run_shell(command="pdftotext /path/to/document.pdf /tmp/output.txt")
text_content = read_file(filetype="txt", file_path="/tmp/output.txt")

Best Practices

  1. Always validate the filetype parameter spelling before troubleshooting
  2. Check both stdout and stderr from pdftotext
  3. For multi-page PDFs, pdftotext preserves page breaks with form feeds
  4. This method works better for text-based PDFs than image-scanned PDFs
Câu hỏi thường gặp

Vì sao tên tham số quan trọng?

Hướng dẫn nói tên tham số sai có thể gây ra lỗi âm thầm.

Đầu ra nhị phân được nhận ra thế nào?

Nó tìm các ký tự nhị phân hoặc dấu hiệu dữ liệu hình ảnh trong kết quả.

Dấu gạch ngang trong lệnh làm gì?

Nó bảo pdftotext in văn bản ra màn hình thay vì ra một tệp.

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
#12934
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ự