html4pdf

Quickstart

Get started with html4pdf in 2 minutes.

1. Get Your API Key

  1. Sign up for free (10 free credits)
  2. Copy your API key from the Dashboard

⚠️ Keep your API key secure! Never commit it to version control.

2. Generate Your First PDF

const response = await fetch('https://api.html4pdf.com/generate-pdf', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer YOUR_API_KEY',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    html: '<h1>Hello World</h1><p>My first PDF!</p>'
  })
});

const data = await response.json();
import requests

response = requests.post(
    'https://api.html4pdf.com/generate-pdf',
    headers={'Authorization': 'Bearer YOUR_API_KEY'},
    json={'html': '<h1>Hello World</h1><p>My first PDF!</p>'}
)

data = response.json()
<?php
$ch = curl_init('https://api.html4pdf.com/generate-pdf');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Authorization: Bearer YOUR_API_KEY',
    'Content-Type: application/json'
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
    'html' => '<h1>Hello World</h1><p>My first PDF!</p>'
]));

$response = curl_exec($ch);
$data = json_decode($response, true);
?>
curl -X POST https://api.html4pdf.com/generate-pdf \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"html": "<h1>Hello World</h1><p>My first PDF!</p>"}'
200OK
{
  "success": true,
  "pdf": "JVBERi0xLjQKJeLjz9MKMyAwIG9...",
  "creditsRemaining": 9,
  "metadata": {
    "size": 12345,
    "processingTime": 3421
  }
}

3. Save the PDF

const fs = require('fs');
const pdfBuffer = Buffer.from(data.pdf, 'base64');
fs.writeFileSync('output.pdf', pdfBuffer);
import base64

pdf_bytes = base64.b64decode(data['pdf'])
with open('output.pdf', 'wb') as f:
    f.write(pdf_bytes)
<?php
file_put_contents('output.pdf', base64_decode($data['pdf']));
?>

Common Errors

401Unauthorized
{
  "error": "Invalid API key"
}

Solution: Check your API key is correct.

402Payment Required
{
  "error": "Insufficient credits"
}

Solution: Buy more credits

400Bad Request
{
  "error": "Must provide one of: html, url, or template"
}

Solution: Include html, url, or template in your request.

Next Steps