Claude Platform Docs
Messages處理檔案

PDF 支援

使用 Claude 處理 PDF:從您的文件中擷取文字、分析圖表並理解視覺內容。

您可以向 Claude 詢問您所提供的 PDF 中的任何文字、圖片、圖表和表格。一些範例使用情境:

  • 分析財務報告並理解圖表/表格
  • 從法律文件中擷取關鍵資訊
  • 協助文件翻譯
  • 將文件資訊轉換為結構化格式

開始之前

檢查 PDF 需求

Claude 可處理任何標準 PDF。請確保您的請求大小符合以下需求:

需求限制
最大請求大小第一方 Claude API 上為 128 MB(因平台而異;請參閱請求大小限制)
每個請求的最大頁數600(當請求的上下文視窗小於 1M tokens 時為 100)
格式標準 PDF(無密碼/加密)

這兩項限制皆適用於整個請求負載,包括與 PDF 一同傳送的任何其他內容。對於大型 PDF,請考慮使用 Files API 上傳並透過 file_id 引用,以保持請求負載較小。

由於 PDF 支援仰賴 Claude 的視覺能力,因此它受到與其他視覺任務相同的限制與注意事項約束。

支援的平台與模型

所有現行模型皆支援 PDF 處理。

使用 Claude 處理 PDF

傳送您的第一個 PDF 請求

從使用 Messages API 的簡單範例開始。您可以透過三種方式向 Claude 提供 PDF:

  1. 以 URL 引用線上託管的 PDF
  2. 在 document 內容區塊中以 base64 編碼的 PDF
  3. 透過 Files API 的 file_id

選項 1:基於 URL 的 PDF 文件

最簡單的方法是直接從 URL 引用 PDF:

client = anthropic.Anthropic()
message = client.messages.create(
    model="claude-opus-5-5",
    max_tokens=1024,
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "document",
                    "source": {
                        "type": "url",
                        "url": "https://assets.anthropic.com/m/1cd9d098ac3e6467/original/Claude-3-Model-Card-October-Addendum.pdf",
                    },
                },
                {"type": "text", "text": "What are the key findings in this document?"},
            ],
        }
    ],
)

print(message.content)

回應會在 content 中以文字區塊的形式傳回 Claude 的分析,並在 usage 中提供 token 消耗量:

Output
{
  "id": "msg_01Hfp8YuFjQ55VgWbpdHDehB",
  "type": "message",
  "role": "assistant",
  "model": "claude-opus-5-5",
  "content": [
    {
      "type": "text",
      "text": "This document is an addendum to the Claude 3 model card, reporting updated evaluation results. The key findings include..."
    }
  ],
  "stop_reason": "end_turn",
  "usage": {
    "input_tokens": 45000,
    "output_tokens": 300
  }
}

選項 2:Base64 編碼的 PDF 文件

如果您需要從本機系統傳送 PDF,或在沒有可用 URL 的情況下:

import base64
import httpx2

# 首先,載入 PDF 並進行編碼
pdf_url = "https://assets.anthropic.com/m/1cd9d098ac3e6467/original/Claude-3-Model-Card-October-Addendum.pdf"
pdf_data = base64.standard_b64encode(
    httpx2.get(pdf_url, follow_redirects=True).content
).decode("utf-8")

# 替代方式:從本機檔案載入
# with open("document.pdf", "rb") as f:
#     pdf_data = base64.standard_b64encode(f.read()).decode("utf-8")

# 使用 base64 編碼傳送給 Claude
client = anthropic.Anthropic()
message = client.messages.create(
    model="claude-opus-5-5",
    max_tokens=1024,
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "document",
                    "source": {
                        "type": "base64",
                        "media_type": "application/pdf",
                        "data": pdf_data,
                    },
                },
                {"type": "text", "text": "What are the key findings in this document?"},
            ],
        }
    ],
)

print(message.content)

選項 3:Files API

對於您會重複使用的 PDF,或當您想避免編碼開銷時,請使用 Files API:

client = anthropic.Anthropic()

# 上傳 PDF 檔案
with open("/path/to/document.pdf", "rb") as f:
    file_upload = client.files.upload(file=("document.pdf", f, "application/pdf"))

# 在訊息中使用已上傳的檔案
message = client.messages.create(
    model="claude-opus-5-5",
    max_tokens=1024,
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "document",
                    "source": {"type": "file", "file_id": file_upload.id},
                },
                {"type": "text", "text": "What are the key findings in this document?"},
            ],
        }
    ],
)

print(message.content)

PDF 支援的運作方式

當您將 PDF 傳送給 Claude 時,會發生以下步驟:

  1. 系統擷取文件的內容。

    • 系統將文件的每一頁轉換為影像。
    • 每一頁的文字會被擷取出來,並與該頁的影像一同提供。
  2. Claude 同時分析文字和圖片。

    • 文件以文字與影像組合的形式提供以供分析。
    • 這讓使用者能夠針對 PDF 的視覺元素(例如圖表、示意圖及其他非文字內容)尋求見解。
  3. Claude 做出回應,並在相關時引用 PDF 的內容。

    Claude 在回應時可以同時引用文字與視覺內容。您可以透過將 PDF 支援與以下功能整合來進一步提升效能:

估算您的成本

PDF 檔案的 token 數量取決於從文件中擷取的文字總量以及頁數:

  • 文字 token 成本:視內容密度而定,每頁通常使用 1,500–3,000 個 token。適用標準 API 定價,無額外的 PDF 費用。
  • 圖片 token 成本:由於每頁都會轉換為圖片,因此會套用相同的圖片成本計算方式。

您可以使用 token 計數來估算您特定 PDF 的成本。

最佳化 PDF 處理

提升效能

請遵循以下最佳實務以獲得最佳結果:

  • 在請求中將 PDF 放在文字之前
  • 使用標準字型
  • 確保文字清晰易讀
  • 將頁面旋轉至正確的直立方向
  • 在提示中使用邏輯頁碼(來自 PDF 檢視器)
  • 必要時將大型 PDF 分割成多個區塊
  • 針對重複分析啟用提示快取

擴展您的實作

對於大量處理,請考慮以下方法:

使用提示快取

使用「prompt caching」(提示快取)快取 PDF,以提升重複查詢的效能:

import base64
import httpx2

# 首先,載入 PDF 並進行編碼
pdf_url = "https://assets.anthropic.com/m/1cd9d098ac3e6467/original/Claude-3-Model-Card-October-Addendum.pdf"
pdf_data = base64.standard_b64encode(
    httpx2.get(pdf_url, follow_redirects=True).content
).decode("utf-8")

# 使用已快取的文件建立訊息
client = anthropic.Anthropic()
message = client.messages.create(
    model="claude-opus-5-5",
    max_tokens=1024,
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "document",
                    "source": {
                        "type": "base64",
                        "media_type": "application/pdf",
                        "data": pdf_data,
                    },
                    "cache_control": {"type": "ephemeral"},
                },
                {
                    "type": "text",
                    "text": "Which model has the highest human preference win rates across each use-case?",
                },
            ],
        }
    ],
)

print(message.content)

處理文件批次

使用 Message Batches API 在單一請求中處理多個 PDF:

import base64
import httpx2

# 首先,載入 PDF 並進行編碼
pdf_url = "https://assets.anthropic.com/m/1cd9d098ac3e6467/original/Claude-3-Model-Card-October-Addendum.pdf"
pdf_data = base64.standard_b64encode(
    httpx2.get(pdf_url, follow_redirects=True).content
).decode("utf-8")

# 建立一批使用該文件的請求
client = anthropic.Anthropic()
message_batch = client.messages.batches.create(
    requests=[
        {
            "custom_id": "my-first-request",
            "params": {
                "model": "claude-opus-5-5",
                "max_tokens": 1024,
                "messages": [
                    {
                        "role": "user",
                        "content": [
                            {
                                "type": "document",
                                "source": {
                                    "type": "base64",
                                    "media_type": "application/pdf",
                                    "data": pdf_data,
                                },
                            },
                            {
                                "type": "text",
                                "text": "Which model has the highest human preference win rates across each use-case?",
                            },
                        ],
                    }
                ],
            },
        },
        {
            "custom_id": "my-second-request",
            "params": {
                "model": "claude-opus-5-5",
                "max_tokens": 1024,
                "messages": [
                    {
                        "role": "user",
                        "content": [
                            {
                                "type": "document",
                                "source": {
                                    "type": "base64",
                                    "media_type": "application/pdf",
                                    "data": pdf_data,
                                },
                            },
                            {
                                "type": "text",
                                "text": "Extract 5 key insights from this document.",
                            },
                        ],
                    }
                ],
            },
        },
    ]
)

print(message_batch)

批次以非同步方式處理。若要檢查進度並在處理結束後擷取結果,請參閱批次處理。

後續步驟

Claude 的視覺能力使其能夠處理和分析圖片,為多模態互動開啟令人興奮的可能性。

在 Claude Cookbook 食譜中探索 PDF 處理的實用範例。

查看 PDF 支援的完整 API 文件。

可用性

PDF 支援在所有模型上可供使用,且具備零資料保留和HIPAA 就緒資格。

API 支援

用法

永遠開啟

狀態可用
雲端平台
狀態可用
雲端平台
file_id 來源需要託管於 Anthropic 的部署。
狀態可用
雲端平台
僅支援 Base64 來源。
狀態可用
雲端平台
僅支援 Base64 來源。

用法

永遠開啟

狀態可用
雲端平台
  • 所有支援的平台

資料處理

資料處理概覽

適用端點

  • /v1/messages
零資料保留符合資格

不含 涵蓋的模型

HIPAA符合資格

HIPAA 適用資格僅適用於透過 Messages API 內嵌傳送的 PDF,不適用於透過 Files API 傳送的 PDF。

這個頁面有幫助嗎?