Skip to main content

Document Processing with Gemini

Gemini can process and analyze large documents, including PDFs and text files, making it powerful for document summarization, information extraction, and analysis.

See Live Demo built using these code snippets

import { GoogleGenerativeAI } from "@google/generative-ai";

const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY);
const model = genAI.getGenerativeModel({ model: "gemini-2.0-flash" });

// Convert PDF to base64
const fileData = await fetch("report.pdf").then(res => res.arrayBuffer());
const base64Data = btoa(
new Uint8Array(fileData)
.reduce((data, byte) => data + String.fromCharCode(byte), '')
);

// Generate content using the PDF
const result = await model.generateContent([
{
inlineData: {
mimeType: "application/pdf",
data: base64Data
}
},
"Summarize this document in bullet points."
]);

console.log(result.response.text());

Use Cases

  • Document summarization
  • Information extraction
  • Document comparison
  • Content analysis
  • Report generation
  • Data extraction from documents

Example Output

When summarizing a document, you might get output like this:

Key Points from Annual Report:

• Revenue grew by 12% in 2024, reaching an all-time high
• Major growth was driven by the tech sector and overseas markets
• The report emphasizes sustainability and innovation as key factors
• New product lines contributed 25% of total revenue
• Customer satisfaction metrics improved by 8%