html4pdf

API Reference

Convert HTML to PDF with a simple POST request.

Base URL

https://api.html4pdf.com

Authentication

Include your API key in the Authorization header:

Authorization: Bearer YOUR_API_KEY

Get 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)

ParameterTypeDescription
htmlstringHTML content with inline CSS
urlstringPublic URL to convert (requires webhook)
templateobjectUse a pre-built template

Options

ParameterDefaultDescription
responseFormatbase64base64 or binary
options.formatA4Page size: A4, Letter, Legal
options.landscapefalseLandscape orientation
options.margin.top20mmTop 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 URL
response = 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 URL

Configure your webhook URL in Dashboard Settings.

Webhook Payload

200OK
{
  "success": true,
  "pdf": "JVBERi0xLjQK...",
  "metadata": {
    "size": 12345,
    "processingTime": 14523
  }
}

Credit Costs

MethodDeliveryCredits
HTMLSync1
HTMLWebhook2
URLWebhook2
TemplateSync1

Rate Limits

  • With API Key: No limit (fair use)
  • Without API Key: 10/minute per IP

Next Steps