Documentation
¶
Overview ¶
Package report 提供通用报告排版、字体加载与 OFD/PDF 输出。 可用于业务报告,也供 analyzer 和 validator 的报告复用。
Index ¶
- Constants
- Variables
- func FromOutput(output string) (string, bool)
- func ThemeByName(name string) (flow.Theme, error)
- func WriteOFD(ctx context.Context, w io.Writer, document *creator.Document) error
- func WritePDF(ctx context.Context, w io.Writer, document *creator.Document) error
- type Builder
- func (b *Builder) Block(kind string, level int, text string, style flow.TextStyle)
- func (b *Builder) Build() (*creator.Document, error)
- func (b *Builder) Callout(variant, title, text string)
- func (b *Builder) Heading(level int, text string, color *creator.Color)
- func (b *Builder) Paragraph(text string, color *creator.Color)
- func (b *Builder) Table(headers []string, rows [][]flow.TableCell, widths []float64, ...)
- func (b *Builder) Tree(root TreeNode)
- type Font
- type Fonts
- type Options
- type TableOptions
- type TreeNode
Examples ¶
Constants ¶
const Width = 174.0
Width 是默认 A4 报告的版心宽度,单位为毫米。
Variables ¶
var ( Ink = creator.Color{R: 35, G: 48, B: 62} Muted = creator.Color{R: 91, G: 104, B: 117} Green = creator.Color{R: 38, G: 125, B: 85} Red = creator.Color{R: 184, G: 59, B: 58} Orange = creator.Color{R: 186, G: 113, B: 25} Blue = creator.Color{R: 47, G: 104, B: 157} )
默认报告配色,供段落、标题和表格单元格复用。
Functions ¶
func ThemeByName ¶
ThemeByName 返回报告主题;空名称默认使用 Fluent 2,名称忽略大小写和首尾空白。
Types ¶
type Builder ¶
Builder 累积报告内容,Build 统一返回文字准备或分页错误。
func New ¶
func New(ctx context.Context, metadata creator.Document, fontPath string, options ...Options) (*Builder, error)
New 创建 A4、18mm 页边距、中文页码和章节大纲的报告,默认使用 Fluent 2。 options 可省略或指定一组排版选项。
Example ¶
使用外部包演示完整调用链;运行时需提供中文字体。
package main
import (
"context"
"io"
"log"
"github.com/zc310/ofd/pkg/creator"
"github.com/zc310/ofd/pkg/creator/flow"
"github.com/zc310/ofd/pkg/report"
)
func main() {
ctx := context.Background()
b, err := report.New(ctx, creator.Document{ID: "summary", Title: "业务报告"}, "/path/to/cjk-font.ttf", report.Options{Theme: "material"})
if err != nil {
log.Fatal(err)
}
b.Heading(1, "业务报告", nil)
b.Paragraph("本月业务处理情况。", nil)
b.Callout("success", "处理完成", "所有任务已完成。")
b.Table([]string{"项目", "数量"}, [][]flow.TableCell{{{Text: "已完成"}, {Text: "128"}}},
[]float64{120, 54}, report.TableOptions{SplitRows: true})
doc, err := b.Build()
if err != nil {
log.Fatal(err)
}
// 替换为文件、内存缓冲区或 HTTP 响应即可接收产物。
if err := report.WriteOFD(ctx, io.Discard, doc); err != nil {
log.Fatal(err)
}
if err := report.WritePDF(ctx, io.Discard, doc); err != nil {
log.Fatal(err)
}
}
Output:
func NewWithFonts ¶
func NewWithFonts(ctx context.Context, metadata creator.Document, fonts *Fonts, options ...Options) (*Builder, error)
NewWithFonts 使用已加载的字体创建与 New 相同版式的报告,不再读取或解析字体。 fonts 可由多个报告复用,每次调用返回独立的 Builder;ctx 和已初始化的 fonts 不得为空。
func (*Builder) Block ¶
Block 按块级样式添加内容,提前处理长路径和不可显示字符。 kind 使用 flow.KindParagraph、flow.KindHeading 等文字块类型常量。
type Font ¶
type Font struct {
// Data 是可独立嵌入的字体数据。
Data []byte
// Face 是与 Data 对应、供文字度量使用的字体面。
Face *sfnt.Font
// contains filtered or unexported fields
}
Font 是排版度量与嵌入共用的字体快照。
type Fonts ¶
type Fonts struct {
// contains filtered or unexported fields
}
Fonts 保存可复用的报告字体。通过 LoadFonts 或 ParseFonts 创建后可供多个报告并发使用。 创建报告后不得修改 Builder.Document.Options.Fonts 中共享的字体数据。
func LoadFonts ¶
LoadFonts 一次加载常规字体及可用的同族实际粗体,供多次 NewWithFonts 调用复用。 path 为空时自动查找中文系统字体,粗体选择规则与 New 相同。
func ParseFonts ¶
ParseFonts 从内存中的 TTF、OTF 或 TTC 创建可复用的报告字体,并复制输入数据。 regular 必须包含中文;bold 为空时标题沿用常规字体,否则须为同族实际粗体。 TTC 自动选择匹配的中文字体面。本函数不读取文件或查找系统字体。
Example ¶
使用一次解析的字体创建多份独立报告,数据也可以来自 go:embed。
package main
import (
"context"
"io"
"log"
"os"
"github.com/zc310/ofd/pkg/creator"
"github.com/zc310/ofd/pkg/report"
)
func main() {
data, err := os.ReadFile("/path/to/cjk-font.ttf")
if err != nil {
log.Fatal(err)
}
fonts, err := report.ParseFonts(data, nil)
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
for _, title := range []string{"月度报告", "年度报告"} {
b, err := report.NewWithFonts(ctx, creator.Document{Title: title}, fonts, report.Options{Theme: "fluent2"})
if err != nil {
log.Fatal(err)
}
b.Heading(1, title, nil)
doc, err := b.Build()
if err != nil {
log.Fatal(err)
}
if err := report.WritePDF(ctx, io.Discard, doc); err != nil {
log.Fatal(err)
}
}
}
Output:
type Options ¶
type Options struct {
// Theme 选择 fluent2(默认)、material、material2、material3、carbon 或 bootstrap5。
Theme string
}
Options 配置通用 OFD/PDF 报告的排版。
type TableOptions ¶
type TableOptions struct {
// SplitRows 允许长单元格按完整文字行跨页续排,续页仍重复表头。
SplitRows bool
// ColumnAlign 按列设置表头和正文的水平对齐,空字符串沿用主题。
ColumnAlign []string
}
TableOptions 配置报告表格的跨页方式。