Gemini 模型可處理 PDF 格式的文件,並運用原生視覺功能瞭解整份文件的內容。這項功能不僅能擷取文字,還可讓 Gemini 執行下列動作:
- 分析及解讀內容,包括文字、圖片、圖表、圖表和表格,即使是長達 1000 頁的文件也沒問題。
- 以結構化輸出格式擷取資訊。
- 根據文件中的圖像和文字元素,摘要內容並回答問題。
- 轉錄文件內容 (例如轉錄為 HTML),保留版面配置和格式,供下游應用程式使用。
您也可以用相同方式傳送非 PDF 文件,但 Gemini 會將這些文件視為一般文字,因此圖表或格式等脈絡資訊會消失。
內嵌傳遞 PDF 資料
您可以在要求中內嵌傳遞 PDF 資料。這種方法最適合用於小型文件或暫時處理,因為您不需要在後續要求中參照該檔案。如果需要參照大型文件進行多輪互動,建議使用 Files API,以縮短要求延遲時間並減少頻寬用量。
以下範例說明如何內嵌傳遞 PDF 資料:
Python
from google import genai
import base64
client = genai.Client()
with open('path/to/document.pdf', 'rb') as f:
pdf_bytes = f.read()
interaction = client.interactions.create(
model="gemini-3.6-flash",
input=[
{
"type": "document",
"data": base64.b64encode(pdf_bytes).decode('utf-8'),
"mime_type": "application/pdf"
},
{"type": "text", "text": "Summarize this document"}
]
)
print(interaction.output_text)
JavaScript
import { GoogleGenAI } from "@google/genai";
import * as fs from "node:fs";
const ai = new GoogleGenAI({});
async function main() {
const pdfData = fs.readFileSync("path/to/document.pdf", {
encoding: "base64"
});
const interaction = await ai.interactions.create({
model: "gemini-3.6-flash",
input: [
{ type: "text", text: "Summarize this document" },
{
type: "document",
data: pdfData,
mime_type: "application/pdf"
}
]
});
console.log(interaction.output_text);
}
main();
REST
PDF_PATH="path/to/document.pdf"
if [[ "$(base64 --version 2>&1)" = *"FreeBSD"* ]]; then
B64FLAGS="--input"
else
B64FLAGS="-w0"
fi
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
"model": "gemini-3.6-flash",
"input": [
{
"type": "document",
"data": "'$(base64 $B64FLAGS $PDF_PATH)'",
"mime_type": "application/pdf"
},
{"type": "text", "text": "Summarize this document"}
]
}'
你也可以上傳本機 PDF 檔案進行處理:
Python
from google import genai
client = genai.Client()
uploaded_file = client.files.upload(file="file.pdf")
interaction = client.interactions.create(
model="gemini-3.6-flash",
input=[
{"type": "document", "uri": uploaded_file.uri, "mime_type": uploaded_file.mime_type},
{"type": "text", "text": "Summarize this document"}
]
)
print(interaction.output_text)
JavaScript
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({});
async function main() {
const uploadedFile = await ai.files.upload({
file: "file.pdf",
config: { mime_type: "application/pdf" }
});
const interaction = await ai.interactions.create({
model