How to Extract and Summarize a Drive PDF With AI Using Apps Script
Read a PDF stored in Google Drive, pull its text, and send it to an AI model for a structured summary, all from Apps Script.
PDFs piling up in Drive are hard to search. This guide reads a PDF by its Drive file id, extracts the text, and asks an AI model to summarize it into key points. It uses a small trick: converting the PDF to a temporary Google Doc to get clean text.
- A PDF file in Google Drive and its file id
- An OpenAI API key
- The Drive Advanced Service enabled in Apps Script
- About 11 minutes
Step 1: Enable the Drive advanced service
In the Apps Script editor open Services in the left rail, add Drive API, and save. This unlocks the conversion that turns a PDF into readable text.
Step 2: Convert the PDF to text
Take the PDF blob from Drive, import it as a Google Doc using OCR, then read the body text. Delete the temporary doc afterward so it does not clutter your Drive.
function pdfToText(fileId) {
const blob = DriveApp.getFileById(fileId).getBlob();
const resource = { title: "temp-ocr", mimeType: "application/vnd.google-apps.document" };
const file = Drive.Files.insert(resource, blob, { ocr: true });
const text = DocumentApp.openById(file.id).getBody().getText();
DriveApp.getFileById(file.id).setTrashed(true);
return text;
}Step 3: Send the text to the model
Pass the extracted text to the Chat Completions API with an instruction to return a short summary and the main points. Trim very long PDFs to stay inside the token budget.
const OPENAI_KEY = "sk-PASTE_YOUR_KEY";
function summarizePdf() {
const text = pdfToText("YOUR_DRIVE_FILE_ID").slice(0, 12000);
const res = UrlFetchApp.fetch("https://api.openai.com/v1/chat/completions", {
method: "post",
contentType: "application/json",
headers: { Authorization: "Bearer " + OPENAI_KEY },
payload: JSON.stringify({
model: "gpt-5-mini",
messages: [
{ role: "system", content: "Summarize in 2 sentences, then list 5 key points." },
{ role: "user", content: text },
],
}),
});
Logger.log(JSON.parse(res.getContentText()).choices[0].message.content);
}Step 4: Run and read the summary
Select summarizePdf and click Run. Approve the permission prompts on the first run. The summary prints to the execution log.
Result: any PDF in Drive becomes a two-sentence summary plus key points in seconds. Loop over a folder of files to triage a whole backlog.
Watch related tutorials
1:42:18
28:14
41:09
9:47
8:23
52:31