Export functions
Write a TanStack table to Excel, PDF or CSV, the options each export takes, and the colors from your theme.
exportXlsx
functionExport the table to an .xlsx file. Returns the file as a Blob. Without a valid license key it still exports, adds the unlicensed notice line below the data, and logs a console warning once per page load.
exportXlsx(table: TableLike, opts?: XlsxExportOptions) => Promise<Blob>Parameters
| Name | Type | Description |
|---|---|---|
table | TableLike | A TanStack table (v8 or v9), or pivot.table from createPivot. |
optsoptional | XlsxExportOptions | Export options. Default: every row, saved as 'export.xlsx'. |
Returns
Promise<Blob>
exportPdf
functionExport the table to a PDF file. Returns the file as a Blob. Loads pdfmake and its fonts on first use. Without a valid license key it still exports, prints the unlicensed notice line below the table, and logs a console warning once per page load.
exportPdf(table: TableLike, opts?: PdfTableExportOptions) => Promise<Blob>Parameters
| Name | Type | Description |
|---|---|---|
table | TableLike | A TanStack table (v8 or v9), or pivot.table from createPivot. |
optsoptional | PdfTableExportOptions | Export options. Default: every row on A4, saved as 'export.pdf'. |
Returns
Promise<Blob>
exportCsv
functionExport the table to a CSV file. Returns the file as a Blob. Free: no license check.
exportCsv(table: TableLike, opts?: CsvExportOptions) => Promise<Blob>Parameters
| Name | Type | Description |
|---|---|---|
table | TableLike | A TanStack table (v8 or v9), or pivot.table from createPivot. |
optsoptional | CsvExportOptions | Export options. Default: every row, saved as 'export.csv'. |
Returns
Promise<Blob>
tableToCsv
functionThe table as CSV text: a header line, then one line per data row. Group rows are left out. Lines end with CRLF. No byte order mark and no file.
tableToCsv(table: TableLike, opts?: CsvExportOptions) => stringParameters
| Name | Type | Description |
|---|---|---|
table | TableLike | A TanStack table (v8 or v9), or pivot.table from createPivot. |
optsoptional | CsvExportOptions | Which rows to write and the field separator. Default: every row, ','. |
Returns
string
XlsxExportOptions
typeOptions for exportXlsx and the workbook model functions.
Properties
| Name | Type | Description |
|---|---|---|
rowsoptional | RowScope | Which rows to export. Default 'all'. |
rowStyleoptional | ((original: unknown) => CellStyle | undefined) | Style a whole row (e.g. bold totals). Called with the row's original data. |
fileNameoptional | string | File name for the download. '.xlsx' is added unless the name ends with it. Default 'export'. |
sheetNameoptional | string | Worksheet name. Default 'Sheet1'. |
groupFilloptional | string | Fill color of group rows. Default '#f4f4f5', the muted gray of a shadcn/ui table. |
downloadoptional | boolean | Save the file in the browser. Default true. |
PdfTableExportOptions
typeOptions for exportPdf: which rows to read, the PDF page and look, and the file.
Properties
| Name | Type | Description |
|---|---|---|
rowsoptional | RowScope | Which rows to export. Default 'all'. |
rowStyleoptional | ((original: unknown) => CellStyle | undefined) | Style a whole row (e.g. bold totals). Called with the row's original data. |
pageSizeoptional | PdfPageSize | Paper size. Default 'A4'. |
pageOrientationoptional | "portrait" | "landscape" | Default: 'landscape' when there are more columns than autoLandscapeThreshold, else 'portrait'. |
marginsoptional | [number, number, number, number] | [left, top, right, bottom] in pt. Default [28, 34, 28, 34]. |
titleoptional | string | Document title rendered above the table. |
subtitleoptional | string | Smaller line under the title. |
logooptional | string | Logo as a data URL, rendered top-left above the title. |
logoWidthoptional | number | Logo width in pt (height auto). Default 90. |
fontSizeoptional | number | Base font size in pt. Default 8. |
headerColoroptional | string | Header row fill color. Default: none, as a shadcn/ui table header. |
headerTextColoroptional | string | Header row text color. Default '#18181b'. |
zebraoptional | boolean | Stripe alternate rows. Default false, as a shadcn/ui table. |
zebraColoroptional | string | Stripe fill color. Default '#fafafa'. |
repeatHeaderoptional | boolean | Repeat the header row on every page. Default true. |
showPageNumbersoptional | boolean | Show the "Page X of Y" + date footer. Default true. |
columnWidthsoptional | "*" | "auto" | (number | "*" | "auto")[] | Column widths: '' (fill, default), 'auto', or an explicit array of pt numbers / 'auto' / '' per column. |
autoLandscapeThresholdoptional | number | Switch to landscape automatically when column count exceeds this (and no explicit orientation was set). Default 8. |
groupColoroptional | string | Group header row fill color. Default '#f4f4f5'. |
groupTextColoroptional | string | Group header row text color. Default '#18181b'. |
subtotalColoroptional | string | Fill of group rows and their totals. Default '#f4f4f5'. |
chartsoptional | readonly PdfChartImage[] | Charts to print with the table, as image data URLs, each with an
optional title and caption. Printed at the content width unless width
(pt) says otherwise. |
chartsPositionoptional | "above" | "below" | Where the charts go: above the table (default) or below it. |
kpisoptional | readonly PdfKpi[] | A strip of headline numbers above the table: a label, the value, and an optional delta line in its own colour ("+4.2%" in green). |
kpiColoroptional | string | KPI box fill. Default '#f4f4f5'. |
textColoroptional | string | Body text colour. Default black. |
borderColoroptional | string | Table line colour. Default '#e4e4e7'. |
verticalLinesoptional | boolean | Draw lines between columns, not only between rows and around the table. Default false, as a shadcn/ui table. |
fitoptional | "auto" | "shrink" | "split" | "none" | What to do when the columns do not fit across the page.
- 'auto' (default): shrink the table text, down to three quarters of
fontSize; past that, split the columns over several pages.
- 'shrink': only shrink, down to 4 pt.
- 'split': only split.
- 'none': leave it to pdfmake, which lets a wide table run off the page. |
repeatColumnsoptional | number | When the columns are split over pages, how many leading columns repeat on every page. Default 1. |
grandTotaloptional | boolean | Deprecated. Has no effect: exports add no total row. Kept so existing code still compiles. |
labelsoptional | { page?: (page: number, pages: number) => string; date?: (now: Date) => string; subtotal?: (group: string) => string; total?: string; } | The words the PDF prints itself, for another language or house style: the page footer, the footer date, a group's subtotal label, and the total row's label. |
watermarkoptional | string | { text: string; color?: string; opacity?: number; } | Text printed large and faint across every page: "DRAFT", "CONFIDENTIAL". |
authoroptional | string | Author recorded in the PDF's document properties. |
fontoptional | PdfFont | A font to print with instead of Roboto, which has no Chinese, Japanese, Korean, Arabic, Hebrew, Thai or Indic characters (they print as empty boxes). Each file is the font's base64 data or a URL pdfmake can fetch. It must cover Latin too: the whole document uses it. |
fileNameoptional | string | File name for the download. '.pdf' is added unless the name ends with it. Default 'export'. |
downloadoptional | boolean | Save the file in the browser. Default true. |
PdfExportOptions
typePage setup and look of a PDF export. Sizes are in pt.
Properties
| Name | Type | Description |
|---|---|---|
pageSizeoptional | PdfPageSize | Paper size. Default 'A4'. |
pageOrientationoptional | "portrait" | "landscape" | Default: 'landscape' when there are more columns than autoLandscapeThreshold, else 'portrait'. |
marginsoptional | [number, number, number, number] | [left, top, right, bottom] in pt. Default [28, 34, 28, 34]. |
titleoptional | string | Document title rendered above the table. |
subtitleoptional | string | Smaller line under the title. |
logooptional | string | Logo as a data URL, rendered top-left above the title. |
logoWidthoptional | number | Logo width in pt (height auto). Default 90. |
fontSizeoptional | number | Base font size in pt. Default 8. |
headerColoroptional | string | Header row fill color. Default: none, as a shadcn/ui table header. |
headerTextColoroptional | string | Header row text color. Default '#18181b'. |
zebraoptional | boolean | Stripe alternate rows. Default false, as a shadcn/ui table. |
zebraColoroptional | string | Stripe fill color. Default '#fafafa'. |
repeatHeaderoptional | boolean | Repeat the header row on every page. Default true. |
showPageNumbersoptional | boolean | Show the "Page X of Y" + date footer. Default true. |
columnWidthsoptional | "*" | "auto" | (number | "*" | "auto")[] | Column widths: '' (fill, default), 'auto', or an explicit array of pt numbers / 'auto' / '' per column. |
autoLandscapeThresholdoptional | number | Switch to landscape automatically when column count exceeds this (and no explicit orientation was set). Default 8. |
groupColoroptional | string | Group header row fill color. Default '#f4f4f5'. |
groupTextColoroptional | string | Group header row text color. Default '#18181b'. |
subtotalColoroptional | string | Fill of group rows and their totals. Default '#f4f4f5'. |
chartsoptional | readonly PdfChartImage[] | Charts to print with the table, as image data URLs, each with an
optional title and caption. Printed at the content width unless width
(pt) says otherwise. |
chartsPositionoptional | "above" | "below" | Where the charts go: above the table (default) or below it. |
kpisoptional | readonly PdfKpi[] | A strip of headline numbers above the table: a label, the value, and an optional delta line in its own colour ("+4.2%" in green). |
kpiColoroptional | string | KPI box fill. Default '#f4f4f5'. |
textColoroptional | string | Body text colour. Default black. |
borderColoroptional | string | Table line colour. Default '#e4e4e7'. |
verticalLinesoptional | boolean | Draw lines between columns, not only between rows and around the table. Default false, as a shadcn/ui table. |
fitoptional | "auto" | "shrink" | "split" | "none" | What to do when the columns do not fit across the page.
- 'auto' (default): shrink the table text, down to three quarters of
fontSize; past that, split the columns over several pages.
- 'shrink': only shrink, down to 4 pt.
- 'split': only split.
- 'none': leave it to pdfmake, which lets a wide table run off the page. |
repeatColumnsoptional | number | When the columns are split over pages, how many leading columns repeat on every page. Default 1. |
grandTotaloptional | boolean | Deprecated. Has no effect: exports add no total row. Kept so existing code still compiles. |
labelsoptional | { page?: (page: number, pages: number) => string; date?: (now: Date) => string; subtotal?: (group: string) => string; total?: string; } | The words the PDF prints itself, for another language or house style: the page footer, the footer date, a group's subtotal label, and the total row's label. |
watermarkoptional | string | { text: string; color?: string; opacity?: number; } | Text printed large and faint across every page: "DRAFT", "CONFIDENTIAL". |
authoroptional | string | Author recorded in the PDF's document properties. |
fontoptional | PdfFont | A font to print with instead of Roboto, which has no Chinese, Japanese, Korean, Arabic, Hebrew, Thai or Indic characters (they print as empty boxes). Each file is the font's base64 data or a URL pdfmake can fetch. It must cover Latin too: the whole document uses it. |
CsvExportOptions
typeOptions for exportCsv and tableToCsv.
Properties
| Name | Type | Description |
|---|---|---|
rowsoptional | RowScope | Which rows to export. Default 'all'. |
rowStyleoptional | ((original: unknown) => CellStyle | undefined) | Style a whole row (e.g. bold totals). Called with the row's original data. |
fileNameoptional | string | File name for the download. '.csv' is added unless the name ends with it. Default 'export'. |
delimiteroptional | string | Field separator. Default ','. |
bomoptional | boolean | Start the file with a UTF-8 byte order mark so Excel reads accents. Default true. |
downloadoptional | boolean | Save the file in the browser. Default true. |
RowScope
typeWhich rows to export.
type RowScope = 'all'
| 'page'
| 'selected'PdfPageSize
typePaper sizes the PDF export supports.
type PdfPageSize = 'A4'
| 'A3'
| 'A5'
| 'LETTER'
| 'LEGAL'PdfFont
typeA TrueType / OpenType font for the PDF. Missing styles fall back to normal.
Properties
| Name | Type | Description |
|---|---|---|
name | string | Name the font is registered under in pdfmake. The whole document is set in it. |
normal | string | Regular style: base64 font data, or a URL (http:, https:, blob:, data:, or a path starting with /, ./ or ../). |
boldoptional | string | Bold style, base64 or a URL. Default: normal. |
italicsoptional | string | Italic style, base64 or a URL. Default: normal. |
bolditalicsoptional | string | Bold italic style, base64 or a URL. Default: bold, else normal. |
saveBlob
functionSave a Blob as a file in the browser. Does nothing outside one.
saveBlob(blob: Blob, fileName: string) => voidParameters
| Name | Type | Description |
|---|---|---|
blob | Blob | The file contents. |
fileName | string | Name the browser saves the file under, used as given. |
Returns
void
exportThemeFromPage
functionRead the shadcn/ui theme colors (--background, --foreground, --muted,
--border) from the page and return them as export colors. Returns no
colors outside a browser, when a variable is missing, or for a dark theme.
ExportMenu does this for you; call it when you export from your own buttons.
exportThemeFromPage(element?: Element) => ExportThemeParameters
| Name | Type | Description |
|---|---|---|
elementoptional | Element | Where to read the variables. Default: the document's root element. |
Returns
exportThemeFrom
functionThe export colors for a set of theme colors. Pure. A dark theme returns no colors: files print on white, so the light defaults fit better.
exportThemeFrom(colors: ThemeColors | null) => ExportThemeParameters
| Name | Type | Description |
|---|---|---|
colors | ThemeColors | null | The page's background, foreground, muted and border colors, as hex. |
Returns
ExportTheme
typeColors for exportPdf and exportXlsx, read from the page. Spread them into the options.
Properties
| Name | Type | Description |
|---|---|---|
pdf | Pick<PdfExportOptions, "headerTextColor" | "textColor" | "groupColor" | "groupTextColor" | "subtotalColor" | "kpiColor" | "borderColor"> | PDF colors: header and body text, group rows, lines and headline-number boxes. |
xlsx | { groupFill?: string; } | Excel colors: the group row fill. |
ThemeColors
typeThe theme colors this reads, as hex.
Properties
| Name | Type | Description |
|---|---|---|
background | string | |
foreground | string | |
muted | string | |
border | string |
luminance
functionRelative luminance of a #rrggbb color, 0 (black) to 1 (white).
luminance(hex: string) => numberParameters
| Name | Type | Description |
|---|---|---|
hex | string |
Returns
number