0

Download Agreement PDF

by
Published 4 days ago

Download the combined signed PDF of an agreement into Windmill object storage.

Script adobe_acrobat_sign Verified

The script

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

4
async function apiBase(auth: RT.AdobeAcrobatSign): Promise<string> {
5
  if (auth.base_uri) return auth.base_uri.replace(/\/+$/, "")
6
  const r = await fetch("https://api.adobesign.com/api/rest/v6/baseUris", {
7
    headers: {
8
      Authorization: `Bearer ${auth.token}`,
9
      Accept: "application/json",
10
    },
11
  })
12
  if (!r.ok) throw new Error(`${r.status} ${await r.text()}`)
13
  const { apiAccessPoint } = (await r.json()) as { apiAccessPoint: string }
14
  return apiAccessPoint.replace(/\/+$/, "")
15
}
16

17
/**
18
 * Download Agreement PDF
19
 * Download the combined PDF (all documents merged) of an agreement to Windmill object storage and return its S3 reference.
20
 */
21
export async function main(
22
  auth: RT.AdobeAcrobatSign,
23
  agreement_id: string,
24
  destination: S3Object,
25
  attach_supporting_documents: boolean | undefined,
26
  audit_report: boolean | undefined
27
) {
28
  const base = await apiBase(auth)
29
  const url = new URL(
30
    `${base}/api/rest/v6/agreements/${agreement_id}/combinedDocument`
31
  )
32
  for (const [k, v] of [
33
    ["attachSupportingDocuments", attach_supporting_documents],
34
    ["auditReport", audit_report],
35
  ] as const) {
36
    if (v !== undefined) {
37
      url.searchParams.append(k, String(v))
38
    }
39
  }
40

41
  const response = await fetch(url, {
42
    method: "GET",
43
    headers: {
44
      Authorization: `Bearer ${auth.token}`,
45
      Accept: "application/pdf",
46
    },
47
  })
48

49
  if (!response.ok) {
50
    throw new Error(`${response.status} ${await response.text()}`)
51
  }
52

53
  await wmill.writeS3File(
54
    destination,
55
    response.body as ReadableStream<Uint8Array>
56
  )
57
  return destination
58
}
59