API Reference
Convert HTML to PDF with a simple POST request.
Base URL
https://api.html4pdf.comAuthentication
Include your API key in the Authorization header:
Authorization: Bearer YOUR_API_KEYGet your API key from the Dashboard.
Generate PDF
Endpoint: POST /generate-pdf
Basic Request
Choose one input method: html, url, or template.
const response = await fetch('https://api.html4pdf.com/generate-pdf', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json'
},
body: JSON.stringify({
html: '<h1>Hello World</h1>',
options: { format: 'A4' }
})
});
const { pdf } = await response.json();import requests
response = requests.post(
'https://api.html4pdf.com/generate-pdf',
headers={'Authorization': 'Bearer YOUR_API_KEY'},
json={
'html': '<h1>Hello World</h1>',
'options': {'format': 'A4'}
}
)
pdf = response.json()['pdf']curl -X POST https://api.html4pdf.com/generate-pdf \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"html": "<h1>Hello World</h1>",
"options": {"format": "A4"}
}'Parameters
Input (choose one)
| Parameter | Type | Description |
|---|---|---|
html | string | HTML content with inline CSS |
url | string | Public URL to convert (requires webhook) |
template | object | Use a pre-built template |
Options
| Parameter | Default | Description |
|---|---|---|
responseFormat | base64 | base64 or binary |
options.format | A4 | Page size: A4, Letter, Legal |
options.landscape | false | Landscape orientation |
options.margin.top | 20mm | Top margin |
See Quickstart for all options.
Response
200OK
{
"success": true,
"pdf": "JVBERi0xLjQKJeLjz9MK...",
"creditsRemaining": 99,
"metadata": {
"size": 12345,
"processingTime": 342
}
}Error Responses
401Unauthorized
{
"error": "Invalid API key"
}Solution: Check your API key in the Dashboard.
402Payment Required
{
"error": "Insufficient credits"
}Solution: Purchase more credits.
400Bad Request
{
"error": "Must provide one of: html, url, or template"
}Solution: Include exactly one input parameter.
Webhooks
For long-running conversions (URLs or large HTML), use webhook delivery:
await fetch('https://api.html4pdf.com/generate-pdf', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json'
},
body: JSON.stringify({
url: 'https://example.com/page',
webhook: true
})
});
// Returns 202 Accepted
// PDF will be sent to your webhook URLresponse = requests.post(
'https://api.html4pdf.com/generate-pdf',
headers={'Authorization': 'Bearer YOUR_API_KEY'},
json={
'url': 'https://example.com/page',
'webhook': True
}
)
# Returns 202 Accepted
# PDF will be sent to your webhook URLConfigure your webhook URL in Dashboard Settings.
Webhook Payload
200OK
{
"success": true,
"pdf": "JVBERi0xLjQK...",
"metadata": {
"size": 12345,
"processingTime": 14523
}
}Credit Costs
| Method | Delivery | Credits |
|---|---|---|
| HTML | Sync | 1 |
| HTML | Webhook | 2 |
| URL | Webhook | 2 |
| Template | Sync | 1 |
Rate Limits
- With API Key: No limit (fair use)
- Without API Key: 10/minute per IP