0
# Core Functionality
1
2
Primary table generation functionality for creating PDF tables from various data sources with comprehensive configuration support.
3
4
## Capabilities
5
6
### autoTable Function
7
8
Main table generation function that creates and renders a table in the PDF document. Supports multiple input formats including HTML tables and JavaScript data.
9
10
```typescript { .api }
11
/**
12
* Creates and draws a table in the PDF document
13
* @param doc - jsPDF document instance
14
* @param options - Configuration options for table generation
15
*/
16
function autoTable(doc: jsPDFDocument, options: UserOptions): void;
17
```
18
19
**Usage Examples:**
20
21
```typescript
22
import { jsPDF } from "jspdf";
23
import { autoTable } from "jspdf-autotable";
24
25
const doc = new jsPDF();
26
27
// Basic table from JavaScript data
28
autoTable(doc, {
29
head: [['Name', 'Age', 'City']],
30
body: [
31
['John', 25, 'New York'],
32
['Jane', 30, 'London'],
33
],
34
});
35
36
// Table from HTML element
37
autoTable(doc, {
38
html: '#data-table',
39
theme: 'striped',
40
});
41
42
// Advanced configuration
43
autoTable(doc, {
44
columns: [
45
{ header: 'Full Name', dataKey: 'name' },
46
{ header: 'Age', dataKey: 'age' },
47
{ header: 'Location', dataKey: 'city' },
48
],
49
body: [
50
{ name: 'John Doe', age: 25, city: 'New York' },
51
{ name: 'Jane Smith', age: 30, city: 'London' },
52
],
53
startY: 20,
54
theme: 'grid',
55
styles: { fontSize: 12 },
56
headStyles: { fillColor: [22, 160, 133] },
57
});
58
```
59
60
### __createTable Function (Experimental)
61
62
Creates a table object without rendering it. Useful for advanced use cases where you need to manipulate the table structure before drawing.
63
64
```typescript { .api }
65
/**
66
* Creates a table object without drawing it to the document
67
* @param doc - jsPDF document instance
68
* @param options - Configuration options for table creation
69
* @returns Table object for further manipulation or rendering
70
*/
71
function __createTable(doc: jsPDFDocument, options: UserOptions): Table;
72
```
73
74
**Usage Example:**
75
76
```typescript
77
import { jsPDF } from "jspdf";
78
import { __createTable, __drawTable } from "jspdf-autotable";
79
80
const doc = new jsPDF();
81
82
// Create table without drawing
83
const table = __createTable(doc, {
84
head: [['Name', 'Score']],
85
body: [['Alice', 95], ['Bob', 87]],
86
});
87
88
// Manipulate table properties
89
table.pageNumber = 2;
90
91
// Draw the table
92
__drawTable(doc, table);
93
```
94
95
### __drawTable Function (Experimental)
96
97
Draws an existing table object to the PDF document. Used in conjunction with `__createTable` for advanced table manipulation.
98
99
```typescript { .api }
100
/**
101
* Draws an existing table object to the PDF document
102
* @param doc - jsPDF document instance
103
* @param table - Table object to draw
104
*/
105
function __drawTable(doc: jsPDFDocument, table: Table): void;
106
```
107
108
### Data Input Formats
109
110
The core functionality supports multiple data input formats:
111
112
#### JavaScript Array Format
113
114
```typescript
115
// Array of arrays (simple format)
116
autoTable(doc, {
117
head: [['Header 1', 'Header 2', 'Header 3']],
118
body: [
119
['Row 1 Col 1', 'Row 1 Col 2', 'Row 1 Col 3'],
120
['Row 2 Col 1', 'Row 2 Col 2', 'Row 2 Col 3'],
121
],
122
});
123
```
124
125
#### Object Array Format
126
127
```typescript
128
// Array of objects with column definitions
129
autoTable(doc, {
130
columns: [
131
{ header: 'Name', dataKey: 'name' },
132
{ header: 'Email', dataKey: 'email' },
133
],
134
body: [
135
{ name: 'John Doe', email: 'john@example.com' },
136
{ name: 'Jane Smith', email: 'jane@example.com' },
137
],
138
});
139
```
140
141
#### HTML Table Format
142
143
```typescript
144
// From existing HTML table element
145
autoTable(doc, {
146
html: '#my-table-id',
147
includeHiddenHtml: false,
148
useCss: true,
149
});
150
151
// From HTML table element reference
152
const tableElement = document.getElementById('my-table');
153
autoTable(doc, {
154
html: tableElement,
155
});
156
```
157
158
### Content Sections
159
160
Tables can include header, body, and footer sections:
161
162
```typescript
163
autoTable(doc, {
164
head: [['Column 1', 'Column 2']],
165
body: [
166
['Body Row 1', 'Data 1'],
167
['Body Row 2', 'Data 2'],
168
],
169
foot: [['Footer 1', 'Footer 2']],
170
showHead: 'everyPage',
171
showFoot: 'lastPage',
172
});
173
```
174
175
### Advanced Features
176
177
#### Cell Spanning
178
179
```typescript
180
autoTable(doc, {
181
body: [
182
[
183
{ content: 'Merged Cell', colSpan: 2 },
184
// Second cell omitted due to colSpan
185
],
186
[
187
{ content: 'Tall Cell', rowSpan: 2 },
188
'Normal Cell',
189
],
190
[
191
// First cell omitted due to rowSpan
192
'Another Cell',
193
],
194
],
195
});
196
```
197
198
#### Pagination Control
199
200
```typescript
201
autoTable(doc, {
202
head: [['Name', 'Details']],
203
body: data,
204
startY: 30,
205
pageBreak: 'auto',
206
rowPageBreak: 'avoid',
207
showHead: 'everyPage',
208
});
209
```
210
211
#### Horizontal Page Breaks
212
213
```typescript
214
autoTable(doc, {
215
head: [['Col1', 'Col2', 'Col3', 'Col4', 'Col5']],
216
body: data,
217
horizontalPageBreak: true,
218
horizontalPageBreakRepeat: [0, 1], // Repeat first two columns
219
horizontalPageBreakBehaviour: 'afterAllRows',
220
});
221
```