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

# File Generation Skill

> Generate files in various formats from structured data including JSON, CSV, PDF, and TXT with template support.

<CardGroup cols={2}>
  <Card title="Type" icon="tag">
    `file_generation`
  </Card>

  <Card title="Formats" icon="file">
    JSON, CSV, PDF, TXT, Markdown
  </Card>
</CardGroup>

**Purpose:** The File Generation skill enables agents to create formatted files from structured data, supporting various output formats for reports, exports, and documentation.

***

## Common Use Cases

<CardGroup cols={2}>
  <Card icon="file-pdf">
    **Creating reports and documents**

    Generate PDF reports, executive summaries, and formatted documents
  </Card>

  <Card icon="table">
    **Exporting data to different formats**

    Convert data to CSV for spreadsheets, JSON for APIs
  </Card>

  <Card icon="file-code">
    **Generating configuration files**

    Create YAML, JSON, or INI configuration files from templates
  </Card>

  <Card icon="file-lines">
    **Template-based file creation**

    Use Jinja2 or custom templates to generate dynamic content
  </Card>
</CardGroup>

***

## Supported Formats

| Format            | Use Cases                    | Key Features                                          |
| ----------------- | ---------------------------- | ----------------------------------------------------- |
| **JSON**          | API exports, config files    | Pretty printing, schema validation, nested structures |
| **CSV**           | Data exports, spreadsheets   | Custom delimiters, headers, Excel compatibility       |
| **PDF**           | Reports, invoices, documents | Custom styling, images, multi-page support            |
| **Text/Markdown** | Documentation, READMEs       | Markdown formatting, template substitution            |

<Tip>
  **Format Selection:** Use CSV for data exports (fast, small), PDF for formatted reports (professional), JSON for API/config (structured).
</Tip>

***

## Configuration

**Example Configuration:**

```json theme={null}
{
  "output_directory": "/opt/generated",
  "allowed_formats": ["json", "csv", "pdf", "txt"],
  "template_directory": "/opt/templates",
  "template_engine": "jinja2",
  "json_indent": 2
}
```

<AccordionGroup>
  <Accordion title="📋 Full Configuration Reference" icon="gear">
    | Parameter            | Type   | Default                 | Description                   |
    | -------------------- | ------ | ----------------------- | ----------------------------- |
    | `output_directory`   | string | "/"                     | Where files are saved         |
    | `allowed_formats`    | array  | \["json", "csv", "txt"] | Permitted file formats        |
    | `template_directory` | string | "/templates"            | Template file location        |
    | `template_engine`    | string | "jinja2"                | Template engine to use        |
    | `json_indent`        | number | 2                       | JSON pretty print indentation |
    | `csv_delimiter`      | string | ","                     | CSV field separator           |
    | `encoding`           | string | "utf-8"                 | File encoding                 |
    | `pdf_engine`         | string | "reportlab"             | PDF generation library        |
    | `pdf_page_size`      | string | "A4"                    | Page size (A4, Letter, etc.)  |
  </Accordion>
</AccordionGroup>

***

## Quick Start

```bash theme={null}
# Create skill
kubiya skill create --name "Report Generator" --type file_generation --enabled

# Associate with agent
kubiya skill associate agent <agent-id> <skill-id>
```

<Card title="View Complete Examples" icon="lightbulb" href="/core-concepts/skills/examples#report-generation">
  See full template usage, PDF generation patterns, and multi-format export examples
</Card>

***

## Template Usage

### Jinja2 Templates

Create reusable templates for consistent file generation:

**Template File** (`/opt/templates/report.md.j2`):

```jinja2 theme={null}
# {{ report_title }}

**Generated:** {{ timestamp }}

## Summary

{% for metric in metrics %}
- **{{ metric.name }}:** {{ metric.value }}{{ metric.unit }}
{% endfor %}

---
*Report ID: {{ report_id }}*
```

**Agent Usage:**

```python theme={null}
data = {
    "report_title": "System Health Report",
    "timestamp": "2024-01-15 10:30:00",
    "metrics": [
        {"name": "CPU Usage", "value": 45, "unit": "%"},
        {"name": "Memory Usage", "value": 62, "unit": "%"}
    ],
    "report_id": "RPT-2024-001"
}

# Generate file from template
generate_file(template="report.md.j2", output="health-report.md", data=data)
```

***

## Security Best Practices

<AccordionGroup>
  <Accordion title="Restrict Output Directory" icon="folder-lock">
    Always specify `output_directory` to limit where files can be written.

    ```json theme={null}
    output_directory: "/opt/generated"  # Files can only be saved here
    ```
  </Accordion>

  <Accordion title="Whitelist Formats" icon="filter">
    Use `allowed_formats` to control which file types can be generated.

    ```json theme={null}
    allowed_formats: ["json", "csv", "txt"]  # No executable formats
    ```
  </Accordion>

  <Accordion title="Validate Template Input" icon="shield-check">
    Sanitize data before passing to templates to prevent injection attacks.

    ```python theme={null}
    from html import escape
    data["user_input"] = escape(data["user_input"])
    ```
  </Accordion>
</AccordionGroup>

<Tip>
  **Performance Tip:** For large data exports, use CSV instead of PDF for better performance and smaller file sizes.
</Tip>

***

## Troubleshooting & Related Skills

<AccordionGroup>
  <Accordion title="Template Not Found" icon="file-slash">
    **Solutions:**

    * Verify `template_directory` path is correct
    * Check template file exists: `ls /opt/templates/`
    * Ensure worker has read permissions on template directory
  </Accordion>

  <Accordion title="PDF Generation Fails" icon="file-pdf">
    **Solutions:**

    * Install required PDF library: `pip install reportlab` or `pip install weasyprint`
    * Verify font files are available if using custom fonts
    * Check PDF engine configuration matches installed library
  </Accordion>

  <Accordion title="Permission Denied on Output" icon="ban">
    **Solutions:**

    * Verify `output_directory` exists: `mkdir -p /opt/generated`
    * Check worker user has write permissions
    * Ensure disk space is available: `df -h`
  </Accordion>
</AccordionGroup>

### Related Skills

<CardGroup cols={2}>
  <Card title="File System Skill" icon="folder-open" href="/core-concepts/skills/file-system">
    Read template files and manage output
  </Card>

  <Card title="Python Skill" icon="python" href="/core-concepts/skills/python">
    Process data before file generation
  </Card>

  <Card title="Data Visualization" icon="chart-line" href="/core-concepts/skills/data-visualization">
    Create visual diagrams and charts
  </Card>

  <Card title="View All Skills" icon="layer-group" href="/core-concepts/skills/built-in-skills">
    Return to built-in skills overview
  </Card>
</CardGroup>
