Langsung ke konten
Skillpedia
PDF, Word & slide7.8k ยท #12925

pdf-extraction-fallbacks

Fungsinya

Skill ini adalah alur kerja multi-metode untuk membaca PDF, dengan validasi sebelum parsing. Skill ini cocok untuk orang yang membutuhkan teks PDF dari sumber yang kadang gagal.

Kapan dipakai

  • โœ“Saat sumber PDF mungkin rusak atau digantikan oleh halaman galat.
  • โœ“Saat ekstraksi teks dari PDF gagal pada metode pertama.
  • โœ“Saat saya ingin catatan tentang metode mana yang berhasil untuk pekerjaan berikutnya.

Link skill

https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/pdf-extraction-fallbacks/SKILL.md

Salin lalu tempelkan ke AI Anda (Claude, ChatGPT, atau Cursor) dan minta AI mengikuti instruksinya.

Cara pakai

Cara paling mudah: salin pesan ini dan tempelkan ke AI Anda. AI akan membaca skill dan mengikuti instruksinya.

Pesan siap tempel

Gunakan skill ini untuk membantu saya: https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/pdf-extraction-fallbacks/SKILL.md Baca file SKILL.md di link tersebut dan ikuti instruksinya untuk tugas saya.
Mau AI Anda menemukan skill seperti ini sendiri?Pasang MCP Skillpedia gratis: cukup satu baris, dan AI mencari di antara 15.000+ skill sebelum tiap tugas.Pasang dalam 1 menit โ†’

Contoh permintaan ke AI

โ€œAmbil teks dari tautan PDF ini. Periksa apakah unduhannya valid, coba beberapa metode, dan beri tahu saya mana yang berhasil.โ€

5 skill baru setiap minggu. 1 menit baca.

Cara kerjanya

AI mengunduh file, memeriksa ukuran dan tanda tangan PDF-nya, lalu menjalankan metode ekstraksi satu per satu dan mencatat metode yang berhasil.

Konten teknis (SKILL.md asli)

Teks asli dari pembuat, dalam bahasa Inggris, langsung dari GitHub.

PDF Extraction with Multi-Fallback Strategy

Purpose

When extracting text from PDFs (especially regulatory documents, handbooks, or protected content), single-method approaches often fail due to JavaScript protection, CORS restrictions, encoding issues, or corrupted downloads. This skill provides a robust multi-fallback workflow that detects failures early and tries sequential extraction methods.

Core Pattern

  1. Download with validation - Check file size and content sanity immediately
  2. Sequential extraction attempts - Try multiple tools in order of reliability
  3. Early failure detection - Don't proceed with obviously corrupt files
  4. Document fallback path - Log which method succeeded for future reference

Step-by-Step Instructions

Step 1: Download and Validate

Before attempting extraction, validate the downloaded file:

# Download the PDF
curl -L -o document.pdf "$URL"

# Check file size (reject if < 1KB - likely error page)
FILE_SIZE=$(stat -f%z document.pdf 2>/dev/null || stat -c%s document.pdf 2>/dev/null)
if [ "$FILE_SIZE" -lt 1024 ]; then
    echo "ERROR: File too small ($FILE_SIZE bytes) - likely not a valid PDF"
    # Check if it's an HTML error page
    head -c 200 document.pdf | grep -i "<html\|<!doctype\|error\|access denied" && \
        echo "Detected HTML error page instead of PDF"
    exit 1
fi

# Check PDF magic bytes
HEAD_BYTES=$(head -c 4 document.pdf)
if [ "$HEAD_BYTES" != "%PDF" ]; then
    echo "ERROR: File does not start with PDF magic bytes"
    head -c 100 document.pdf
    exit 1
fi

Step 2: Primary Extraction (pdftotext)

# Try pdftotext first (fastest, most reliable for simple PDFs)
if command -v pdftotext &> /dev/null; then
    pdftotext -layout document.pdf output.txt 2>/dev/null
    if [ -s output.txt ]; then
        WORD_COUNT=$(wc -w < output.txt)
        if [ "$WORD_COUNT" -gt 50 ]; then
            echo "SUCCESS: pdftotext extracted $WORD_COUNT words"
            exit 0
        fi
    fi
fi

Step 3: Fallback 1 (PyMuPDF/fitz)

# Try PyMuPDF - handles more complex PDFs
import fitz  # pymupdf

try:
    doc = fitz.open("document.pdf")
    text = ""
    for page in doc:
        text += page.get_text()
    
    if len(text.strip()) > 500:  # Sanity check
        with open("output.txt", "w") as f:
            f.write(text)
        print(f"SUCCESS: PyMuPDF extracted {len(text)} characters")
    else:
        print("WARNING: PyMuPDF extraction too short, trying next method")
except Exception as e:
    print(f"PyMuPDF failed: {e}")

Step 4: Fallback 2 (pdfplumber)

# Try pdfplumber - better for tables and structured content
import pdfplumber

try:
    text = ""
    with pdfplumber.open("document.pdf") as pdf:
        for page in pdf.pages:
            page_text = page.extract_text()
            if page_text:
                text += page_text + "\n"
    
    if len(text.strip()) > 500:
        with open("output.txt", "w") as f:
            f.write(text)
        print(f"SUCCESS: pdfplumber extracted {len(text)} characters")
    else:
        print("WARNING: pdfplumber extraction too short")
except Exception as e:
    print(f"pdfplumber failed: {e}")

Step 5: Handle JavaScript-Protected Pages

If all methods fail, the PDF may be JavaScript-protected:

# Check for JavaScript in PDF
import fitz

doc = fitz.open("document.pdf")
has_js = False
for page in doc:
    if page.get_java_script():
        has_js = True
        break

if has_js:
    print("WARNING: PDF contains JavaScript - may be protected")
    # Try rendering pages as images and OCR (requires additional tools)
    # Or try alternative download source

Step 6: Alternative Sources

If the primary URL fails:

  • Try alternative domains (e.g., .gov mirrors, archive.org)
  • Check if the document is available via API
  • Look for HTML version of the same content
  • Search for the document title + "pdf" to find mirrors

Decision Tree

Download PDF
    โ”‚
    โ”œโ”€โ†’ File < 1KB? โ†’ REJECT (likely error page)
    โ”œโ”€โ†’ No %PDF header? โ†’ REJECT (not a PDF)
    โ”‚
    โ””โ”€โ†’ Valid PDF
         โ”‚
         โ”œโ”€โ†’ pdftotext โ†’ >50 words? โ†’ SUCCESS
         โ”‚              โ””โ”€โ†’ Try next
         โ”‚
         โ”œโ”€โ†’ PyMuPDF โ†’ >500 chars? โ†’ SUCCESS
         โ”‚             โ””โ”€โ†’ Try next
         โ”‚
         โ”œโ”€โ†’ pdfplumber โ†’ >500 chars? โ†’ SUCCESS
         โ”‚                 โ””โ”€โ†’ Try next
         โ”‚
         โ””โ”€โ†’ All failed โ†’ Check for JS protection, try alternat
Pertanyaan umum

Bagaimana file diperiksa sebelum diurai?

Panduan ini memeriksa ukuran file dan apakah awal file tampak seperti PDF, bukan halaman HTML.

Mengapa metode yang berhasil dicatat?

Panduan ini menyatakan bahwa hal itu membantu pekerjaan berikutnya memakai metode yang sudah terbukti berhasil.

Apakah unduhan yang buruk bisa dilewati?

Bisa. Panduan ini berhenti lebih awal ketika file jelas bukan PDF yang valid.

Detail

Deskripsi asli dari pembuat

Multi-fallback PDF/text extraction with early failure detection and sequential tool fallbacks

repo: HKUDS/AgentPilot

Popularitas

Bintang GitHub
7.8k
Peringkat
#12925
Lisensi
MIT
Diperbarui
1/11/2026

Apakah skill ini berguna?

0

Masuk untuk memberi suara.

Komentar

  • โ€ฆ

Skill lain dari repositori ini

Skill serupa