POST/api/v1/convert/html-to-pdf

HTML 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

NameTypeRequiredDefaultDescription
htmlstringOne of html or url required-Raw HTML string to convert.
urlstringOne of html or url required-Live URL to render and convert to PDF.
cssstringNonullAdditional CSS to inject into the page before rendering.
page_sizestringNoA4Page size: A4, LETTER, LEGAL, TABLOID, A3, A5, etc.
orientationstringNoportraitPage orientation: portrait or landscape.
margin_topintNo20mmTop margin in millimeters.
margin_bottomintNo20mmBottom margin in millimeters.
margin_leftintNo20mmLeft margin in millimeters.
margin_rightintNo20mmRight margin in millimeters.
sanitize_htmlboolNotrueSanitize 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.pdf
Python
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"
}