Skip to content

Commit e3ff90f

Browse files
authored
Update pdf-endpoint.mdx (#23175)
1 parent 3c08119 commit e3ff90f

File tree

1 file changed

+47
-25
lines changed

1 file changed

+47
-25
lines changed

src/content/docs/browser-rendering/rest-api/pdf-endpoint.mdx

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,28 @@ sidebar:
77

88
import { Tabs, TabItem } from "~/components";
99

10-
The `/pdf` endpoint instructs the browser to render the webpage as a PDF document.
10+
The `/pdf` endpoint instructs the browser to generate a PDF of a webpage or custom HTML using Cloudflare's headless browser rendering service.
11+
12+
## Endpoint
13+
14+
```txt
15+
https://api.cloudflare.com/client/v4/accounts/<accountId>/browser-rendering/pdf
16+
```
17+
18+
## Required fields
19+
You must provide either `url` or `html`:
20+
- `url` (string)
21+
- `html` (string)
22+
23+
## Common use cases
24+
25+
- Capture a PDF of a webpage
26+
- Generate PDFs, such as invoices, licenses, reports, and certificates, directly from HTML
1127

1228
## Basic usage
1329

30+
### Convert a URL to PDF
31+
1432
<Tabs syncKey="workersExamples"> <TabItem label="curl">
1533

1634
Navigate to `https://example.com/` and inject custom CSS and an external stylesheet. Then return the rendered page as a PDF.
@@ -51,11 +69,35 @@ console.log(content);
5169

5270
</TabItem> </Tabs>
5371

72+
### Convert custom HTML to PDF
73+
74+
If you have raw HTML you want to generate a PDF from, use the `html` option. You can still apply custom styles using the `addStyleTag` parameter.
75+
76+
```bash
77+
curl -X POST https://api.cloudflare.com/client/v4/accounts/<acccountID>/browser-rendering/pdf \
78+
-H 'Authorization: Bearer <apiToken>' \
79+
-H 'Content-Type: application/json' \
80+
-d '{
81+
"html": "<html><body>Advanced Snapshot</body></html>",
82+
"addStyleTag": [
83+
{ "content": "body { font-family: Arial; }" },
84+
{ "url": "https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" }
85+
]
86+
}' \
87+
--output "invoice.pdf"
88+
```
89+
5490
## Advanced usage
5591

56-
Navigate to `https://example.com`, first setting an additional HTTP request header and configuring the page size (`viewport`). Then, wait until there are no more than 2 network connections for at least 500 ms, or until the maximum timeout of 4500 ms is reached, before considering the page loaded and returning the rendered PDF document.
92+
:::note[Looking for more parameters?]
93+
Visit the [Browser Rendering PDF API reference](/api/resources/browser_rendering/subresources/pdf/methods/create/) for all available parameters, such as setting HTTP credentials using `authenticate`, setting `cookies`, and customizing load behavior using `gotoOptions`.
94+
:::
95+
96+
### Advanced page load with custom headers and viewport
5797

58-
The `goToOptions` parameter exposes most of [Puppeteer'd API](https://pptr.dev/api/puppeteer.gotooptions).
98+
Navigate to `https://example.com`, setting additional HTTP headers and configuring the page size (viewport). The PDF generation will wait until there are no more than two network connections for at least 500 ms, or until the maximum timeout of 4500 ms is reached, before rendering.
99+
100+
The `goToOptions` parameter exposes most of [Puppeteer's API](https://pptr.dev/api/puppeteer.gotooptions).
59101

60102
```bash
61103
curl -X POST 'https://api.cloudflare.com/client/v4/accounts/<accountId>/browser-rendering/pdf' \
@@ -78,9 +120,9 @@ curl -X POST 'https://api.cloudflare.com/client/v4/accounts/<accountId>/browser-
78120
--output "advanced-output.pdf"
79121
```
80122

81-
## Blocking images and styles when generating a PDF
123+
### Blocking images and styles when generating a PDF
82124

83-
The options `rejectResourceTypes` and `rejectRequestPattern` can be used to block requests. The opposite can also be done, _only_ allow certain requests using `allowResourceTypes` and `allowRequestPattern`.
125+
The options `rejectResourceTypes` and `rejectRequestPattern` can be used to block requests during rendering. The opposite can also be done, _only_ allow certain requests using `allowResourceTypes` and `allowRequestPattern`.
84126

85127
```bash
86128
curl -X POST https://api.cloudflare.com/client/v4/accounts/<acccountID>/browser-rendering/pdf \
@@ -93,23 +135,3 @@ curl -X POST https://api.cloudflare.com/client/v4/accounts/<acccountID>/browser-
93135
}' \
94136
--output "cloudflare.pdf"
95137
```
96-
97-
## Generate PDF from custom HTML
98-
99-
If you have HTML you'd like to generate a PDF from, the `html` option can be used. The option `addStyleTag` can be used to add custom styles.
100-
101-
```bash
102-
curl -X POST https://api.cloudflare.com/client/v4/accounts/<acccountID>/browser-rendering/pdf \
103-
-H 'Authorization: Bearer <apiToken>' \
104-
-H 'Content-Type: application/json' \
105-
-d '{
106-
"html": "<html><body>Advanced Snapshot</body></html>",
107-
"addStyleTag": [
108-
{ "content": "body { font-family: Arial; }" },
109-
{ "url": "https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" }
110-
]
111-
}' \
112-
--output "invoice.pdf"
113-
```
114-
115-
Many more options exist, like setting HTTP credentials using `authenticate`, setting `cookies`, and using `gotoOptions` to control page load behaviour - check the endpoint [reference](/api/resources/browser_rendering/subresources/pdf/methods/create/) for all available parameters.

0 commit comments

Comments
 (0)