您現在可以詢問 Claude 關於您提供的 PDF 中的任何文字、圖片、圖表和表格。一些範例用例:

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

開始之前

檢查 PDF 要求

Claude 可以處理任何標準 PDF。但是,在使用 PDF 支援時,您應確保您的請求大小符合以下要求:

要求限制
最大請求大小32MB
每個請求的最大頁數100
格式標準 PDF(無密碼/加密)

請注意,這兩個限制都適用於整個請求負載,包括與 PDF 一起發送的任何其他內容。

由於 PDF 支援依賴於 Claude 的視覺能力,因此它受到與其他視覺任務相同的限制和考量

支援的平台和模型

PDF 支援目前通過直接 API 訪問和 Google Vertex AI 支援以下模型:

  • Claude Opus 4 (claude-opus-4-20250514)
  • Claude Sonnet 4 (claude-sonnet-4-20250514)
  • Claude Sonnet 3.7 (claude-3-7-sonnet-20250219)
  • Claude Sonnet 3.5 模型 (claude-3-5-sonnet-20241022, claude-3-5-sonnet-20240620)
  • Claude Haiku 3.5 (claude-3-5-haiku-20241022)

此功能將很快在 Amazon Bedrock 上支援。


使用 Claude 處理 PDF

發送您的第一個 PDF 請求

讓我們從使用 Messages API 的簡單示例開始。您可以通過三種方式向 Claude 提供 PDF:

  1. 作為對線上託管的 PDF 的 URL 引用
  2. 作為 document 內容區塊中的 base64 編碼 PDF
  3. 通過 Files APIfile_id

選項 1:基於 URL 的 PDF 文件

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

 curl https://api.anthropic.com/v1/messages \
   -H "content-type: application/json" \
   -H "x-api-key: $ANTHROPIC_API_KEY" \
   -H "anthropic-version: 2023-06-01" \
   -d '{
     "model": "claude-opus-4-20250514",
     "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?"
         }]
     }]
 }'

選項 2:Base64 編碼的 PDF 文件

如果您需要從本地系統發送 PDF 或當 URL 不可用時:

# Method 1: Fetch and encode a remote PDF
curl -s "https://assets.anthropic.com/m/1cd9d098ac3e6467/original/Claude-3-Model-Card-October-Addendum.pdf" | base64 | tr -d '\n' > pdf_base64.txt

# Method 2: Encode a local PDF file
# base64 document.pdf | tr -d '\n' > pdf_base64.txt

# Create a JSON request file using the pdf_base64.txt content
jq -n --rawfile PDF_BASE64 pdf_base64.txt '{
    "model": "claude-opus-4-20250514",
    "max_tokens": 1024,
    "messages": [{
        "role": "user",
        "content": [{
            "type": "document",
            "source": {
                "type": "base64",
                "media_type": "application/pdf",
                "data": $PDF_BASE64
            }
        },
        {
            "type": "text",
            "text": "What are the key findings in this document?"
        }]
    }]
}' > request.json

# Send the API request using the JSON file
curl https://api.anthropic.com/v1/messages \
  -H "content-type: application/json" \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -d @request.json

選項 3:Files API

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

# First, upload your PDF to the Files API
curl -X POST https://api.anthropic.com/v1/files \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "anthropic-beta: files-api-2025-04-14" \
  -F "file=@document.pdf"

# Then use the returned file_id in your message
curl https://api.anthropic.com/v1/messages \
  -H "content-type: application/json" \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "anthropic-beta: files-api-2025-04-14" \
  -d '{
    "model": "claude-opus-4-20250514", 
    "max_tokens": 1024,
    "messages": [{
      "role": "user",
      "content": [{
        "type": "document",
        "source": {
          "type": "file",
          "file_id": "file_abc123"
        }
      },
      {
        "type": "text",
        "text": "What are the key findings in this document?"
      }]
    }]
  }'

PDF 支援的工作原理

當您向 Claude 發送 PDF 時,會發生以下步驟:

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 分割成塊
  • 啟用提示快取以進行重複分析

擴展您的實施

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

使用提示快取

快取 PDF 以提高重複查詢的性能:

# Create a JSON request file using the pdf_base64.txt content
jq -n --rawfile PDF_BASE64 pdf_base64.txt '{
    "model": "claude-opus-4-20250514",
    "max_tokens": 1024,
    "messages": [{
        "role": "user",
        "content": [{
            "type": "document",
            "source": {
                "type": "base64",
                "media_type": "application/pdf",
                "data": $PDF_BASE64
            },
            "cache_control": {
              "type": "ephemeral"
            }
        },
        {
            "type": "text",
            "text": "Which model has the highest human preference win rates across each use-case?"
        }]
    }]
}' > request.json

# Then make the API call using the JSON file
curl https://api.anthropic.com/v1/messages \
  -H "content-type: application/json" \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -d @request.json

處理文件批次

使用 Message Batches API 進行高容量工作流程:

# Create a JSON request file using the pdf_base64.txt content
jq -n --rawfile PDF_BASE64 pdf_base64.txt '
{
  "requests": [
      {
          "custom_id": "my-first-request",
          "params": {
              "model": "claude-opus-4-20250514",
              "max_tokens": 1024,
              "messages": [
                {
                    "role": "user",
                    "content": [
                        {
                            "type": "document",
                            "source": {
 "type": "base64",
 "media_type": "application/pdf",
 "data": $PDF_BASE64
                            }
                        },
                        {
                            "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-4-20250514",
              "max_tokens": 1024,
              "messages": [
                {
                    "role": "user",
                    "content": [
                        {
                            "type": "document",
                            "source": {
 "type": "base64",
 "media_type": "application/pdf",
 "data": $PDF_BASE64
                            }
                        },
                        {
                            "type": "text",
                            "text": "Extract 5 key insights from this document."
                        }
                    ]
                }
              ]
          }
      }
  ]
}
' > request.json

# Then make the API call using the JSON file
curl https://api.anthropic.com/v1/messages/batches \
  -H "content-type: application/json" \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -d @request.json

下一步