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

pdftotext-fallback

What it does

This skill is a recovery method for PDF text extraction using pdftotext from the shell. It is for people whose PDF reading tools give unusable results.

When to use it

  • ✓When reading a PDF returns binary or image data.
  • ✓When Python PDF extraction in the sandbox fails or returns garbled text.
  • ✓When I need reliable text from a PDF and a simple command should do it.

Skill link

https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/pdftotext-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/pdftotext-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 sandbox gives me garbled text from my PDF. Please use pdftotext from the shell to get the real text.”

5 new skills a week. 1 minute to read.

How it works

The AI runs pdftotext from the shell and prints the output to the screen, using the layout option for complex pages or writing to a file for large documents.

Technical content (original SKILL.md)

Original text by the author, straight from GitHub.

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
FAQ

Why is pdftotext considered reliable?

The guide describes it as a mature command line tool that handles many PDF edge cases.

What does the dash do?

It sends the extracted text to the screen so the AI can capture it.

What is the option for large PDFs?

The guide suggests extracting to a file rather than to the screen.

Details

Original description by the author

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

repo: HKUDS/AgentPilot

Popularity

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

Is this skill useful?

0

Sign in to vote.

Comments

  • …

More skills from this repository

Similar skills