IntegrationsAdvanced

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.

11 minAdvanced

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.

Apps Script - Services
Services [ + Add a service ]
----------------------------------
Drive API v3 [x]
Docs API v1 [ ]
[ Add ]
Add the Drive advanced service before running the script.

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.

Code.gs
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.

Code.gs
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.

Apps Script - Execution log
$Run -> summarizePdf
The report covers 2026 hiring trends across tech.
Key points:
- Remote roles fell 12 percent year over year
- AI skills appear in 1 of 3 listings
$
Scanned PDFs need OCR
The ocr: true flag is what makes image-only or scanned PDFs readable. For text-based PDFs it still works and is a touch slower, so leave it on for reliability.

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

Tags
#google-drive#pdf#apps-script#summarize