Generate PDF

Generate a PDF from a template with custom variable values.

POST/functions/v1/generate-pdf-preview

Generate a PDF document from a template

Request

Headers

HeaderValueRequired
x-api-keyYour API keyYes
Content-Typeapplication/jsonYes

Body Parameters

ParameterTypeRequiredDescription
templateIdstringRequiredUUID of the template to use for PDF generation
variablesobjectRequiredKey-value pairs for template variables

Request Example

curl -X POST https://api.pdf-sign.com/functions/v1/generate-pdf-preview \
  -H "x-api-key: pdfsign_your_api_key" \
  -H "Content-Type: application/json" \
  -d '{
    "templateId": "550e8400-e29b-41d4-a716-446655440000",
    "variables": {
      "clientName": "John Doe",
      "invoiceNumber": "INV-001",
      "amount": "1000.00",
      "date": "2024-01-15"
    }
  }'

Response

Success Response (200 OK)

{
  "success": true,
  "pdf_url": "https://api.pdf-sign.com/storage/v1/object/public/pdfs/generated-pdf.pdf",
  "generation_id": "660f9500-f39c-52e5-b827-557766551111",
  "file_size": 45678
}

Response Fields

FieldTypeDescription
successbooleanIndicates if the request was successful
pdf_urlstringPublic URL to download the generated PDF
generation_idstringUnique identifier for this generation
file_sizenumberFile size in bytes

Error Responses

400 Bad Request

Missing required parameters or invalid data.

{
  "error": "Missing required parameter: templateId",
  "code": "BAD_REQUEST",
  "status": 400
}

401 Unauthorized

Invalid or missing API key.

{
  "error": "Invalid API key",
  "code": "UNAUTHORIZED",
  "status": 401
}

403 Forbidden

Quota exceeded or template not accessible.

{
  "error": "Monthly quota exceeded",
  "code": "QUOTA_EXCEEDED",
  "status": 403
}

404 Not Found

Template not found.

{
  "error": "Template not found",
  "code": "NOT_FOUND",
  "status": 404
}

Code Examples

JavaScript

async function generatePDF(templateId, variables) {
  const response = await fetch(
    'https://api.pdf-sign.com/functions/v1/generate-pdf-preview',
    {
      method: 'POST',
      headers: {
        'x-api-key': process.env.PDFSIGN_API_KEY,
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({ templateId, variables }),
    }
  );

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

  return response.json();
}

// Usage
const result = await generatePDF('your-template-id', {
  clientName: 'John Doe',
  amount: '1000.00',
});

console.log('PDF URL:', result.pdf_url);

Python

import requests
import os

def generate_pdf(template_id: str, variables: dict) -> dict:
    response = requests.post(
        'https://api.pdf-sign.com/functions/v1/generate-pdf-preview',
        headers={
            'x-api-key': os.environ['PDFSIGN_API_KEY'],
            'Content-Type': 'application/json',
        },
        json={
            'templateId': template_id,
            'variables': variables,
        }
    )

    response.raise_for_status()
    return response.json()

# Usage
result = generate_pdf('your-template-id', {
    'clientName': 'John Doe',
    'amount': '1000.00',
})

print(f"PDF URL: {result['pdf_url']}")

PHP

<?php

function generatePDF($templateId, $variables) {
    $ch = curl_init('https://api.pdf-sign.com/functions/v1/generate-pdf-preview');

    curl_setopt_array($ch, [
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POST => true,
        CURLOPT_HTTPHEADER => [
            'x-api-key: ' . getenv('PDFSIGN_API_KEY'),
            'Content-Type: application/json',
        ],
        CURLOPT_POSTFIELDS => json_encode([
            'templateId' => $templateId,
            'variables' => $variables,
        ]),
    ]);

    $response = curl_exec($ch);
    curl_close($ch);

    return json_decode($response, true);
}

// Usage
$result = generatePDF('your-template-id', [
    'clientName' => 'John Doe',
    'amount' => '1000.00',
]);

echo "PDF URL: " . $result['pdf_url'];

Variable Matching

All variables defined in your template must be provided in the variables object. Missing variables will result in a 400 Bad Request error.