pdf-read-file-fallback
Extract text from PDFs using pdftotext when read_file returns binary data
- License 许可
- MIT
- 所属仓库
- AG064/OpenSpace
- 最近更新
- 2026-10-08
- Stars
- ★ 0
- Forks
- 0
01 前言 Front Matter
- name
- pdf-read-file-fallback
- description
- Extract text from PDFs using pdftotext when read_file returns binary data
02 资源文件 1
- 其他.skill_id36 B
03 SKILL.md 正文
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
pdftotextis part of thepoppler-utilspackage on most Linux systems- For scanned/image-only PDFs, consider OCR tools (tesseract) instead
- Always validate parameter names against tool documentation (
filetypevsfile_type)
04 同仓库更多 Skills
write-special-chars-ts-check
Workaround for write_file failures caused by special characters (apostrophes, backticks, template literals) using a Python heredoc script, plus a reliable TypeScript type-checking step that filters output to only the newly-written files to avoid being misled by pre-existing errors.
write-special-chars-readback
Workaround for write_file failures caused by special characters (apostrophes, backticks, template literals) using a Python heredoc script, with mandatory post-write verification via read_file and cleanup of /tmp helper scripts.
write-special-chars-heredoc
Workaround for write_file failures caused by special characters (apostrophes, backticks, template literals) by using a Python heredoc script executed via shell.
worldmonitor-reference
Curated architecture index of the WorldMonitor open-source dashboard. Reference for extracting patterns — vanilla TypeScript, Panel class hierarchy, service layer, CSS grid, API edge functions, refresh scheduler, and 60+ data source integrations.
virtual-list
Implement efficient virtual scrolling for rendering large lists with DOM recycling, chunk-based rendering, and performance optimizations
verify-directory-structure
Diagnose and resolve file access issues by first verifying the directory structure.