TableBuilder API Reference
Complete API reference for the TableBuilder fluent API. For an overview and basic example, see TableBuilder Documentation.
Creating a Builder
createTableBuilder(config?: TableBuilderConfig): TableBuilder
Creates a new TableBuilder instance.
Parameters:
config(optional): Configuration objectautoStyle?: boolean- Whether to automatically apply stylingdefaultTheme?: ThemeType- Default theme ('striped', 'grid', 'plain')defaultMargin?: MarginPadding- Default margin configuration
Example:
const builder = createTableBuilder({
defaultTheme: 'striped',
defaultMargin: 20
})Header Methods
addHeader(row: CellDef[]): this
Adds a single header row to the table.
Example:
builder.addHeader(['Column 1', 'Column 2', 'Column 3'])setHeader(rows: CellDef[][]): this
Sets the entire header (replaces existing header).
Example:
builder.setHeader([
['Main Header'],
['Sub Header 1', 'Sub Header 2', 'Sub Header 3']
])clearHeader(): this
Clears all header rows.
Example:
builder.clearHeader()Body Methods
addRow(row: CellDef[]): this
Adds a single row to the table body.
Example:
builder.addRow(['Value 1', 'Value 2', 'Value 3'])addRows(rows: CellDef[][]): this
Adds multiple rows to the table body.
Example:
builder.addRows([
['Row 1 Col 1', 'Row 1 Col 2', 'Row 1 Col 3'],
['Row 2 Col 1', 'Row 2 Col 2', 'Row 2 Col 3']
])setBody(rows: CellDef[][]): this
Sets the entire body (replaces existing body).
Example:
builder.setBody([
['Data 1', 'Data 2', 'Data 3'],
['Data 4', 'Data 5', 'Data 6']
])clearBody(): this
Clears all body rows.
Example:
builder.clearBody()Footer Methods
addFooter(row: CellDef[]): this
Adds a single footer row to the table.
Example:
builder.addFooter(['Total', '', '$1,234.56'])setFooter(rows: CellDef[][]): this
Sets the entire footer (replaces existing footer).
Example:
builder.setFooter([
['Subtotal', '', '$1,000.00'],
['Tax', '', '$234.56'],
['Total', '', '$1,234.56']
])clearFooter(): this
Clears all footer rows.
Example:
builder.clearFooter()Column Configuration
setColumns(columns: ColumnInput[]): this
Sets column definitions for the table.
Example:
builder.setColumns([
{ header: 'Name', dataKey: 'name' },
{ header: 'Age', dataKey: 'age' },
{ header: 'City', dataKey: 'city' }
])Style Methods
setTheme(theme: ThemeType): this
Sets the table theme.
Available themes:
'striped'- Alternating row colors'grid'- Grid with borders'plain'- Minimal styling
Example:
builder.setTheme('grid')setHeaderStyles(styles: Styles): this
Sets styles for the header section.
Example:
builder.setHeaderStyles({
fillColor: '#4CAF50',
textColor: '#ffffff',
fontStyle: 'bold',
fontSize: 12
})setBodyStyles(styles: Styles): this
Sets styles for the body section.
Example:
builder.setBodyStyles({
fontSize: 10,
textColor: '#333333'
})setFooterStyles(styles: Styles): this
Sets styles for the footer section.
Example:
builder.setFooterStyles({
fillColor: '#2196F3',
textColor: '#ffffff',
fontStyle: 'bold'
})setAlternateRowStyles(styles: Styles): this
Sets styles for alternate rows.
Example:
builder.setAlternateRowStyles({ fillColor: '#f5f5f5' })setColumnStyles(columnIndex: number | string, styles: Styles): this
Sets styles for a specific column.
Example:
builder
.setColumnStyles(0, { halign: 'left' })
.setColumnStyles(2, { halign: 'right', fontStyle: 'bold' })Layout Methods
setMargin(margin: MarginPadding | number): this
Sets the table margins.
Example:
// Single number for all sides
builder.setMargin(20)
// Object for individual sides
builder.setMargin({ top: 40, right: 20, bottom: 20, left: 20 })setStartY(y: number): this
Sets the starting Y position for the table.
Example:
builder.setStartY(50)setTableWidth(width: TableWidthType): this
Sets the table width.
Example:
builder.setTableWidth('auto')
// or
builder.setTableWidth(180)setExtraOptions(options: AutoTableOptions): this
Merges extra jsPDF-AutoTable options into the table configuration. Provided options override any previously set options for the same keys.
Example:
builder.setExtraOptions({
theme: 'striped',
margin: { top: 40, right: 20, bottom: 20, left: 20 },
startY: 50
})Build Methods
build(pdf?: jsPDF): void
Builds and renders the table to the PDF.
Example:
builder.build()
// or with explicit PDF instance
builder.build(pdfInstance)getContent(): AutoTableContent
Gets the table content configuration without building.
Returns:
{
header?: CellDef[][],
body: CellDef[][],
footer?: CellDef[][],
columns?: ColumnInput[]
}Example:
const content = builder.getContent()
createTable(content, options)getOptions(): AutoTableOptions
Gets the table options configuration without building.
Returns:
{
theme?: ThemeType,
headStyles?: Styles,
bodyStyles?: Styles,
// ... other options
}Example:
const options = builder.getOptions()Utility Methods
reset(): this
Resets the builder to initial state while preserving default configuration.
Example:
builder
.addHeader(['Col 1', 'Col 2'])
.addRow(['Data 1', 'Data 2'])
.build()
.reset() // Clear all data
.addHeader(['New Col 1', 'New Col 2'])
.build()clone(): TableBuilder
Creates a copy of the current builder with the same configuration.
Example:
const template = createTableBuilder({ defaultTheme: 'grid' })
.setHeaderStyles({ fillColor: '#4CAF50', textColor: '#fff' })
const table1 = template.clone()
.addHeader(['Name', 'Age'])
.addRows([['John', 30], ['Jane', 25]])
const table2 = template.clone()
.addHeader(['Product', 'Price'])
.addRows([['Laptop', '$999'], ['Phone', '$699']])Helper Utilities
fromObjects(data: Record<string, any>[])
Converts an array of objects to table data.
Example:
import { fromObjects } from 'vue-pdfiy'
const employees = [
{ name: 'John', age: 30, city: 'NYC' },
{ name: 'Jane', age: 25, city: 'LA' }
]
const { header, body } = fromObjects(employees)
builder.setHeader(header).addRows(body)fromKeys(obj: Record<string, any>)
Extracts keys from an object.
Example:
import { fromKeys } from 'vue-pdfiy'
const data = { name: 'John', age: 30, city: 'NYC' }
const headers = fromKeys(data)
// ['name', 'age', 'city']formatCurrency(value: number, currency?: string)
Formats a number as currency.
Example:
import { formatCurrency } from 'vue-pdfiy'
formatCurrency(1234.56) // '$1,234.56'
formatCurrency(1234.56, '€') // '€1,234.56'formatDate(date: Date | string, format?: string)
Formats a date.
Example:
import { formatDate } from 'vue-pdfiy'
formatDate(new Date('2024-01-15')) // '2024-01-15'
formatDate('2024-01-15', 'MM/DD/YYYY') // '01/15/2024'createTotalRow(label: string, data: any[][], columnIndices: number[])
Creates a total row by summing specified columns.
Example:
import { createTotalRow } from 'vue-pdfiy'
const data = [
['Product A', 100, 50],
['Product B', 200, 75]
]
const totalRow = createTotalRow('Total', data, [1, 2])
// ['Total', 300, 125]