0

Export Document

Published Oct 13, 2026

Export a Google Doc as PDF, DOCX, Markdown, etc. to Windmill object storage. Requires drive.readonly or drive on the connection.

Script gdocs Verified

The script

Submitted by hugo989 Bun
Verified 41 days ago
1
import * as wmill from "windmill-client"
2
import { S3Object } from "windmill-client"
3

4
const FORMATS = {
5
  pdf: "application/pdf",
6
  docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
7
  odt: "application/vnd.oasis.opendocument.text",
8
  rtf: "application/rtf",
9
  txt: "text/plain",
10
  md: "text/markdown",
11
  html_zip: "application/zip",
12
  epub: "application/epub+zip",
13
}
14

15
/**
16
 * Export Document
17
 * Export a Google Doc (all tabs) as PDF, Word (docx), OpenDocument, RTF, plain text, Markdown, zipped HTML or EPUB, save it to Windmill object storage and return its S3 reference. Without `destination`, a key is generated. Drive caps exports at 10 MB. Requires drive.readonly or drive on the connection (the documents scope can't download files).
18
 */
19
export async function main(
20
  auth: RT.Gdocs,
21
  document_id: string,
22
  format: "pdf" | "docx" | "odt" | "rtf" | "txt" | "md" | "html_zip" | "epub" = "pdf",
23
  destination: S3Object | undefined
24
) {
25
  const url = new URL(
26
    `https://www.googleapis.com/drive/v3/files/${document_id}/export`
27
  )
28
  url.searchParams.append("mimeType", FORMATS[format])
29
  const response = await fetch(url, {
30
    headers: { Authorization: `Bearer ${auth.token}` },
31
  })
32
  if (!response.ok) {
33
    throw new Error(`${response.status} ${await response.text()}`)
34
  }
35

36
  return await wmill.writeS3File(
37
    destination || undefined,
38
    await response.blob(),
39
    undefined,
40
    FORMATS[format]
41
  )
42
}
43