网格布局
概述
gpdf 使用受 Bootstrap 启发的 12 列网格系统。每行被分为 12 等份列,你可以指定每个内容块所占的列数。
列配置
全宽(12 列)
page.AutoRow(func(r *template.RowBuilder) {
r.Col(12, func(c *template.ColBuilder) {
c.Text("Full width content", template.BgColor(pdf.RGBHex(0xE3F2FD)))
})
})
┌─────────────────────────────────────────────────┐
│ Full width content Col 12 │
└─────────────────────────────────────────────────┘
两等列(6 + 6)
page.AutoRow(func(r *template.RowBuilder) {
r.Col(6, func(c *template.ColBuilder) {
c.Text("Col 6 (left)", template.BgColor(pdf.RGBHex(0xE8F5E9)))
})
r.Col(6, func(c *template.ColBuilder) {
c.Text("Col 6 (right)", template.BgColor(pdf.RGBHex(0xFFF3E0)))
})
})
┌────────────────────────┬────────────────────────┐
│ Col 6 (left) │ Col 6 (right) │
└────────────────────────┴────────────────────────┘
三等列(4 + 4 + 4)
page.AutoRow(func(r *template.RowBuilder) {
r.Col(4, func(c *template.ColBuilder) {
c.Text("Col 4", template.BgColor(pdf.RGBHex(0xFCE4EC)))
})
r.Col(4, func(c *template.ColBuilder) {
c.Text("Col 4", template.BgColor(pdf.RGBHex(0xF3E5F5)))
})
r.Col(4, func(c *template.ColBuilder) {
c.Text("Col 4", template.BgColor(pdf.RGBHex(0xE8EAF6)))
})
})
┌────────────────┬────────────────┬────────────────┐
│ Col 4 │ Col 4 │ Col 4 │
└────────────────┴────────────────┴────────────────┘
四等列(3 + 3 + 3 + 3)
page.AutoRow(func(r *template.RowBuilder) {
r.Col(3, func(c *template.ColBuilder) {
c.Text("Col 3", template.BgColor(pdf.RGBHex(0xE0F7FA)))
})
r.Col(3, func(c *template.ColBuilder) {
c.Text("Col 3", template.BgColor(pdf.RGBHex(0xE0F2F1)))
})
r.Col(3, func(c *template.ColBuilder) {
c.Text("Col 3", template.BgColor(pdf.RGBHex(0xFFF9C4)))
})
r.Col(3, func(c *template.ColBuilder) {
c.Text("Col 3", template.BgColor(pdf.RGBHex(0xFFECB3)))
})
})
┌───────────┬───────────┬───────────┬───────────┐
│ Col 3 │ Col 3 │ Col 3 │ Col 3 │
└───────────┴───────────┴───────────┴───────────┘
侧边栏 + 主内容(3 + 9)
page.AutoRow(func(r *template.RowBuilder) {
r.Col(3, func(c *template.ColBuilder) {
c.Text("Sidebar (3)", template.BgColor(pdf.RGBHex(0xD7CCC8)))
})
r.Col(9, func(c *template.ColBuilder) {
c.Text("Main content (9)", template.BgColor(pdf.RGBHex(0xF5F5F5)))
})
})
┌───────────┬─────────────────────────────────────┐
│ Sidebar │ Main content (9) │
│ (3) │ │
└───────────┴─────────────────────────────────────┘
文章 + 面板(8 + 4)
page.AutoRow(func(r *template.RowBuilder) {
r.Col(8, func(c *template.ColBuilder) {
c.Text("Article area (8)", template.BgColor(pdf.RGBHex(0xE1F5FE)))
})
r.Col(4, func(c *template.ColBuilder) {
c.Text("Side panel (4)", template.BgColor(pdf.RGBHex(0xFBE9E7)))
})
})
┌─────────────────────────────────┬───────────────┐
│ Article area (8) │ Side panel │
│ │ (4) │
└─────────────────────────────────┴───────────────┘
行类型
自适应高度行
AutoRow 根据内容自动调整高度:
page.AutoRow(func(r *template.RowBuilder) {
r.Col(12, func(c *template.ColBuilder) {
c.Text("This row's height is determined by its content")
})
})
固定高度行
Row 接受显式的高度值:
// 30mm tall row
page.Row(document.Mm(30), func(r *template.RowBuilder) {
r.Col(12, func(c *template.ColBuilder) {
c.Text("This row is 30mm tall", template.BgColor(pdf.RGBHex(0xE3F2FD)))
})
})
// 50mm tall row with two columns
page.Row(document.Mm(50), func(r *template.RowBuilder) {
r.Col(6, func(c *template.ColBuilder) {
c.Text("Left: 50mm row", template.BgColor(pdf.RGBHex(0xE8F5E9)))
})
r.Col(6, func(c *template.ColBuilder) {
c.Text("Right: 50mm row", template.BgColor(pdf.RGBHex(0xFFF3E0)))
})
})
┌─────────────────────────────────────────────────┐
│ This row is 30mm tall │
│ height: 30mm │
└─────────────────────────────────────────────────┘
┌────────────────────────┬────────────────────────┐
│ Left: 50mm row │ Right: 50mm row │
│ │ │
│ │ │
│ height: 50mm │
└────────────────────────┴────────────────────────┘
列中的多个内容
每列可以包含垂直堆叠的多个元素:
page.AutoRow(func(r *template.RowBuilder) {
r.Col(6, func(c *template.ColBuilder) {
c.Text("Left column - line 1")
c.Text("Left column - line 2")
c.Text("Left column - line 3")
})
r.Col(6, func(c *template.ColBuilder) {
c.Text("Right column - line 1")
c.Text("Right column - line 2")
c.Text("Right column - line 3")
})
})
┌────────────────────────┬────────────────────────┐
│ Left column - line 1 │ Right column - line 1 │
│ Left column - line 2 │ Right column - line 2 │
│ Left column - line 3 │ Right column - line 3 │
└────────────────────────┴────────────────────────┘
页面尺寸
| 常量 | 尺寸 | 尺寸值 |
|---|---|---|
document.A4 | A4 | 210mm x 297mm |
document.A3 | A3 | 297mm x 420mm |
document.Letter | US Letter | 8.5" x 11" |
document.Legal | US Legal | 8.5" x 14" |
单位
gpdf 支持多种尺寸单位:
| 函数 | 单位 | 示例 |
|---|---|---|
document.Pt(v) | 点(1/72 英寸) | document.Pt(12) |
document.Mm(v) | 毫米 | document.Mm(20) |
document.Cm(v) | 厘米 | document.Cm(2.5) |
document.In(v) | 英寸 | document.In(1) |
document.Em(v) | 相对于字号 | document.Em(2) |
document.Pct(v) | 父元素的百分比 | document.Pct(50) |
自定义边距
// Uniform margins
template.WithMargins(document.UniformEdges(document.Mm(20)))
// Custom margins per side
template.WithMargins(document.Edges{
Top: document.Mm(25),
Right: document.Mm(15),
Bottom: document.Mm(25),
Left: document.Mm(15),
})