@rtorcato/cf-common
Interfaces
RenderOptions
Defined in: index.ts:16
Options shared by screenshot and pdf.
Properties
viewport?
optionalviewport?:Viewport
Defined in: index.ts:18
Page size before loading. Puppeteer's default is 800×600.
waitUntil?
optionalwaitUntil?:PuppeteerLifeCycleEvent|PuppeteerLifeCycleEvent[]
Defined in: index.ts:20
When navigation counts as done, e.g. 'networkidle0', and its timeout in ms.
timeout?
optionaltimeout?:number
Defined in: index.ts:21
launch?
optionallaunch?:WorkersLaunchOptions
Defined in: index.ts:23
Passed to puppeteer.launch (keep_alive, …).
Type Aliases
RenderTarget
RenderTarget = {
url:string; } | {html:string; }
Defined in: index.ts:13
The page to render: a URL to load, or an HTML string to set as the content.
Functions
withBrowser()
withBrowser<
R>(binding,work,options?):Promise<R>
Defined in: index.ts:40
Launch a browser on the Browser Rendering binding, run work with it, and
always close it, also when work throws. A failed launch (no binding, the
concurrency or rate limit reached) throws a non-exposed 502
BROWSER_LAUNCH_FAILED with the original as cause. Errors from work pass
through unchanged.
Example
const title = await withBrowser(env.BROWSER, async (browser) => {
const page = await browser.newPage()
await page.goto('https://example.com')
return page.title()
})
Type Parameters
R
R
Parameters
binding
BrowserWorker
work
(browser) => Promise<R>
options?
WorkersLaunchOptions
Returns
Promise<R>
screenshot()
screenshot(
binding,target,options?):Promise<Uint8Array<ArrayBufferLike>>
Defined in: index.ts:127
Render a page and return a screenshot (PNG unless screenshot.type says
otherwise). A URL that isn't http(s) throws an exposed 400
BROWSER_INVALID_URL; a failed navigation or capture throws a non-exposed
502 BROWSER_RENDER_FAILED.
Example
const png = await screenshot(env.BROWSER, { url: 'https://example.com' }, {
viewport: { width: 1200, height: 630 },
})
return new Response(png, { headers: { 'Content-Type': 'image/png' } })
Parameters
binding
BrowserWorker
target
options?
RenderOptions & object = {}
Returns
Promise<Uint8Array<ArrayBufferLike>>
pdf()
pdf(
binding,target,options?):Promise<Uint8Array<ArrayBufferLike>>
Defined in: index.ts:141
Render a page and return it as a PDF. Same errors as screenshot.
Example
const file = await pdf(env.BROWSER, { html: invoiceHtml }, { pdf: { format: 'A4', printBackground: true } })
Parameters
binding
BrowserWorker
target
options?
RenderOptions & object = {}
Returns
Promise<Uint8Array<ArrayBufferLike>>