---
name: create-pdf-report
description: "Generate professional PDF reports with tables, charts, and styled layouts."
whenToUse: "User asks to create a PDF document or report"
---

# Skill: Create PDF Report

## Description
Generate professional PDF reports with Chinese font support, tables, charts, and styled layouts using Python reportlab. Use this skill whenever the user asks to create a PDF, report, analysis document, or any deliverable in PDF format.

## When to Use
- User asks to create a PDF document or report
- User asks to generate an analysis, summary, or briefing as a downloadable file
- User asks to "整理成PDF" or "发给我" with document content
- Any task requiring formatted document output

## Prerequisites
- Python with `reportlab` library (confirmed available)
- Chinese font: `C:/Windows/Fonts/msyh.ttc` (Microsoft YaHei) and `C:/Windows/Fonts/msyhbd.ttc` (Bold)

## Steps

### 1. Register Chinese Fonts
```python
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont

pdfmetrics.registerFont(TTFont('MSYH', "C:/Windows/Fonts/msyh.ttc", subfontIndex=0))
pdfmetrics.registerFont(TTFont('MSYHB', "C:/Windows/Fonts/msyhbd.ttc", subfontIndex=0))
```

### 2. Use ParagraphStyle Helper
Define a helper function to avoid verbose style definitions:
```python
from reportlab.lib.styles import ParagraphStyle
from reportlab.lib.enums import TA_LEFT, TA_CENTER, TA_JUSTIFY

def S(name, **kw):
    defaults = dict(fontName='MSYH', fontSize=10.5, leading=18, textColor=DARK)
    defaults.update(kw)
    return ParagraphStyle(name, **defaults)
```

### 3. Standard Color Palette
```python
from reportlab.lib.colors import HexColor, white

PRIMARY = HexColor('#1a73e8')   # Blue
DARK = HexColor('#1a1a2e')      # Dark text
ACCENT = HexColor('#e94560')    # Red accent
GREEN = HexColor('#27ae60')     # Green
ORANGE = HexColor('#f39c12')    # Orange
GRAY = HexColor('#666666')      # Gray text
LGRAY = HexColor('#e0e0e0')     # Light gray borders
LIGHT_BLUE = HexColor('#e8f0fe')
LIGHT_GREEN = HexColor('#e8f5e9')
LIGHT_RED = HexColor('#fce4ec')
```

### 4. Document Structure
```python
from reportlab.lib.pagesizes import A4
from reportlab.lib.units import mm, cm
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle, PageBreak, HRFlowable

doc = SimpleDocTemplate(output_path, pagesize=A4, 
    rightMargin=2*cm, leftMargin=2*cm, topMargin=2*cm, bottomMargin=2*cm)
story = []
```

### 5. Table Best Practices
- Always use Paragraph objects inside table cells (not plain strings) for Chinese text
- Set VALIGN to 'MIDDLE' or 'TOP'
- Use alternating row backgrounds for readability
- Header rows: colored background + white text

### 6. Output Path
- Save to: `~/Desktop/{filename}.pdf` or system temp directory as `{filename}.pdf`
- Include full file path in response message so bridge can auto-send

### 7. Important Notes
- **Encoding**: Use `sys.stdout.reconfigure(encoding='utf-8')` if printing Chinese to stdout
- **Emoji**: Avoid emoji in print statements (cp1252 encoding error). Use emoji only in PDF content via Paragraph objects
- **String escaping**: Be careful with nested quotes in Chinese strings — use unicode escapes or triple-quoted strings
- **NEVER use Unicode subscript/superscript characters** in reportlab — use `<sub>` and `<super>` tags instead

## Verification
After generating, verify with:
```python
print(f"PDF saved to: {output_path}")
print(f"File size: {os.path.getsize(output_path) / 1024:.1f} KB")
```
