Skip to content
Skillpedia
PDF, Word & slides7.8k · #12931

pdf-read-file-fallback

What it does

This skill is a fallback for PDFs that the built-in reader returns as image or binary data. It is for people who need the words inside PDFs that the AI cannot read normally.

When to use it

  • ✓When reading a PDF returns image or binary data instead of text.
  • ✓When the PDF is a scanned document or has complex formatting.
  • ✓When I need the text from a PDF in a readable form.

Skill link

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

Copy it and paste it into your AI (Claude, ChatGPT or Cursor), asking it to follow the instructions.

How to use

The easiest way: copy the message and paste it into your AI. It reads the skill and follows the instructions.

Ready-to-paste message

Use this skill to help me: https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/pdf-read-file-fallback/SKILL.md Read the SKILL.md file at that link and follow its instructions for my task.
Want your AI to find skills like this on its own?Install the free Skillpedia MCP: one line, and it searches 15,000+ skills before each task.Install in 1 minute →

Example of what to ask your AI

“The AI only gets binary data from my PDF. Please pull the text out with a command line tool.”

5 new skills a week. 1 minute to read.

How it works

The AI first tries the normal file read, checks whether the result is usable, and if it is not, runs pdftotext with the layout option to get the text.

Technical content (original SKILL.md)

Original text by the author, straight from GitHub.

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)
FAQ

What does the layout option do?

It keeps the original spacing and structure of the page as far as possible.

What if pdftotext is missing?

The guide suggests a Python library, such as pdfplumber, as a backup.

How is binary output spotted?

The guide notes signs such as null bytes, a very short result or image headers.

Details

Original description by the author

Extract text from PDFs using pdftotext when read_file returns binary data

repo: HKUDS/AgentPilot

Popularity

GitHub stars
7.8k
Ranking position
#12931
License
MIT
Updated
11/1/2026

Is this skill useful?

0

Sign in to vote.

Comments

  • …

More skills from this repository

Similar skills