← All skills
Data https://ai-skills-vault.pages.dev/skills/pdf-document-automation.md

PDF Document Automation

Piles of PDFs into validated rows, with a review loop.

Download .md All skills
Install URLhttps://ai-skills-vault.pages.dev/skills/pdf-document-automation.md
Install prompt tap to copy
Read the skill at https://ai-skills-vault.pages.dev/skills/pdf-document-automation.md and install it as a skill in your agent. Follow it exactly, including its checklists. Then ask me the questions it requires and do the work.

PDF is a container, not a format. The first job is always: is there a text layer, or is this a photo of paper?

When to use this skill

Stage 1 - Classify before extracting

For each PDF, detect:

  1. Text layer present? Extract with PyMuPDF; if a page yields under 50 characters, it needs OCR.
  2. Digital vs scanned vs hybrid. Hybrid is common: a scanned signature page inside a digital contract.
  3. Layout family. Invoices from 40 vendors are 40 layouts. Cluster them; one parser per family beats one parser for everything.
  4. Form fields (AcroForm) - read them directly, it is free and exact.
  5. Encryption - try an empty owner password; if it fails, ask for credentials, never brute force.
  6. Page count and size - a 400-page file needs streaming and a time budget, not a bigger prompt.

Log a classification summary before writing any extraction logic.

Stage 2 - Text and table extraction

import fitz  # PyMuPDF
doc = fitz.open(path)
for page in doc:
    text = page.get_text("text")
    if len(text.strip()) < 50:
        pass  # route to OCR
    tables = page.find_tables()  # keep cell geometry, not just the text

Stage 3 - OCR when needed

Stage 4 - Schema and extraction strategy

Define the target first:

class LineItem(BaseModel):
    description: str
    qty: float | None
    unit_price: float | None
    amount: float

class Invoice(BaseModel):
    vendor: str
    invoice_no: str
    invoice_date: date
    currency: str
    total: float
    tax: float | None
    line_items: list[LineItem]
    source_page: int

Strategy ladder, cheapest first:

  1. Regex / anchor rules for stable layouts and form fields. Deterministic, free, auditable.
  2. Table parser + column mapping for tabular documents.
  3. LLM extraction with the schema for variable layouts and messy prose. Always with temperature=0 and structured output.
  4. Hybrid: rules for the fields that are always in the same place, LLM for the rest. This is the production answer.

For every extracted field, keep a confidence and the source_quote (the exact text it came from). Verify the quote exists in the page text - that single check catches most hallucinations.

Stage 5 - Validation (the part that creates trust)

  1. Schema validation, then business rules: dates in range, total == sum(line_items) + tax within 1%, currency in the allowlist, invoice number unique.
  2. Cross-field: invoice date not after due date; line item sum not wildly off the stated total.
  3. Duplicate detection by document hash and by (vendor, invoice_no).
  4. Arithmetic: recompute every total. A mismatch is a finding, not a rounding error.
  5. Confidence gate: below threshold → human review queue, never auto-post.

Stage 6 - The review loop

Stage 7 - Output and delivery

Stage 8 - Scale and operations

Quality checklist

Copy-ready agent prompt

Read the skill at the URL above. I will give you sample PDFs and the fields I need. First classify the documents (text layer, layout families, OCR need) and show me the summary. Then define the pydantic schema, write the extraction ladder (rules first, LLM only where needed) with source quotes and confidence, add the validation rules including arithmetic checks, and produce the Excel export plus a rejects CSV. Run it on my samples and report field-level accuracy honestly.
Copied