POST
/api/v1/pdfPDF 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)
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
| html | string | * | - | Raw HTML content to convert. Either html or url is required. |
| url | string | * | - | URL to convert to PDF. Either html or url is required. |
| format | string | No | A4 | Page format: A4, Letter, Legal, A3, A5, or Tabloid. |
| width | string | No | - | Custom page width (e.g., "210mm", "8.5in"). Overrides format. |
| height | string | No | - | Custom page height (e.g., "297mm", "11in"). Overrides format. |
| margin | string | object | No | 10mm | Margin size. String for uniform margins, or object with top, right, bottom, left. |
| headerTemplate | string | No | - | HTML template for the page header. Supports variables: date, title, url, pageNumber, totalPages. |
| footerTemplate | string | No | - | HTML template for the page footer. Same variables as headerTemplate. |
| landscape | boolean | No | false | Generate in landscape orientation. |
| printBackground | boolean | No | true | Include 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.pdfJavaScript
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)Go
Go
package main
import (
"bytes"
"encoding/json"
"io"
"net/http"
"os"
)
func main() {
body, _ := json.Marshal(map[string]interface{}{
"url": "https://example.com/report",
"format": "A4",
"margin": map[string]string{
"top": "20mm", "bottom": "20mm",
"left": "15mm", "right": "15mm",
},
"printBackground": true,
})
req, _ := http.NewRequest("POST",
"https://captureapi.dev/api/v1/pdf",
bytes.NewReader(body))
req.Header.Set("X-API-Key", os.Getenv("CAPTURE_API_KEY"))
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
f, _ := os.Create("report.pdf")
defer f.Close()
io.Copy(f, resp.Body)
}Ruby
Ruby
require "net/http"
require "uri"
require "json"
uri = URI("https://captureapi.dev/api/v1/pdf")
req = Net::HTTP::Post.new(uri, "Content-Type" => "application/json")
req["X-API-Key"] = ENV["CAPTURE_API_KEY"]
req.body = JSON.generate({
url: "https://example.com/report",
format: "A4",
margin: { top: "20mm", bottom: "20mm", left: "15mm", right: "15mm" },
printBackground: true
})
res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) { |http|
http.request(req)
}
File.open("report.pdf", "wb") { |f| f.write(res.body) }PHP
PHP
<?php
$payload = json_encode([
"url" => "https://example.com/report",
"format" => "A4",
"margin" => [
"top" => "20mm", "bottom" => "20mm",
"left" => "15mm", "right" => "15mm"
],
"printBackground" => true,
]);
$ch = curl_init("https://captureapi.dev/api/v1/pdf");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"X-API-Key: " . getenv("CAPTURE_API_KEY"),
"Content-Type: application/json"
]);
$response = curl_exec($ch);
curl_close($ch);
file_put_contents("report.pdf", $response);
?>