> ## Documentation Index
> Fetch the complete documentation index at: https://docs.peedief.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Generate PDF from Template

> Generate a PDF by rendering a named template with provided context data

## Generate PDF from Template

Create a PDF using your custom template and structured JSON data. This is the primary endpoint for template-based PDF generation.

### Path Parameters

* `templateName` (string, required): The name of your custom template created in the dashboard

### Request Headers

* `x-api-key` (string, required): Your API key for authentication
* `Content-Type`: `application/json`

### Request Body

```json theme={null}
{
  "contextJson": {
    // Your template data - structure depends on your template
    "customerName": "John Doe",
    "invoiceNumber": "INV-001",
    "amount": 150.00
  },
  "fileName": "optional-filename.pdf"
}
```

### Response

**Success (200)**:

```json theme={null}
{
  "success": true,
  "downloadUrl": "https://peedief.com/download/abc123...",
  "previewUrl": "https://peedief.com/preview/abc123...",
  "fileName": "invoice-001.pdf",
  "fileSize": 245760,
  "templateId": "tpl_abc123",
  "templateName": "invoice"
}
```

Use `previewUrl` as the `src` for an iframe (or `<object>` tag) when you want to render a read-only preview directly in your UI. The URL is short-lived and expires alongside the download link.

**Error (400/401/404)**:

```json theme={null}
{
  "success": false,
  "error": "Template not found",
  "code": "TEMPLATE_NOT_FOUND"
}
```

### Example Request

```bash theme={null}
curl -X POST "https://peedief.com/api/templates/by-name/my-invoice-template/pdf" \
  -H "x-api-key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "contextJson": {
      "companyName": "Acme Corp",
      "customerInfo": {
        "name": "Jane Smith",
        "email": "jane@example.com",
        "address": "123 Main St, City, State 12345"
      },
      "items": [
        {
          "description": "Product A",
          "quantity": 2,
          "price": 50.00
        }
      ],
      "total": 100.00,
      "dueDate": "2024-02-15"
    },
    "fileName": "invoice-jane-smith.pdf"
  }'
```

### Template Data Structure

The `contextJson` object structure depends entirely on your template design. Common patterns include:

**Certificate Template**:

```json theme={null}
{
  "contextJson": {
    "recipientName": "John Smith",
    "courseName": "Advanced JavaScript",
    "completionDate": "2024-01-30",
    "instructor": "Dr. Sarah Wilson",
    "grade": "A+"
  }
}
```

**Invoice Template**:

```json theme={null}
{
  "contextJson": {
    "invoiceNumber": "INV-2024-001",
    "customerName": "TechStart Inc",
    "services": [
      {
        "description": "Web Development",
        "hours": 40,
        "rate": 125.00
      }
    ],
    "subtotal": 5000.00,
    "taxRate": 0.08,
    "total": 5400.00
  }
}
```

### Error Codes

* `TEMPLATE_NOT_FOUND`: Template with specified name doesn't exist
* `INVALID_API_KEY`: API key is invalid or expired
* `MISSING_TEMPLATE_DATA`: Required template fields are missing
* `QUOTA_EXCEEDED`: Account has exceeded PDF generation limits


## OpenAPI

````yaml POST /api/templates/by-name/{templateName}/pdf
openapi: 3.1.0
info:
  title: Peedief API
  description: Convert HTML to PDF with Peedief API
  version: 1.0.0
servers:
  - url: https://peedief.com
security:
  - apiKeyAuth: []
paths:
  /api/templates/by-name/{templateName}/pdf:
    post:
      summary: Generate PDF from template by name
      description: Generate a PDF by rendering a named template with provided context data
      parameters:
        - name: templateName
          in: path
          required: true
          description: URL-encoded name of the template to use for PDF generation
          schema:
            type: string
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/TemplatePDFRequest'
      responses:
        '200':
          description: PDF generated successfully
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/TemplatePDFResponse'
        '400':
          description: Bad request - invalid input or template rendering failed
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '401':
          description: Unauthorized - no valid session
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '404':
          description: Template not found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '500':
          description: Internal server error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
components:
  schemas:
    TemplatePDFRequest:
      type: object
      required:
        - contextJson
      properties:
        contextJson:
          type: object
          description: JSON object containing the data to populate the template variables
          additionalProperties: true
        fileName:
          type: string
          description: >-
            Optional filename for the generated PDF (defaults to
            templateName-timestamp.pdf)
    TemplatePDFResponse:
      type: object
      properties:
        success:
          type: boolean
          description: Whether the PDF generation was successful
          example: true
        downloadUrl:
          type: string
          description: Signed URL to download the generated PDF (expires in 1 hour)
        previewUrl:
          type: string
          description: >-
            Signed URL to preview the generated PDF in an iframe (expires in 1
            hour)
        fileName:
          type: string
          description: Name of the generated PDF file
        fileSize:
          type: integer
          description: Size of the generated PDF in bytes
        templateId:
          type: string
          description: ID of the template used for generation
        templateName:
          type: string
          description: Name of the template used for generation
    Error:
      type: object
      properties:
        success:
          type: boolean
          example: false
        error:
          type: string
          description: Error message
        details:
          type: array
          description: Additional error details (for validation errors)
          items:
            type: object
  securitySchemes:
    apiKeyAuth:
      type: apiKey
      in: header
      name: x-api-key

````