Tables

Basic Table

A simple table with headers and data rows.

doc := template.New(
    template.WithPageSize(document.A4),
    template.WithMargins(document.UniformEdges(document.Mm(15))),
)

page := doc.AddPage()

page.AutoRow(func(r *template.RowBuilder) {
    r.Col(12, func(c *template.ColBuilder) {
        c.Text("Basic Table", template.FontSize(18), template.Bold())
        c.Spacer(document.Mm(5))
    })
})

page.AutoRow(func(r *template.RowBuilder) {
    r.Col(12, func(c *template.ColBuilder) {
        c.Table(
            []string{"Name", "Age", "City"},
            [][]string{
                {"Alice", "30", "Tokyo"},
                {"Bob", "25", "New York"},
                {"Charlie", "35", "London"},
                {"Diana", "28", "Paris"},
            },
        )
    })
})

Styled Table

Custom column widths, header styling, and alternating row colors.

doc := template.New(
    template.WithPageSize(document.A4),
    template.WithMargins(document.UniformEdges(document.Mm(15))),
)

page := doc.AddPage()

page.AutoRow(func(r *template.RowBuilder) {
    r.Col(12, func(c *template.ColBuilder) {
        c.Text("Styled Table", template.FontSize(18), template.Bold())
        c.Spacer(document.Mm(5))
    })
})

darkBlue := pdf.RGBHex(0x1A237E)
lightGray := pdf.RGBHex(0xF5F5F5)

page.AutoRow(func(r *template.RowBuilder) {
    r.Col(12, func(c *template.ColBuilder) {
        c.Table(
            []string{"Product", "Category", "Qty", "Unit Price", "Total"},
            [][]string{
                {"Laptop Pro 15", "Electronics", "2", "$1,299.00", "$2,598.00"},
                {"Wireless Mouse", "Accessories", "10", "$29.99", "$299.90"},
                {"USB-C Hub", "Accessories", "5", "$49.99", "$249.95"},
                {"Monitor 27\"", "Electronics", "3", "$399.00", "$1,197.00"},
                {"Keyboard", "Accessories", "10", "$79.99", "$799.90"},
                {"Webcam HD", "Electronics", "4", "$89.99", "$359.96"},
            },
            template.ColumnWidths(30, 20, 10, 20, 20),
            template.TableHeaderStyle(
                template.TextColor(pdf.White),
                template.BgColor(darkBlue),
            ),
            template.TableStripe(lightGray),
        )
    })
})

Tables in Columns

Place multiple tables side-by-side using the grid system.

doc := template.New(
    template.WithPageSize(document.A4),
    template.WithMargins(document.UniformEdges(document.Mm(15))),
)

page := doc.AddPage()

page.AutoRow(func(r *template.RowBuilder) {
    r.Col(12, func(c *template.ColBuilder) {
        c.Text("Tables in Grid Columns", template.FontSize(18), template.Bold())
        c.Spacer(document.Mm(5))
    })
})

page.AutoRow(func(r *template.RowBuilder) {
    r.Col(6, func(c *template.ColBuilder) {
        c.Text("Team A", template.Bold())
        c.Spacer(document.Mm(2))
        c.Table(
            []string{"Player", "Score"},
            [][]string{
                {"Alice", "95"},
                {"Bob", "87"},
                {"Charlie", "92"},
            },
            template.ColumnWidths(60, 40),
        )
    })
    r.Col(6, func(c *template.ColBuilder) {
        c.Text("Team B", template.Bold())
        c.Spacer(document.Mm(2))
        c.Table(
            []string{"Player", "Score"},
            [][]string{
                {"Diana", "91"},
                {"Eve", "88"},
                {"Frank", "85"},
            },
            template.ColumnWidths(60, 40),
        )
    })
})

Table Vertical Alignment

Control vertical alignment of cell content: top (default), middle, or bottom.

doc := template.New(
    template.WithPageSize(document.A4),
    template.WithMargins(document.UniformEdges(document.Mm(20))),
)

page := doc.AddPage()
page.AutoRow(func(r *template.RowBuilder) {
    r.Col(12, func(c *template.ColBuilder) {
        c.Text("Table Vertical Align Demo", template.FontSize(20), template.Bold())
        c.Spacer(document.Mm(8))

        // Default (top) alignment
        c.Text("Default (Top) Alignment:", template.Bold())
        c.Spacer(document.Mm(3))
        c.Table(
            []string{"Short", "Tall Cell"},
            [][]string{
                {"A", "This cell has\nmuch more content\nthat spans\nmultiple lines"},
                {"B", "Another tall\ncell with\nlong text"},
            },
            template.TableHeaderStyle(
                template.BgColor(pdf.RGBHex(0x1565C0)),
                template.TextColor(pdf.White),
            ),
        )
        c.Spacer(document.Mm(8))

        // Middle alignment
        c.Text("Middle Alignment:", template.Bold())
        c.Spacer(document.Mm(3))
        c.Table(
            []string{"Short", "Tall Cell"},
            [][]string{
                {"A", "This cell has\nmuch more content\nthat spans\nmultiple lines"},
                {"B", "Another tall\ncell with\nlong text"},
            },
            template.TableHeaderStyle(
                template.BgColor(pdf.RGBHex(0x2E7D32)),
                template.TextColor(pdf.White),
            ),
            template.TableCellVAlign(document.VAlignMiddle),
        )
        c.Spacer(document.Mm(8))

        // Bottom alignment
        c.Text("Bottom Alignment:", template.Bold())
        c.Spacer(document.Mm(3))
        c.Table(
            []string{"Short", "Tall Cell"},
            [][]string{
                {"A", "This cell has\nmuch more content\nthat spans\nmultiple lines"},
                {"B", "Another tall\ncell with\nlong text"},
            },
            template.TableHeaderStyle(
                template.BgColor(pdf.RGBHex(0xE65100)),
                template.TextColor(pdf.White),
            ),
            template.TableCellVAlign(document.VAlignBottom),
        )
    })
})