//native
/**
* Get PDF
* Render a page (url or raw html) to a PDF, returned base64-encoded so Windmill previews it. Extra fields go in options (gotoOptions, viewport, waitForSelector, userAgent, ...).
*/
export async function main(
auth: RT.Cloudflare,
url: string | undefined,
html: string | undefined,
options: { [key: string]: any } | undefined
) {
if (!url && !html) throw new Error("Either url or html is required")
const response = await fetch(
`https://api.cloudflare.com/client/v4/accounts/${auth.account_id}/browser-rendering/pdf`,
{
method: "POST",
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
url: url || undefined,
html: html || undefined,
...options,
}),
}
)
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`)
}
const bytes = new Uint8Array(await response.arrayBuffer())
let binary = ""
for (let i = 0; i < bytes.length; i += 0x8000) {
binary += String.fromCharCode(...bytes.subarray(i, i + 0x8000))
}
// Windmill renders { pdf: <base64> } inline in the result panel.
return { pdf: btoa(binary) }
}
Submitted by hugo989 4 hours ago