模板
报告
带章节和指标的专业报告 PDF 组件
概述
Report 组件生成结构化报告 PDF,包含标题页、章节、关键指标和数据表格。适用于商业报告、季度回顾和分析摘要。
用法
import "github.com/gpdf-dev/gpdf/template"
doc := template.Report(template.ReportData{
Title: "Quarterly Report",
Subtitle: "Q1 2026 - Financial Summary",
Author: "ACME Corporation",
Date: "April 1, 2026",
Sections: []template.ReportSection{
{
Title: "Executive Summary",
Content: "This report presents the financial performance of ACME Corporation " +
"for the first quarter of 2026. Revenue increased by 15% compared to Q4 2025, " +
"driven primarily by strong growth in the cloud services division.",
Metrics: []template.ReportMetric{
{Label: "Revenue", Value: "$12.5M", ColorHex: 0x2E7D32},
{Label: "Growth", Value: "+15%", ColorHex: 0x2E7D32},
{Label: "Customers", Value: "2,450", ColorHex: 0x1565C0},
{Label: "Margin", Value: "22%", ColorHex: 0x1565C0},
},
},
{
Title: "Revenue Breakdown",
Table: &template.ReportTable{
Header: []string{"Division", "Q1 2026", "Q4 2025", "Change"},
ColumnWidths: []float64{35, 22, 22, 21},
Rows: [][]string{
{"Cloud Services", "$5,200,000", "$4,100,000", "+26.8%"},
{"Enterprise Software", "$3,800,000", "$3,500,000", "+8.6%"},
{"Consulting", "$2,100,000", "$1,900,000", "+10.5%"},
{"Support & Maintenance", "$1,400,000", "$1,350,000", "+3.7%"},
},
},
},
{
Title: "Expense Summary",
Table: &template.ReportTable{
Header: []string{"Category", "Amount", "% of Revenue"},
ColumnWidths: []float64{40, 30, 30},
Rows: [][]string{
{"Personnel", "$5,500,000", "44.0%"},
{"Infrastructure", "$1,800,000", "14.4%"},
{"Marketing", "$1,200,000", "9.6%"},
{"R&D", "$950,000", "7.6%"},
{"General & Admin", "$300,000", "2.4%"},
},
},
},
},
})
data, err := doc.Generate()
┌─ Page 1 (Title) ─────────────────────────────────┐
│ │
│ │
│ Quarterly Report │
│ Q1 2026 - Financial Summary │
│ │
│ ACME Corporation │
│ April 1, 2026 │
│ │
│ │
└───────────────────────────────────────────────────┘
┌─ Page 2 ──────────────────────────────────────────┐
│ │
│ Executive Summary │
│ ──────────────────────────────────────────────── │
│ This report presents the financial performance │
│ of ACME Corporation for Q1 2026... │
│ │
│ ┌──────────┬──────────┬──────────┬──────────┐ │
│ │ Revenue │ Growth │Customers │ Margin │ │
│ │ $12.5M │ +15% │ 2,450 │ 22% │ │
│ └──────────┴──────────┴──────────┴──────────┘ │
│ │
│ Revenue Breakdown │
│ ──────────────────────────────────────────────── │
│ ┌────────────────┬──────────┬──────────┬──────┐ │
│ │ Division │ Q1 2026 │ Q4 2025 │Change│ │
│ ├────────────────┼──────────┼──────────┼──────┤ │
│ │ Cloud Services │$5,200,000│$4,100,000│+26.8%│ │
│ │ Enterprise SW │$3,800,000│$3,500,000│ +8.6%│ │
│ │ Consulting │$2,100,000│$1,900,000│+10.5%│ │
│ │ Support │$1,400,000│$1,350,000│ +3.7%│ │
│ └────────────────┴──────────┴──────────┴──────┘ │
│ │
└───────────────────────────────────────────────────┘
数据类型
ReportData
| 字段 | 类型 | 说明 |
|---|---|---|
Title | string | 报告标题 |
Subtitle | string | 报告副标题 |
Author | string | 作者/组织名称 |
Date | string | 报告日期 |
Sections | []ReportSection | 报告章节 |
ReportSection
| 字段 | 类型 | 说明 |
|---|---|---|
Title | string | 章节标题 |
Content | string | 章节正文 |
Metrics | []ReportMetric | 以卡片形式显示的关键指标 |
Table | *ReportTable | 可选的数据表格 |
ReportMetric
type ReportMetric struct {
Label string
Value string
ColorHex uint32 // Hex color for the value (e.g., 0x2E7D32)
}
ReportTable
type ReportTable struct {
Header []string
ColumnWidths []float64
Rows [][]string
}
自定义
doc := template.Report(reportData,
template.WithPageSize(document.Letter),
template.WithFont("Inter", fontData),
)
使用外观模式
import "github.com/gpdf-dev/gpdf"
doc := gpdf.NewReport(reportData)