cURL Examples

Example cURL commands for PDF API.

cURL Examples

cURL is perfect for testing the API from the command line or scripting PDF generation.


Basic Markdown to PDF

curl -X POST https://api.pdfapi.dev/v1/pdf/markdown \
  -H "Authorization: Bearer sk_live_your_key_here" \
  -H "Content-Type: application/json" \
  -d '{"markdown": "# Hello World\n\nThis is my first PDF!"}' \
  --output hello.pdf

With Theme and Options

curl -X POST https://api.pdfapi.dev/v1/pdf/markdown \
  -H "Authorization: Bearer sk_live_your_key_here" \
  -H "Content-Type: application/json" \
  -d '{
    "markdown": "# Monthly Report\n\n## Summary\n\n- Revenue: $45,678\n- Users: 1,234",
    "theme": "corporate",
    "options": {
      "format": "A4",
      "margin": {
        "top": "25mm",
        "bottom": "25mm",
        "left": "20mm",
        "right": "20mm"
      }
    }
  }' \
  --output report.pdf

HTML to PDF

curl -X POST https://api.pdfapi.dev/v1/pdf/html \
  -H "Authorization: Bearer sk_live_your_key_here" \
  -H "Content-Type: application/json" \
  -d '{
    "html": "<!DOCTYPE html><html><head><style>body { font-family: Arial; padding: 40px; } h1 { color: #2563eb; }</style></head><body><h1>Invoice #1234</h1><p>Thank you for your business!</p></body></html>",
    "options": {
      "format": "A4",
      "print_background": true
    }
  }' \
  --output invoice.pdf

URL to PDF

curl -X POST https://api.pdfapi.dev/v1/pdf/url \
  -H "Authorization: Bearer sk_live_your_key_here" \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://example.com",
    "options": {
      "format": "A4",
      "wait_for": 2000,
      "full_page": true
    }
  }' \
  --output webpage.pdf

Template to PDF

curl -X POST https://api.pdfapi.dev/v1/pdf/template \
  -H "Authorization: Bearer sk_live_your_key_here" \
  -H "Content-Type: application/json" \
  -d '{
    "template": "invoice",
    "data": {
      "invoice_number": "INV-2025-001",
      "date": "2025-01-18",
      "customer": {
        "name": "Acme Corp",
        "email": "billing@acme.com"
      },
      "items": [
        {"description": "Widget Pro", "quantity": 2, "unit_price": 29.99}
      ],
      "total": 59.98
    }
  }' \
  --output invoice.pdf

Using Environment Variables

Store your API key in an environment variable:

export PDFAPI_KEY="sk_live_your_key_here"

curl -X POST https://api.pdfapi.dev/v1/pdf/markdown \
  -H "Authorization: Bearer $PDFAPI_KEY" \
  -H "Content-Type: application/json" \
  -d '{"markdown": "# Hello World"}' \
  --output hello.pdf

Reading Markdown from File

# Create a markdown file
cat > document.md << 'EOF'
# Project Proposal

## Overview

This proposal outlines our plan for...

## Timeline

| Phase | Duration | Deliverables |
|-------|----------|--------------|
| Planning | 2 weeks | Requirements doc |
| Development | 6 weeks | Working prototype |
| Testing | 2 weeks | QA report |

## Budget

Total: $50,000
EOF

# Convert to PDF using jq to properly escape
curl -X POST https://api.pdfapi.dev/v1/pdf/markdown \
  -H "Authorization: Bearer $PDFAPI_KEY" \
  -H "Content-Type: application/json" \
  -d "$(jq -n --rawfile md document.md '{markdown: $md, theme: "corporate"}')" \
  --output proposal.pdf

Error Handling

Check the HTTP status code and parse error responses:

response=$(curl -s -w "\n%{http_code}" -X POST https://api.pdfapi.dev/v1/pdf/markdown \
  -H "Authorization: Bearer $PDFAPI_KEY" \
  -H "Content-Type: application/json" \
  -d '{"markdown": ""}')

status_code=$(echo "$response" | tail -n1)
body=$(echo "$response" | sed '$d')

if [ "$status_code" -eq 200 ]; then
  echo "$body" > document.pdf
  echo "PDF saved successfully"
else
  echo "Error: $body"
fi

Batch Processing

Process multiple files in a loop:

#!/bin/bash

for file in documents/*.md; do
  filename=$(basename "$file" .md)
  echo "Converting $file..."

  curl -s -X POST https://api.pdfapi.dev/v1/pdf/markdown \
    -H "Authorization: Bearer $PDFAPI_KEY" \
    -H "Content-Type: application/json" \
    -d "$(jq -n --rawfile md "$file" '{markdown: $md}')" \
    --output "output/${filename}.pdf"

  # Respect rate limits
  sleep 1
done

Check Usage

curl https://api.pdfapi.dev/v1/usage \
  -H "Authorization: Bearer $PDFAPI_KEY" | jq

Output:

{
  "data": {
    "plan": "pro",
    "pdfs_generated": 156,
    "pdfs_limit": 3000,
    "pdfs_remaining": 2844
  }
}

List API Keys

curl https://api.pdfapi.dev/v1/keys \
  -H "Authorization: Bearer $PDFAPI_KEY" | jq

Tips

  1. Use -s for silent mode in scripts to suppress progress bars
  2. Use jq for JSON parsing and construction
  3. Respect rate limits with sleep between requests
  4. Store output as binary with --output flag
  5. Check status codes for error handling