POST/api/v1/pdf

PDF Generation API

Convert any HTML content or publicly accessible URL into a beautifully formatted PDF document. Full control over page dimensions, margins, headers, footers, and print media styles.

Request Body (JSON)

ParameterTypeRequiredDefaultDescription
htmlstring*-Raw HTML content to convert. Either html or url is required.
urlstring*-URL to convert to PDF. Either html or url is required.
formatstringNoA4Page format: A4, Letter, Legal, A3, A5, or Tabloid.
widthstringNo-Custom page width (e.g., "210mm", "8.5in"). Overrides format.
heightstringNo-Custom page height (e.g., "297mm", "11in"). Overrides format.
marginstring | objectNo10mmMargin size. String for uniform margins, or object with top, right, bottom, left.
headerTemplatestringNo-HTML template for the page header. Supports variables: date, title, url, pageNumber, totalPages.
footerTemplatestringNo-HTML template for the page footer. Same variables as headerTemplate.
landscapebooleanNofalseGenerate in landscape orientation.
printBackgroundbooleanNotrueInclude background colors and images in the PDF.

Code Examples

cURL

cURL
# From URL
curl -X POST "https://captureapi.dev/api/v1/pdf" \
  -H "X-API-Key: cap_your_key" \
  -H "Content-Type: application/json" \
  -d '{"url": "https://example.com", "format": "A4", "margin": "20mm"}' \
  -o document.pdf

# From HTML
curl -X POST "https://captureapi.dev/api/v1/pdf" \
  -H "X-API-Key: cap_your_key" \
  -H "Content-Type: application/json" \
  -d '{
    "html": "<h1>Invoice #1234</h1><p>Amount: $99.00</p>",
    "format": "Letter",
    "margin": {"top": "25mm", "bottom": "25mm", "left": "15mm", "right": "15mm"},
    "headerTemplate": "<div style=\"font-size:10px;text-align:center;width:100%\">My Company</div>",
    "footerTemplate": "<div style=\"font-size:10px;text-align:center;width:100%\">Page <span class=\"pageNumber\"></span> of <span class=\"totalPages\"></span></div>"
  }' \
  -o invoice.pdf

JavaScript

JavaScript
async function generatePdf(options) {
  const response = await fetch("https://captureapi.dev/api/v1/pdf", {
    method: "POST",
    headers: {
      "X-API-Key": process.env.CAPTURE_API_KEY,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      html: options.html,
      url: options.url,
      format: options.format || "A4",
      margin: options.margin || "10mm",
      landscape: options.landscape || false,
      printBackground: true,
    }),
  });

  if (!response.ok) {
    const error = await response.json();
    throw new Error(error.error.message);
  }

  return response.arrayBuffer();
}

// Generate invoice PDF
const pdf = await generatePdf({
  html: `
    <html>
      <body>
        <h1>Invoice #1234</h1>
        <table>
          <tr><td>Service</td><td>$99.00</td></tr>
          <tr><td><strong>Total</strong></td><td><strong>$99.00</strong></td></tr>
        </table>
      </body>
    </html>
  `,
  format: "Letter",
  margin: { top: "25mm", bottom: "25mm", left: "15mm", right: "15mm" }
});

Python

Python
import requests
import os

def generate_pdf(html=None, url=None, **kwargs):
    payload = {
        "format": kwargs.get("format", "A4"),
        "margin": kwargs.get("margin", "10mm"),
        "landscape": kwargs.get("landscape", False),
        "printBackground": kwargs.get("print_background", True),
    }
    if html:
        payload["html"] = html
    elif url:
        payload["url"] = url
    else:
        raise ValueError("Either html or url is required")

    response = requests.post(
        "https://captureapi.dev/api/v1/pdf",
        json=payload,
        headers={"X-API-Key": os.environ["CAPTURE_API_KEY"]}
    )
    response.raise_for_status()
    return response.content

# Usage
pdf_bytes = generate_pdf(
    url="https://example.com/report",
    format="A4",
    margin={"top": "20mm", "bottom": "20mm", "left": "15mm", "right": "15mm"}
)
with open("report.pdf", "wb") as f:
    f.write(pdf_bytes)