API Documentation

Complete guide to integrating HTML to PDF conversion into your applications

Quick Start

Get up and running in minutes

1. Get Your API Key

Generate an API key from your dashboard or use the API endpoint:

POST /api/keys/generate
Content-Type: application/json

{
  "name": "My API Key"
}

2. Make Your First Request

Convert HTML to PDF with a simple POST request:

curl -X POST https://api.htmltopdf.ac/api/convert \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "html": "<h1>Hello World</h1>",
    "options": {
      "format": "A4",
      "margin": "1cm"
    }
  }' \
  --output document.pdf

Success!

You should now have a PDF file named document.pdf in your current directory.

Authentication

Secure your API requests with Bearer tokens

All API requests must include your API key in the Authorization header using the Bearer scheme:

Authorization: Bearer YOUR_API_KEY

Security Best Practices

  • Never expose your API key in client-side code
  • Use environment variables to store your API key
  • Rotate your API keys regularly
  • Use different API keys for development and production
  • Revoke compromised keys immediately

Important

If your API key is compromised, delete it immediately from your dashboard and generate a new one.

API Endpoints

Complete reference for all available endpoints

POST
/api/convert

Convert HTML content to PDF format

Request Body:

{
  "html": "<html>...</html>",      // Required: HTML content
  "url": "https://example.com",    // Alternative to html
  "options": {                     // Optional: PDF options
    "format": "A4",
    "margin": "1cm",
    "printBackground": true,
    "displayHeaderFooter": true,
    "headerTemplate": "<div>...</div>",
    "footerTemplate": "<div>...</div>"
  }
}

Response:

Returns PDF file as binary data (application/pdf)


POST
/api/keys/generate

Generate a new API key

Request Body:

{
  "name": "My API Key"    // Optional: Human-readable name
}

Response:

{
  "apiKey": "pk_live_...",
  "name": "My API Key",
  "createdAt": "2024-01-15T10:30:00Z"
}

GET
/api/keys/usage

Get usage statistics for your API key

Response:

{
  "usage": {
    "current": 1250,
    "limit": 10000,
    "resetAt": "2024-02-01T00:00:00Z"
  },
  "plan": "pro"
}

GET
/api/billing/plans

Get available billing plans and pricing

Response:

{
  "plans": [
    {
      "id": "free",
      "name": "Free",
      "price": 0,
      "limits": {
        "conversions": 100,
        "period": "month"
      }
    },
    {
      "id": "pro",
      "name": "Pro",
      "price": 29,
      "limits": {
        "conversions": 10000,
        "period": "month"
      }
    }
  ]
}

PDF Options

Customize your PDF output with these options

OptionTypeDefaultDescription
formatstringA4Page format: A4, Letter, Legal, Tabloid, A3, A5
widthstring-Page width (e.g., "210mm", "8.5in")
heightstring-Page height (e.g., "297mm", "11in")
marginobject/string0Page margins: "1cm" or {top, right, bottom, left}
landscapebooleanfalsePrint in landscape orientation
printBackgroundbooleanfalsePrint background graphics
scalenumber1Scale of the webpage (0.1-2)
displayHeaderFooterbooleanfalseDisplay header and footer
headerTemplatestring-HTML template for header
footerTemplatestring-HTML template for footer
preferCSSPageSizebooleanfalseUse CSS-defined page size

Example with multiple options:

{
  "html": "<h1>My Document</h1>",
  "options": {
    "format": "A4",
    "landscape": false,
    "margin": {
      "top": "2cm",
      "right": "2cm",
      "bottom": "2cm",
      "left": "2cm"
    },
    "printBackground": true,
    "displayHeaderFooter": true,
    "headerTemplate": "<div style='font-size:10px; text-align:center; width:100%;'>My Header</div>",
    "footerTemplate": "<div style='font-size:10px; text-align:center; width:100%;'>Page <span class='pageNumber'></span> of <span class='totalPages'></span></div>"
  }
}

Error Handling

Common errors and how to resolve them

All errors follow a consistent JSON format:

{
  "error": {
    "code": "ERROR_CODE",
    "message": "Human-readable error message",
    "details": {}  // Optional additional information
  }
}

Common Error Codes

401
UNAUTHORIZED

Missing or invalid API key

Solution:

Ensure your Authorization header includes a valid API key

400
INVALID_REQUEST

Invalid request body or parameters

Solution:

Check that your request includes either html or url parameter, and all options are valid

429
RATE_LIMIT_EXCEEDED

Too many requests

Solution:

Wait before making another request, or upgrade your plan for higher limits

402
QUOTA_EXCEEDED

Monthly conversion quota exceeded

Solution:

Wait for your quota to reset, or upgrade your plan

500
CONVERSION_FAILED

PDF conversion failed

Solution:

Check your HTML is valid, external resources are accessible, and retry the request

413
PAYLOAD_TOO_LARGE

Request body exceeds maximum size

Solution:

Reduce the size of your HTML content or use the url parameter instead

Rate Limits

Understanding request limits by plan tier

Rate limits are applied per API key and reset monthly. Different plans have different limits:

Free

$0/mo
Conversions100/month
Rate limit10/min
Max size1 MB
SupportCommunity

Pro

$29/mo
Conversions10,000/month
Rate limit60/min
Max size10 MB
SupportEmail

Enterprise

Custom
ConversionsUnlimited
Rate limitCustom
Max size100 MB
SupportPriority

Rate Limit Headers

Every API response includes headers showing your current rate limit status:

X-RateLimit-Limit: 60
X-RateLimit-Remaining: 45
X-RateLimit-Reset: 1640000000

Code Examples

Integration examples in popular programming languages

Basic Conversion

curl -X POST https://api.htmltopdf.ac/api/convert \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "html": "<html><body><h1>Hello World</h1></body></html>",
    "options": {
      "format": "A4",
      "margin": "1cm"
    }
  }' \
  --output document.pdf

Convert from URL

curl -X POST https://api.htmltopdf.ac/api/convert \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://example.com",
    "options": {
      "format": "Letter",
      "landscape": true
    }
  }' \
  --output webpage.pdf