POST
/api/v1/convert/html-to-pdfHTML to PDF
Convert raw HTML or a live URL into a PDF with full CSS support. Handles flexbox, CSS Grid, web fonts, and SVG out of the box. Append ?async=true to process large documents in the background.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
| html | string | One of html or url required | - | Raw HTML string to convert. |
| url | string | One of html or url required | - | Live URL to render and convert to PDF. |
| css | string | No | null | Additional CSS to inject into the page before rendering. |
| page_size | string | No | A4 | Page size: A4, LETTER, LEGAL, TABLOID, A3, A5, etc. |
| orientation | string | No | portrait | Page orientation: portrait or landscape. |
| margin_top | int | No | 20mm | Top margin in millimeters. |
| margin_bottom | int | No | 20mm | Bottom margin in millimeters. |
| margin_left | int | No | 20mm | Left margin in millimeters. |
| margin_right | int | No | 20mm | Right margin in millimeters. |
| sanitize_html | bool | No | true | Sanitize HTML input to prevent XSS. Disable for trusted content. |
Code Examples
cURL
curl -X POST "https://app.alternapdf.com/api/v1/convert/html-to-pdf" \
-H "X-API-Key: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"html": "<h1>Quarterly Report</h1><p>Revenue increased <strong>32%</strong> year over year.</p>",
"css": "body { font-family: Georgia, serif; } h1 { color: #1a365d; }",
"page_size": "A4",
"orientation": "portrait"
}' \
--output report.pdfPython
import requests
response = requests.post(
"https://app.alternapdf.com/api/v1/convert/html-to-pdf",
headers={"X-API-Key": "YOUR_API_KEY"},
json={
"html": "<h1>Quarterly Report</h1><p>Revenue grew <strong>32%</strong>.</p>",
"css": "body { font-family: Georgia, serif; }",
"page_size": "A4"
}
)
with open("report.pdf", "wb") as f:
f.write(response.content)JavaScript
const response = await fetch("https://app.alternapdf.com/api/v1/convert/html-to-pdf", {
method: "POST",
headers: {
"X-API-Key": "YOUR_API_KEY",
"Content-Type": "application/json",
},
body: JSON.stringify({
html: "<h1>Quarterly Report</h1><p>Revenue grew <strong>32%</strong>.</p>",
css: "body { font-family: Georgia, serif; }",
page_size: "A4",
}),
});
const buffer = await response.arrayBuffer();
fs.writeFileSync("report.pdf", Buffer.from(buffer));Response
Synchronous (default)
Returns the PDF as a binary download with Content-Type: application/pdf.
HTTP/1.1 200 OK
Content-Type: application/pdf
Content-Disposition: attachment; filename="converted.pdf"
X-Credits-Used: 1
X-Credits-Remaining: 4999
<binary PDF data>Asynchronous ?async=true
Returns a job ID for polling. Use the Async Jobs endpoint to check status and download the result.
HTTP/1.1 202 Accepted
Content-Type: application/json
{
"job_id": "abc-123-def-456",
"status": "processing",
"poll_url": "/api/v1/jobs/abc-123-def-456"
}