Home API Tools Posts Hire Me About
Sign In Create Account
Live Free tier included

Convert HTML to PDF via API

Transform HTML & CSS into professionally styled PDF documents. Custom page sizes, orientation, headers, footers — perfect for invoices, reports, and certificates.

A4 / Letter / A3 Headers & Footers CSS Styling No API Key

Try it Free

Write HTML, choose your settings, and generate a PDF instantly.

Try Free
Result

Write HTML and click Generate PDF

API Documentation

Convert any HTML/CSS content into a professionally styled PDF document. Supports custom page sizes, orientation, margins, headers, and footers.

Endpoint

POST https://www.mahmoudalhabash.com/api/v1/html-to-pdf

Send an HTML string and configuration. The API renders the HTML into a PDF and returns a base64-encoded document you can download or embed.

Request Parameters

ParameterTypeRequiredDefaultDescription
htmlstringrequiredHTML content to convert (max 500KB)
formatstringoptionalA4Paper size: A4, A3, A5, Letter, Legal
orientationstringoptionalportraitportrait or landscape
margin_topnumberoptional10Top margin in mm
margin_bottomnumberoptional10Bottom margin in mm
header_htmlstringoptionalHTML for fixed page header
footer_htmlstringoptionalHTML for fixed page footer
filenamestringoptionaldocumentOutput filename (without .pdf)

Response Fields

status "success" or "error"
filename Generated filename with .pdf extension
pages Number of pages in the PDF
size_bytes File size in bytes
size_human Human-readable file size (e.g. "24.5 KB")
took_ms Processing time in milliseconds
pdf Base64-encoded data URI — use in download links or embed

Code Examples

cURL
curl -X POST https://www.mahmoudalhabash.com/api/v1/html-to-pdf \ -H "Content-Type: application/json" \ -d '{ "html": "<h1>Invoice #001</h1><p>Total: $99</p>", "format": "A4", "orientation": "portrait", "filename": "invoice-001" }'
Response 200JSON
{ "status": "success", "filename": "invoice-001.pdf", "format": "A4", "orientation": "portrait", "pages": 1, "size_bytes": 24567, "size_human": "24 KB", "took_ms": 890, "pdf": "data:application/pdf;base64,JVBERi0x..." }
JavaScript (fetch)
const res = await fetch('https://www.mahmoudalhabash.com/api/v1/html-to-pdf', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ html: document.getElementById('invoice').innerHTML, format: 'A4', filename: 'invoice' }) }); const { pdf, filename } = await res.json(); // Trigger download const a = document.createElement('a'); a.href = pdf; a.download = filename; a.click();
Python (requests)
import requests, base64 resp = requests.post( 'https://www.mahmoudalhabash.com/api/v1/html-to-pdf', json={ 'html': '<h1>Report</h1><p>Generated PDF</p>', 'format': 'A4', 'filename': 'report' } ) data = resp.json() pdf_b64 = data['pdf'].split(',')[1] with open(data['filename'], 'wb') as f: f.write(base64.b64decode(pdf_b64)) print(f"Saved {data['filename']} ({data['size_human']})")

Error Codes

422 Validation failed — HTML is required (max 500KB)
429 Rate limit exceeded
500 PDF rendering failed — invalid HTML or internal error

Rate Limits

Unlimited — Free during beta

No API key required. HTML is rendered server-side using DomPDF. External images via URL are supported. JavaScript is not executed.