How to generate a table inside a PDF
c.NewTable(n) fixes the column count, and every NewCell adds the next cell left to
right, starting a new row once the current one is full.
table := c.NewTable(3)
p := c.NewStyledParagraph()
p.SetText("Product #1")
cell := table.NewCell()
cell.SetHorizontalAlignment(creator.CellHorizontalAlignmentLeft)
if err := cell.SetContent(p); err != nil {
return err
}
if err := c.Draw(table); err != nil {
return err
}Cells are not addressable by coordinates. The order you add them in is the layout, and adding one cell too many silently starts a new row rather than reporting anything. Almost every visual property - borders, background color, alignment, indent - belongs to the cell rather than the table, which is why most examples wrap cell creation in a small local helper. The table itself owns the column count, header rows and row wrapping.
SetContent takes any drawable, so a cell can hold a paragraph, an image, a division or
another table.
c.NewGrid(n) is the alternative. It builds explicit rows, so a cell that would pass the
last column returns an error instead of reflowing, and spans are easier to keep track of.
The one thing it will not do is repeat header rows across pages; Table.SetHeaderRows is
still the only call for that.
Column widths, spans, header rows and nesting are covered in the tables guides, with the Grid equivalents in the grid guides.