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.pdfSuccess!
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_KEYSecurity 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
/api/convertConvert 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)
/api/keys/generateGenerate 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"
}/api/keys/usageGet usage statistics for your API key
Response:
{
"usage": {
"current": 1250,
"limit": 10000,
"resetAt": "2024-02-01T00:00:00Z"
},
"plan": "pro"
}/api/billing/plansGet 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
| Option | Type | Default | Description |
|---|---|---|---|
format | string | A4 | Page format: A4, Letter, Legal, Tabloid, A3, A5 |
width | string | - | Page width (e.g., "210mm", "8.5in") |
height | string | - | Page height (e.g., "297mm", "11in") |
margin | object/string | 0 | Page margins: "1cm" or {top, right, bottom, left} |
landscape | boolean | false | Print in landscape orientation |
printBackground | boolean | false | Print background graphics |
scale | number | 1 | Scale of the webpage (0.1-2) |
displayHeaderFooter | boolean | false | Display header and footer |
headerTemplate | string | - | HTML template for header |
footerTemplate | string | - | HTML template for footer |
preferCSSPageSize | boolean | false | Use 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
UNAUTHORIZEDMissing or invalid API key
Solution:
Ensure your Authorization header includes a valid API key
INVALID_REQUESTInvalid request body or parameters
Solution:
Check that your request includes either html or url parameter, and all options are valid
RATE_LIMIT_EXCEEDEDToo many requests
Solution:
Wait before making another request, or upgrade your plan for higher limits
QUOTA_EXCEEDEDMonthly conversion quota exceeded
Solution:
Wait for your quota to reset, or upgrade your plan
CONVERSION_FAILEDPDF conversion failed
Solution:
Check your HTML is valid, external resources are accessible, and retry the request
PAYLOAD_TOO_LARGERequest 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
Pro
Enterprise
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: 1640000000Code 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.pdfConvert 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