Loading...
  • 构建
  • 管理
  • 模型与定价
  • 客户端 SDK
  • API 参考
Search...
⌘K
Log in
PDF 支持
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Solutions

  • AI agents
  • Code modernization
  • Coding
  • Customer support
  • Education
  • Financial services
  • Government
  • Life sciences

Partners

  • Amazon Bedrock
  • Google Cloud's Vertex AI

Learn

  • Blog
  • Courses
  • Use cases
  • Connectors
  • Customer stories
  • Engineering at Anthropic
  • Events
  • Powered by Claude
  • Service partners
  • Startups program

Company

  • Anthropic
  • Careers
  • Economic Futures
  • Research
  • News
  • Responsible Scaling Policy
  • Security and compliance
  • Transparency

Learn

  • Blog
  • Courses
  • Use cases
  • Connectors
  • Customer stories
  • Engineering at Anthropic
  • Events
  • Powered by Claude
  • Service partners
  • Startups program

Help and security

  • Availability
  • Status
  • Support
  • Discord

Terms and policies

  • Privacy policy
  • Responsible disclosure policy
  • Terms of service: Commercial
  • Terms of service: Consumer
  • Usage policy
构建/文件处理

PDF 支持

使用 Claude 处理 PDF。从您的文档中提取文本、分析图表并理解视觉内容。

This feature is eligible for Zero Data Retention (ZDR). When your organization has a ZDR arrangement, data sent through this feature is not stored after the API response is returned.

您可以向 Claude 询问您提供的 PDF 中的任何文本、图片、图表和表格。一些示例用例包括:

  • 分析财务报告并理解图表/表格
  • 从法律文件中提取关键信息
  • 文档翻译协助
  • 将文档信息转换为结构化格式

开始之前

检查 PDF 要求

Claude 适用于任何标准 PDF。确保您的请求大小符合以下要求:

要求限制
最大请求大小32 MB (因平台而异)
每个请求的最大页数600(对于具有 200k 令牌上下文窗口的模型为 100)
格式标准 PDF(无密码/加密)

两个限制都适用于整个请求有效负载,包括与 PDF 一起发送的任何其他内容。对于大型 PDF,请考虑使用 Files API 上传并通过 file_id 引用,以保持请求有效负载较小。

密集 PDF(许多小字体页面、复杂表格或大量图形)可能会在达到页面限制之前填满上下文窗口。即使使用 Files API,大型 PDF 的请求也可能在达到页面限制之前失败。尝试将文档分成几个部分;对于大型文件,由于每个页面都作为图像处理,对嵌入式图像进行下采样也可以帮助。

由于 PDF 支持依赖于 Claude 的视觉功能,它受到与其他视觉任务相同的 限制和注意事项。

支持的平台和模型

PDF 支持目前通过直接 API 访问和 Google Vertex AI 支持。所有 活跃模型 都支持 PDF 处理。

PDF 支持现在在 Amazon Bedrock 上可用,具有以下注意事项:

Amazon Bedrock PDF 支持

通过 Amazon Bedrock 的 Converse API 使用 PDF 支持时,有两种不同的文档处理模式:

重要: 要在 Converse API 中访问 Claude 的完整视觉 PDF 理解功能,您必须启用引用。如果未启用引用,API 将回退到仅基本文本提取。了解更多关于 使用引用。

文档处理模式

  1. Converse Document Chat(原始模式 - 仅文本提取)

    • 从 PDF 提供基本文本提取
    • 无法分析 PDF 中的图像、图表或视觉布局
    • 对于 3 页 PDF 大约使用 1,000 个令牌
    • 在未启用引用时自动使用
  2. Claude PDF Chat(新模式 - 完整视觉理解)

    • 提供 PDF 的完整视觉分析
    • 可以理解和分析图表、图形、图像和视觉布局
    • 将每个页面同时作为文本和图像处理以获得全面理解
    • 对于 3 页 PDF 大约使用 7,000 个令牌
    • 需要在 Converse API 中启用引用

关键限制

  • Converse API:视觉 PDF 分析需要启用引用。目前没有选项可以在不启用引用的情况下使用视觉分析(与 InvokeModel API 不同)。
  • InvokeModel API:提供对 PDF 处理的完全控制,无需强制引用。

常见问题

如果客户报告在使用 Converse API 时 Claude 看不到其 PDF 中的图像或图表,他们可能需要启用引用标志。没有它,Converse 会回退到仅基本文本提取。

这是 Converse API 的已知限制。对于需要不带引用的视觉 PDF 分析的应用程序,请考虑改用 InvokeModel API。

对于非 PDF 文件,如 .csv、.xlsx、.docx、.md 或 .txt 文件,请参阅 使用其他文件格式。


使用 Claude 处理 PDF

发送您的第一个 PDF 请求

让我们从使用 Messages API 的简单示例开始。您可以通过三种方式向 Claude 提供 PDF:

  1. 作为对在线托管的 PDF 的 URL 引用
  2. 作为 document 内容块中的 base64 编码 PDF
  3. 通过来自 Files API 的 file_id

选项 1:基于 URL 的 PDF 文档

最简单的方法是直接从 URL 引用 PDF:

client = anthropic.Anthropic()
message = client.messages.create(
    model="claude-opus-4-7",
    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)

选项 2:Base64 编码的 PDF 文档

如果您需要从本地系统发送 PDF 或 URL 不可用时:

import base64
import httpx

# First, load and encode the PDF
pdf_url = "https://assets.anthropic.com/m/1cd9d098ac3e6467/original/Claude-3-Model-Card-October-Addendum.pdf"
pdf_data = base64.standard_b64encode(httpx.get(pdf_url).content).decode("utf-8")

# Alternative: Load from a local file
# with open("document.pdf", "rb") as f:
#     pdf_data = base64.standard_b64encode(f.read()).decode("utf-8")

# Send to Claude using base64 encoding
client = anthropic.Anthropic()
message = client.messages.create(
    model="claude-opus-4-7",
    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()

# Upload the PDF file
with open("document.pdf", "rb") as f:
    file_upload = client.beta.files.upload(file=("document.pdf", f, "application/pdf"))

# Use the uploaded file in a message
message = client.beta.messages.create(
    model="claude-opus-4-7",
    max_tokens=1024,
    betas=["files-api-2025-04-14"],
    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 支持的工作原理

当您向 Claude 发送 PDF 时,会发生以下步骤:

  1. 1

    系统提取文档的内容。

    • 系统将文档的每一页转换为图像。
    • 从每一页提取文本,并与每一页的图像一起提供。
  2. 2

    Claude 分析文本和图像以更好地理解文档。

    • 文档作为文本和图像的组合提供以供分析。
    • 这允许用户询问 PDF 的视觉元素的见解,例如图表、图表和其他非文本内容。
  3. 3

    Claude 响应,在相关时引用 PDF 的内容。

    Claude 可以在响应时引用文本和视觉内容。您可以通过集成 PDF 支持来进一步改进性能:

    • 提示缓存:改进重复分析的性能。
    • 批处理:用于大量文档处理。
    • 工具使用:从文档中提取特定信息以用作工具输入。

估计您的成本

PDF 文件的令牌计数取决于从文档提取的总文本以及页数:

  • 文本令牌成本:每页通常使用 1,500-3,000 个令牌,具体取决于内容密度。标准 API 定价适用,无额外 PDF 费用。
  • 图像令牌成本:由于每一页都转换为图像,应用相同的 基于图像的成本计算。

您可以使用 令牌计数 来估计您特定 PDF 的成本。


优化 PDF 处理

改进性能

遵循这些最佳实践以获得最佳结果:

  • 在请求中将 PDF 放在文本之前
  • 使用标准字体
  • 确保文本清晰易读
  • 将页面旋转到正确的直立方向
  • 在提示中使用逻辑页码(来自 PDF 查看器)
  • 在需要时将大型 PDF 分成块
  • 为重复分析启用提示缓存

扩展您的实现

对于大容量处理,请考虑以下方法:

使用提示缓存

缓存 PDF 以改进重复查询的性能:

client = anthropic.Anthropic()
# ...
message = client.messages.create(
    model="claude-opus-4-7",
    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": "Analyze this document."},
            ],
        }
    ],
)

处理文档批次

使用消息批处理 API 进行大容量工作流:

client = anthropic.Anthropic()
# ...
message_batch = client.messages.batches.create(
    requests=[
        {
            "custom_id": "doc1",
            "params": {
                "model": "claude-opus-4-7",
                "max_tokens": 1024,
                "messages": [
                    {
                        "role": "user",
                        "content": [
                            {
                                "type": "document",
                                "source": {
                                    "type": "base64",
                                    "media_type": "application/pdf",
                                    "data": pdf_data,
                                },
                            },
                            {"type": "text", "text": "Summarize this document."},
                        ],
                    }
                ],
            },
        }
    ]
)

后续步骤

尝试 PDF 示例

在 cookbook 食谱中探索 PDF 处理的实际示例。

查看 API 参考

查看 PDF 支持的完整 API 文档。

Was this page helpful?

  • 检查 PDF 要求
  • Amazon Bedrock PDF 支持
  • 使用 Claude 处理 PDF
  • 发送您的第一个 PDF 请求
  • PDF 支持的工作原理
  • 优化 PDF 处理