Code Examples

Practical examples for generating PDFs with the DocFast API — invoices, reports, receipts, and integration guides.

Generate an Invoice PDF

Create a professional invoice with inline CSS and convert it to PDF with a single API call.

HTML — invoice.html
<html>
<body style="font-family: sans-serif; padding: 40px; color: #333;">
  <div style="display: flex; justify-content: space-between;">
    <div>
      <h1 style="margin: 0; color: #111;">INVOICE</h1>
      <p style="color: #666;">#INV-2026-0042</p>
    </div>
    <div style="text-align: right;">
      <strong>Acme Corp</strong><br>
      123 Main St<br>
      hello@acme.com
    </div>
  </div>

  <table style="width: 100%; border-collapse: collapse; margin-top: 40px;">
    <tr style="border-bottom: 2px solid #111;">
      <th style="text-align: left; padding: 8px 0;">Item</th>
      <th style="text-align: right; padding: 8px 0;">Qty</th>
      <th style="text-align: right; padding: 8px 0;">Price</th>
    </tr>
    <tr style="border-bottom: 1px solid #eee;">
      <td style="padding: 12px 0;">API Pro Plan (monthly)</td>
      <td style="text-align: right;">1</td>
      <td style="text-align: right;">$49.00</td>
    </tr>
    <tr>
      <td style="padding: 12px 0;">Extra PDF renders (500)</td>
      <td style="text-align: right;">500</td>
      <td style="text-align: right;">$15.00</td>
    </tr>
  </table>

  <p style="text-align: right; font-size: 1.4em; margin-top: 24px;">
    <strong>Total: $64.00</strong>
  </p>
</body>
</html>
curl
curl -X POST https://docfast.dev/v1/convert/html \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"html": "<html>...your invoice HTML...</html>"}' \
  --output invoice.pdf

Convert Markdown to PDF

Send Markdown content directly — DocFast renders it with clean typography and outputs a styled PDF.

curl
curl -X POST https://docfast.dev/v1/convert/markdown \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "markdown": "# Project Report\n\n## Summary\n\nQ4 revenue grew **32%** year-over-year.\n\n## Key Metrics\n\n| Metric | Value |\n|--------|-------|\n| Revenue | $1.2M |\n| Users | 45,000 |\n| Uptime | 99.97% |\n\n## Next Steps\n\n1. Launch mobile SDK\n2. Expand EU infrastructure\n3. SOC 2 certification"
  }' \
  --output report.pdf

HTML Report with Charts

Embed inline SVG charts in your HTML for data-driven reports — no JavaScript or external libraries needed.

HTML — report with SVG bar chart
<html>
<body style="font-family: sans-serif; padding: 40px;">
  <h1>Quarterly Revenue</h1>

  <svg width="400" height="200" viewBox="0 0 400 200">
    <!-- Bars -->
    <rect x="20"  y="120" width="60" height="80"  fill="#34d399"/>
    <rect x="110" y="80"  width="60" height="120" fill="#34d399"/>
    <rect x="200" y="50"  width="60" height="150" fill="#34d399"/>
    <rect x="290" y="20"  width="60" height="180" fill="#34d399"/>
    <!-- Labels -->
    <text x="50"  y="115" text-anchor="middle" font-size="12">$80k</text>
    <text x="140" y="75"  text-anchor="middle" font-size="12">$120k</text>
    <text x="230" y="45"  text-anchor="middle" font-size="12">$150k</text>
    <text x="320" y="15"  text-anchor="middle" font-size="12">$180k</text>
  </svg>
</body>
</html>
curl
curl -X POST https://docfast.dev/v1/convert/html \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d @report.json \
  --output chart-report.pdf

Receipt / Confirmation PDF

Generate a simple receipt or order confirmation — perfect for e-commerce and SaaS billing.

HTML — receipt template
<html>
<body style="font-family: sans-serif; max-width: 400px; margin: 0 auto; padding: 40px;">
  <div style="text-align: center; margin-bottom: 24px;">
    <h2 style="margin: 0;">Payment Receipt</h2>
    <p style="color: #888;">Feb 20, 2026</p>
  </div>

  <hr style="border: none; border-top: 1px dashed #ccc;">

  <p><strong>Order:</strong> #ORD-98712</p>
  <p><strong>Customer:</strong> jane@example.com</p>

  <table style="width: 100%; margin: 16px 0;">
    <tr>
      <td>Pro Plan</td>
      <td style="text-align: right;">$29.00</td>
    </tr>
    <tr>
      <td>Tax</td>
      <td style="text-align: right;">$2.90</td>
    </tr>
  </table>

  <hr style="border: none; border-top: 1px dashed #ccc;">

  <p style="text-align: right; font-size: 1.3em;">
    <strong>Total: $31.90</strong>
  </p>
  <p style="text-align: center; color: #34d399; margin-top: 24px;">
    ✓ Payment successful
  </p>
</body>
</html>

URL to PDF

Capture a live webpage and convert it to PDF. Send a URL to the /v1/convert/url endpoint and get a rendered PDF back. JavaScript is disabled for security (SSRF protection), and private/internal URLs are blocked.

curl — basic
curl -X POST https://docfast.dev/v1/convert/url \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"url": "https://example.com"}' \
  --output page.pdf
curl — with options
curl -X POST https://docfast.dev/v1/convert/url \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://example.com",
    "format": "A4",
    "margin": { "top": "20mm", "bottom": "20mm" },
    "scale": 0.8,
    "printBackground": true
  }' \
  --output page.pdf

Node.js Integration

A complete Node.js script to generate a PDF and save it to disk. Works with Node 18+ using native fetch.

JavaScript — generate-pdf.mjs
const html = `
  <h1>Hello from Node.js</h1>
  <p>Generated at ${new Date().toISOString()}</p>
`;

const res = await fetch("https://docfast.dev/v1/convert/html", {
  method: "POST",
  headers: {
    "Authorization": `Bearer ${process.env.DOCFAST_API_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({ html }),
});

if (!res.ok) throw new Error(`API error: ${res.status}`);

const buffer = Buffer.from(await res.arrayBuffer());
await import("fs").then(fs =>
  fs.writeFileSync("output.pdf", buffer)
);

console.log("✓ Saved output.pdf");

Python Integration

Generate a PDF from Python using the requests library. Drop this into any Flask, Django, or FastAPI app.

Python — generate_pdf.py
import os
import requests

html = """
<h1>Hello from Python</h1>
<p>This PDF was generated via the DocFast API.</p>
<ul>
  <li>Fast rendering</li>
  <li>Pixel-perfect output</li>
  <li>Simple REST API</li>
</ul>
"""

response = requests.post(
    "https://docfast.dev/v1/convert/html",
    headers={
        "Authorization": f"Bearer {os.environ['DOCFAST_API_KEY']}",
        "Content-Type": "application/json",
    },
    json={"html": html},
)

response.raise_for_status()

with open("output.pdf", "wb") as f:
    f.write(response.content)

print("✓ Saved output.pdf")

Go Integration

SDK coming soon. In the meantime, use the HTTP example below — it works with any HTTP client.

Go — generate-pdf.go
package main

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

func main() {
    body, _ := json.Marshal(map[string]string{
        "html": "<h1>Hello</h1><p>Generated with DocFast</p>",
    })

    req, _ := http.NewRequest("POST", "https://docfast.dev/v1/convert/html", bytes.NewReader(body))
    req.Header.Set("Authorization", "Bearer "+os.Getenv("DOCFAST_API_KEY"))
    req.Header.Set("Content-Type", "application/json")

    resp, err := http.DefaultClient.Do(req)
    if err != nil { panic(err) }
    defer resp.Body.Close()

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

PHP Integration

SDK coming soon. In the meantime, use the HTTP example below — it works with any HTTP client. Laravel: Use this in any controller or Artisan command.

PHP — generate-pdf.php
<?php
$html = '<h1>Hello</h1><p>Generated with DocFast</p>';

$options = [
    'http' => [
        'method'  => 'POST',
        'header'  => implode("\r\n", [
            'Authorization: Bearer ' . getenv('DOCFAST_API_KEY'),
            'Content-Type: application/json',
        ]),
        'content' => json_encode(['html' => $html]),
    ],
];

$pdf = file_get_contents('https://docfast.dev/v1/convert/html', false, stream_context_create($options));
file_put_contents('output.pdf', $pdf);
echo "✓ Saved output.pdf\n";