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

pdftotext-fallback

Skill này làm gì

Skill này là phương pháp khôi phục để trích xuất văn bản PDF bằng pdftotext từ shell. Dành cho người có công cụ đọc PDF cho kết quả không dùng được.

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 trích xuất PDF bằng Python trong sandbox thất bại hoặc trả về văn bản lỗi.
  • ✓Khi tôi cần văn bản đáng tin cậy từ PDF và một lệnh đơn giản là đủ.

Liên kết skill

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

“Sandbox trả về văn bản bị lỗi từ PDF của tôi. Hãy dùng pdftotext từ shell để lấy văn bản thật.”

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

Cách hoạt động

AI chạy pdftotext từ shell và in đầu ra ra màn hình, dùng tùy chọn layout cho các trang phức tạp hoặc ghi vào tệp với tài liệu lớ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.

pdftotext Fallback for PDF Text Extraction

When to Use

Apply this pattern when:

  • read_file on a PDF returns binary/image data instead of readable text
  • Python-based PDF extraction (PyMuPDF, pdfplumber, etc.) in execute_code_sandbox fails or returns garbled content
  • You need reliable text extraction from PDF files as a recovery strategy

Why This Works

The pdftotext utility (from poppler-utils) is a mature, command-line tool that handles many PDF edge cases that confuse Python libraries or the read_file tool. It's pre-installed on most Linux systems and provides consistent, reliable text extraction.

Steps

Step 1: Detect the Failure

Recognize extraction failure when:

  • read_file returns binary content, image data, or garbled text
  • Error messages indicate encoding issues or unsupported formats
  • Python PDF libraries in sandbox fail with import errors or extraction failures

Step 2: Use pdftotext via run_shell

Extract to stdout (recommended for quick extraction):

pdftotext /path/to/file.pdf -

The - argument outputs directly to stdout for easy capture in your tool response.

Example:

run_shell("pdftotext document.pdf -")

Step 3: Handle Complex PDFs

Preserve layout (maintains original formatting):

pdftotext -layout /path/to/file.pdf -

Extract to a file (for large PDFs):

pdftotext /path/to/file.pdf /path/to/output.txt

Then read the output file with read_file.

Handle encoded text:

pdftotext -enc UTF-8 /path/to/file.pdf -

Step 4: Verify and Continue

  • Check the extracted text for completeness
  • If text is still garbled, the PDF may be image-based (scanned) - consider OCR tools
  • Proceed with your task using the extracted text

Code Examples

Basic extraction:

# Simple text extraction
text = run_shell("pdftotext document.pdf -")

With error handling:

# Try extraction, check for success
result = run_shell("pdftotext document.pdf - && echo 'SUCCESS' || echo 'FAILED'")

Multi-page PDF with layout:

# Preserve tables and formatting
text = run_shell("pdftotext -layout document.pdf -")

Troubleshooting

Issue Solution
pdftotext: command not found Install poppler-utils: apt-get install poppler-utils
Garbled/special characters Try -enc UTF-8 or -enc ASCII7
Missing formatting Use -layout flag
Scanned PDF (no text) Requires OCR (e.g., tesseract), not text extraction
Large PDF timeout Extract to file instead of stdout

Limitations

  • Does not work on image-only (scanned) PDFs without embedded text
  • May lose complex formatting, tables, or embedded objects
  • Requires poppler-utils to be installed (common on Linux, may need installation on other systems)

Related Tools

  • pdfinfo: Get PDF metadata (pages, size, etc.)
  • pdftoppm: Convert PDF pages to images
  • tesseract: OCR for scanned PDFs
Câu hỏi thường gặp

Vì sao pdftotext được coi là đáng tin?

Hướng dẫn mô tả nó là công cụ dòng lệnh chín muồi, xử lý được nhiều trường hợp khó của PDF.

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

Nó gửi văn bản đã trích xuất ra màn hình để AI có thể lấy được.

Tùy chọn cho PDF lớn là gì?

Hướng dẫn gợi ý trích xuất ra một tệp thay vì ra màn hình.

Chi tiết

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

Recover PDF text extraction when read_file returns binary data by using pdftotext via shell

repo: HKUDS/AgentPilot

Độ phổ biến

Sao GitHub
7.8k
Vị trí xếp hạng
#12942
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ự