---
name: pdf-text-extraction-9424c5-ab4d977fdba0
description: Extract text from PDF files using pdftotext when read_file returns binary data
---

# PDF Text Extraction via pdftotext

## Problem

When using `read_file` on PDF documents, the function may return binary image data or garbled content instead of readable text. This occurs because PDFs can contain scanned images or complex binary structures that `read_file` cannot properly parse as text.

## Solution

Use the `pdftotext` command-line utility via `run_shell` to extract clean text content from PDF files.

## Steps

### 1. Verify PDF file exists

```python
import os

pdf_path = "path/to/document.pdf"
if not os.path.exists(pdf_path):
    raise FileNotFoundError(f"PDF not found: {pdf_path}")
```

### 2. Extract text using pdftotext

```python
from tools import run_shell

# Extract text to stdout
result = run_shell(command=f"pdftotext '{pdf_path}' -", timeout=60)
pdf_text = result.stdout

# Alternative: extract to a temporary file
temp_txt = "/tmp/extracted.txt"
run_shell(command=f"pdftotext '{pdf_path}' '{temp_txt}'", timeout=60)
with open(temp_txt, 'r') as f:
    pdf_text = f.read()
```

### 3. Handle parameter naming carefully

When calling `read_file`, be aware of the parameter name:
- Use `filetype="pdf"` (not `file_type`)
- Some tool implementations may use different parameter names

```python
# Correct parameter usage
content = read_file(file_path="doc.pdf", filetype="pdf")

# If this returns binary/garbled data, fall back to pdftotext
```

## Common pdftotext Options

| Option | Description |
|--------|-------------|
| `-` | Output to stdout |
| `-layout` | Maintain original layout |
| `-f <n>` | Start from page n |
| `-l <n>` | End at page n |
| `-q` | Quiet mode |

Example with options:
```python
result = run_shell(command=f"pdftotext -layout -q '{pdf_path}' -", timeout=60)
```

## Error Handling

```python
from tools import run_shell

def extract_pdf_text(pdf_path):
    """Extract text from PDF using pdftotext with error handling."""
    import os
    
    if not os.path.exists(pdf_path):
        raise FileNotFoundError(f"PDF not found: {pdf_path}")
    
    result = run_shell(command=f"pdftotext '{pdf_path}' -", timeout=60)
    
    if result.returncode != 0:
        raise RuntimeError(f"pdftotext failed: {result.stderr}")
    
    return result.stdout.strip()
```

## When to Use This Pattern

- `read_file` returns binary data, garbled text, or image content for a PDF
- You need searchable/processable text from PDF documents
- The PDF contains text (not just scanned images - for those, consider OCR tools)

## Prerequisites

- `pdftotext` must be installed (part of `poppler-utils` on Debian/Ubuntu, `poppler` on macOS via Homebrew)
- Verify availability: `run_shell(command="which pdftotext")`

---
Source attribution: AG064
Original project: https://github.com/AG064/OpenSpace/blob/ff671ef61ed6587c49bd54c81cdc751c0b99e363/gdpval_bench/skills/pdf-text-extraction-9424c5/SKILL.md
License: MIT
License information: https://opensource.org/license/mit
Original upstream instructions; downloaded from Dave AI.

## Upstream license notice

MIT License

Copyright (c) 2026 ✨Data Intelligence Lab@HKU✨

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.



# Dave AI resource metadata

Creator / publisher: AG064
Original project: https://github.com/AG064/OpenSpace/blob/ff671ef61ed6587c49bd54c81cdc751c0b99e363/gdpval_bench/skills/pdf-text-extraction-9424c5/SKILL.md
Attribution basis: Source repository record

Fields of work: Data & Analytics, Development & Engineering
Roles: Backend Developer, Data Analyst, QA / Test Engineer
Specialties: Backend
Tools: Pdftotext, Run_Shell
Country applicability: Country not specified
Classification basis: Source catalog labels
Instruction SHA-256: e1c6b57a466060fed01a0439ae02d8dab0397866836462f61ef6d9d86d541bd4
