HTML to PDF

Convert HTML content to PDF documents with full CSS support.

HTML to PDF

Convert HTML with full CSS support to pixel-perfect PDF documents. Supports custom fonts, CSS Grid, Flexbox, and more.

POST /v1/pdf/html

Request Body

ParameterTypeRequiredDescription
htmlstringYesComplete HTML document
optionsobjectNoPDF generation options

Options Object

ParameterTypeDefaultDescription
formatstring"A4"Paper format
landscapebooleanfalseLandscape orientation
marginobject-Page margins
print_backgroundbooleantrueInclude backgrounds
scalenumber1.0Scale factor (0.1 to 2.0)
headerstring-HTML for page header
footerstring-HTML for page footer
wait_fornumber0Wait time in ms before capture

Code Examples

curl -X POST https://api.pdfapi.dev/v1/pdf/html \
  -H "Authorization: Bearer sk_live_xxx" \
  -H "Content-Type: application/json" \
  -d '{
    "html": "<!DOCTYPE html><html><head><style>body { font-family: Arial, sans-serif; padding: 40px; } h1 { color: #2563eb; } .invoice-table { width: 100%; border-collapse: collapse; } .invoice-table th, .invoice-table td { border: 1px solid #e5e7eb; padding: 12px; text-align: left; }</style></head><body><h1>Invoice #1234</h1><table class=\"invoice-table\"><tr><th>Item</th><th>Price</th></tr><tr><td>Widget</td><td>$10.00</td></tr><tr><td>Gadget</td><td>$25.00</td></tr></table><p><strong>Total: $35.00</strong></p></body></html>",
    "options": {
      "format": "A4",
      "print_background": true
    }
  }' \
  --output invoice.pdf
const html = `
<!DOCTYPE html>
<html>
<head>
  <style>
    body {
      font-family: 'Segoe UI', Arial, sans-serif;
      padding: 40px;
      color: #1f2937;
    }
    h1 { color: #2563eb; margin-bottom: 20px; }
    .invoice-table {
      width: 100%;
      border-collapse: collapse;
      margin: 20px 0;
    }
    .invoice-table th, .invoice-table td {
      border: 1px solid #e5e7eb;
      padding: 12px;
      text-align: left;
    }
    .invoice-table th {
      background: #f9fafb;
      font-weight: 600;
    }
    .total { font-size: 1.25rem; font-weight: bold; }
  </style>
</head>
<body>
  <h1>Invoice #1234</h1>
  <table class="invoice-table">
    <tr><th>Item</th><th>Quantity</th><th>Price</th></tr>
    <tr><td>Widget Pro</td><td>2</td><td>$20.00</td></tr>
    <tr><td>Gadget Plus</td><td>1</td><td>$25.00</td></tr>
  </table>
  <p class="total">Total: $45.00</p>
</body>
</html>
`;

const response = await fetch('https://api.pdfapi.dev/v1/pdf/html', {
  method: 'POST',
  headers: {
    'Authorization': `Bearer ${process.env.PDFAPI_KEY}`,
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    html,
    options: {
      format: 'A4',
      print_background: true,
      margin: { top: '20mm', bottom: '20mm' }
    }
  }),
});

const buffer = await response.arrayBuffer();
import requests
import os

html = '''
<!DOCTYPE html>
<html>
<head>
  <style>
    body { font-family: Arial, sans-serif; padding: 40px; }
    h1 { color: #2563eb; }
    table { width: 100%; border-collapse: collapse; }
    th, td { border: 1px solid #e5e7eb; padding: 12px; }
  </style>
</head>
<body>
  <h1>Invoice #1234</h1>
  <table>
    <tr><th>Item</th><th>Price</th></tr>
    <tr><td>Widget</td><td>$10.00</td></tr>
  </table>
</body>
</html>
'''

response = requests.post(
    'https://api.pdfapi.dev/v1/pdf/html',
    headers={
        'Authorization': f'Bearer {os.environ["PDFAPI_KEY"]}',
        'Content-Type': 'application/json',
    },
    json={
        'html': html,
        'options': {
            'format': 'A4',
            'print_background': True
        }
    }
)

with open('invoice.pdf', 'wb') as f:
    f.write(response.content)
package main

import (
    "bytes"
    "encoding/json"
    "io"
    "net/http"
    "os"
)

func main() {
    html := `<!DOCTYPE html><html><head><style>
        body { font-family: Arial; padding: 40px; }
        h1 { color: #2563eb; }
    </style></head><body><h1>Invoice</h1></body></html>`

    payload := map[string]interface{}{
        "html": html,
        "options": map[string]interface{}{
            "format":           "A4",
            "print_background": true,
        },
    }

    body, _ := json.Marshal(payload)
    req, _ := http.NewRequest("POST", "https://api.pdfapi.dev/v1/pdf/html", bytes.NewBuffer(body))
    req.Header.Set("Authorization", "Bearer "+os.Getenv("PDFAPI_KEY"))
    req.Header.Set("Content-Type", "application/json")

    client := &http.Client{}
    resp, _ := client.Do(req)
    defer resp.Body.Close()

    pdf, _ := io.ReadAll(resp.Body)
    os.WriteFile("invoice.pdf", pdf, 0644)
}

CSS Support

Supported Features

  • Layout: CSS Grid, Flexbox, floats, positioning
  • Typography: Custom fonts, @font-face, Google Fonts
  • Colors: RGB, HSL, hex, CSS variables
  • Media Queries: Use @media print for print-specific styles
  • Backgrounds: Colors, gradients, images
  • Borders: All border styles, border-radius

Web Fonts

Load web fonts using @import or <link>:

<head>
  <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700&display=swap" rel="stylesheet">
  <style>
    body { font-family: 'Inter', sans-serif; }
  </style>
</head>
@media print {
  .no-print { display: none; }
  .page-break { page-break-after: always; }
  body { font-size: 12pt; }
}

Page Breaks

Control page breaks with CSS:

/* Force page break before element */
.chapter { page-break-before: always; }

/* Force page break after element */
.section-end { page-break-after: always; }

/* Prevent page break inside element */
.keep-together { page-break-inside: avoid; }

Headers and Footers

Add custom headers and footers with dynamic variables:

{
  "html": "...",
  "options": {
    "header": "<div style='font-size: 10px; text-align: center; width: 100%;'>Company Name - Confidential</div>",
    "footer": "<div style='font-size: 10px; width: 100%; display: flex; justify-content: space-between;'><span>Generated on <span class='date'></span></span><span>Page <span class='pageNumber'></span> of <span class='totalPages'></span></span></div>",
    "margin": {
      "top": "40mm",
      "bottom": "30mm"
    }
  }
}

Available Variables

ClassDescription
pageNumberCurrent page number
totalPagesTotal page count
dateCurrent date
titleDocument title
urlDocument URL (if applicable)

Response

Success (200)

Content-Type: application/pdf
Content-Disposition: attachment; filename="document.pdf"

Error Responses

Missing HTML (400)

{
  "error": {
    "code": "VALIDATION_ERROR",
    "message": "HTML content is required"
  }
}

Generation Failed (500)

{
  "error": {
    "code": "PDF_GENERATION_ERROR",
    "message": "Failed to generate PDF",
    "details": {
      "reason": "External resource failed to load"
    }
  }
}