/pdf - Render PDF
The /pdf
endpoint instructs the browser to generate a PDF of a webpage or custom HTML using Cloudflare's headless browser rendering service.
https://api.cloudflare.com/client/v4/accounts/<accountId>/browser-rendering/pdf
You must provide either url
or html
:
url
(string)html
(string)
- Capture a PDF of a webpage
- Generate PDFs, such as invoices, licenses, reports, and certificates, directly from HTML
Navigate to https://example.com/
and inject custom CSS and an external stylesheet. Then return the rendered page as a PDF.
curl -X POST 'https://api.cloudflare.com/client/v4/accounts/<accountId>/browser-rendering/pdf' \ -H 'Authorization: Bearer <apiToken>' \ -H 'Content-Type: application/json' \ -d '{ "url": "https://example.com/", "addStyleTag": [ { "content": "body { font-family: Arial; }" } ] }' \ --output "output.pdf"
import Cloudflare from "cloudflare";
const client = new Cloudflare({ apiToken: process.env["CLOUDFLARE_API_TOKEN"],});
const pdf = await client.browserRendering.pdf.create({ account_id: process.env["CLOUDFLARE_ACCOUNT_ID"], url: "https://example.com/", addStyleTag: [ { content: "body { font-family: Arial; }" } ]});
console.log(pdf);
const content = await pdf.blob();console.log(content);
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.
curl -X POST https://api.cloudflare.com/client/v4/accounts/<acccountID>/browser-rendering/pdf \ -H 'Authorization: Bearer <apiToken>' \ -H 'Content-Type: application/json' \ -d '{ "html": "<html><body>Advanced Snapshot</body></html>", "addStyleTag": [ { "content": "body { font-family: Arial; }" }, { "url": "https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" } ]}' \ --output "invoice.pdf"