Lộ trình học›Claude API›Building with the Claude API Mở bản đồ

6.43 · PDF support — Đọc tài liệu thẳng từ PDF

Bài viếtTrung cấp15 phút đọc

Thời xưa: PDF → OCR → text → send Claude. Nhiều bước, mất quality. Hiện tại: PDF gửi thẳng. Claude hiểu cả text, chart, table, image trong PDF.

Sau bài này, bạn sẽ
  1. Gửi PDF file cho Claude qua base64 hoặc URL
  2. Claude tự extract: text, hình, chart, bảng — không cần OCR tiền xử lý
  3. Biết limits và khi nào dùng PDF vs chunking manually
  4. Case studies: contract review, financial report analysis

Mở đầu: PDF là "first-class citizen"

Thay đổi game cho:

  • Legal: contract review
  • Finance: 10-K, earnings report
  • Research: academic papers
  • HR: CV parsing

Cách gửi PDF

Base64

import base64

with open("earth.pdf", "rb") as f:
    file_bytes = base64.standard_b64encode(f.read()).decode("utf-8")

messages = [{
    "role": "user",
    "content": [
        {
            "type": "document",  # ← không phải "image"
            "source": {
                "type": "base64",
                "media_type": "application/pdf",
                "data": file_bytes
            }
        },
        {
            "type": "text",
            "text": "Summarize this document in 3 bullet points."
        }
    ]
}]

response = client.messages.create(
    model="claude-sonnet-4-6-20260205",
    max_tokens=2000,
    messages=messages
)

URL

{
    "type": "document",
    "source": {
        "type": "url",
        "url": "https://example.com/paper.pdf"
    }
}

So với image

Image blockDocument (PDF) block
Type"image""document"
Media typeimage/png, image/jpgapplication/pdf
ContentSingle imageMulti-page document
ExtractionVisual contentText + images + tables + structure

PDF block is special — Claude "page-through" document.

Limits

  • Max size: 32MB per PDF (check docs cập nhật)
  • Max pages: 100 pages
  • Token cost: Depends on content — text cheap, images expensive

Quy tắc ngón cái

PagesEstimated tokens
1 text page~500 tokens
10 page report~5,000 tokens
100 page paper~50,000 tokens
Pages với heavy images2-3x

Claude extract được gì

Text

Full text, preserve structure (headers, lists, paragraphs).

Tables

Data + relationships:

Q1 revenue: $100M (+15% YoY)
Q2 revenue: $120M (+20% YoY)

Charts / graphs

Interpret visual. Ví dụ bar chart → Claude mô tả trend, values.

Images / diagrams

Describe picture embedded in PDF.

Footnotes, citations

Preserve cross-reference.

Ví dụ: Financial report analysis

with open("apple_10k_2025.pdf", "rb") as f:
    pdf_bytes = base64.standard_b64encode(f.read()).decode("utf-8")

messages = [{
    "role": "user",
    "content": [
        {
            "type": "document",
            "source": {
                "type": "base64",
                "media_type": "application/pdf",
                "data": pdf_bytes
            }
        },
        {
            "type": "text",
            "text": """Analyze this 10-K report. Extract:

1. Total revenue (current year + YoY growth)
2. Top 3 business segments by revenue
3. Key risks mentioned (3 most important)
4. Cash position
5. R&D spending

Output as JSON."""
        }
    ]
}]

response = chat(messages)

Claude read 300-page 10-K, extract structured data.

Ví dụ: Contract review

prompt = """Review this contract. For each clause, rate:
- Favorable / Neutral / Unfavorable (for our side as licensee)
- Suggest redline if needed

Focus on:
1. Payment terms
2. Termination
3. IP ownership
4. Liability limits
5. Confidentiality
"""

Paralegals tiết kiệm 4-8 giờ/contract.

Ví dụ: Research paper summarization

prompt = """Read this research paper. Output:

1. One-sentence tl;dr
2. Key contributions (3 bullets)
3. Methodology summary (4-6 sentences)
4. Limitations mentioned
5. Open questions for future research

Format: Markdown."""

Academic tool. Process 80 papers/literature review in 4 hours.

Multi-PDF analysis

content = [
    {"type": "document", "source": {"type": "base64", "media_type": "application/pdf", "data": pdf1}},
    {"type": "document", "source": {"type": "base64", "media_type": "application/pdf", "data": pdf2}},
    {"type": "document", "source": {"type": "base64", "media_type": "application/pdf", "data": pdf3}},
    {"type": "text", "text": "Compare methodologies across 3 papers. Highlight contradictions."}
]

Powerful cho literature review, contract compare.

When PDF direct vs chunking

PDF direct (Claude reads whole thing):

  • Small-medium PDFs (<100 pages)
  • Need understanding of structure
  • Charts/images matter

Manual chunking (RAG — Module 7):

  • Large corpus (1000+ docs)
  • Vector search across documents
  • User queries need retrieval

Hybrid: Search (RAG) → identify top 3 docs → send each as PDF to Claude for deep analysis.

Prompt caching cho PDF

PDF ~ 50K tokens. Same PDF, multiple queries → cache = 90% cost saving.

messages = [{
    "role": "user",
    "content": [
        {
            "type": "document",
            "source": {"type": "base64", ...},
            "cache_control": {"type": "ephemeral"}  # ← cache PDF
        },
        {"type": "text", "text": "First question..."}
    ]
}]

Second call với cùng PDF + khác question = read from cache. Details ở bài 6.47-6.49.

Anti-patterns

Gửi scanned poor-quality PDF

PDF pages là low-res images → Claude hallucinate text.

Fix: Improve scan quality hoặc OCR tiền xử lý với good tool.

PDF > 100 pages

Exceed limit → error.

Fix: Split PDF into chunks. Hoặc RAG approach.

Không cache PDF cho repeated query

Same PDF, 10 questions → 10x cost.

Fix: Enable prompt caching.

Prompt vague cho dense PDF

"Summarize" PDF 300 trang → generic summary.

Fix: Specific prompt: "Extract these 5 fields...", "Compare X vs Y...".

Áp dụng ngay

Bài tập 1: Analyze 1 PDF (20 phút)

Download PDF research paper (arxiv.org). Gửi Claude, yêu cầu:

  • Tl;dr 1 câu
  • 3 key contributions
  • Methodology
  • Limitations

So sánh output với read manually.

Bài tập 2: Multi-PDF compare (20 phút)

2 contract template (online templates free). Send both, ask "find differences".

Observe Claude analyze side-by-side.

Tóm tắt bài học

Những điều cốt lõi
  • PDF block type "document" — Claude read first-class.
  • Max 100 pages, 32MB. Check limits cập nhật.
  • Extract everything: text, table, chart, image, structure.
  • Use for: contract, financial report, research paper, deep doc analysis.
  • RAG (Module 7) khi scale 1000+ docs. PDF direct cho <100 pages focused.
Kiểm tra nhanh2 câu hỏi · chọn đáp án để xem giải thích
    Bạn đã hoàn thành bài này chưa?Tiến độ lưu trên trình duyệt của bạn.