jsPDF-AutoTable
jsPDF-AutoTable is a widely-used plugin for the jsPDF library, enabling the generation of tabular data within PDF documents directly from JavaScript. It supports parsing existing HTML tables or constructing tables programmatically using JavaScript arrays for header, body, and footer content. The current stable version is 5.0.7, with active development indicated by frequent releases addressing bug fixes, dependency updates, and feature enhancements. Key differentiators include its tight integration with jsPDF, flexibility in content sources (HTML or raw data), and comprehensive styling options, making it a robust solution for dynamically generating structured reports and documents. It provides fine-grained control over cell spanning, styling, and column definitions, and ships with TypeScript types for improved developer experience.
Common errors
-
TypeError: doc.autoTable is not a function
cause The `jspdf-autotable` plugin was not correctly applied to the jsPDF instance or class, particularly in non-browser environments since v5.0.0.fixIf using an ESM setup or in Node.js since v5, ensure you explicitly call `applyPlugin(jsPDF)` after importing jsPDF and `applyPlugin`. For example: `import { jsPDF } from 'jspdf'; import { applyPlugin } from 'jspdf-autotable'; applyPlugin(jsPDF); const doc = new jsPDF(); doc.autoTable({...});` Alternatively, use the direct function call: `autoTable(doc, { ... });` -
Cannot find module 'jspdf-autotable/es'
cause This import path was specific to older versions (pre-v5) for explicitly loading the ESM build. Since v5.0.0, the correct build is chosen automatically based on your project setup.fixRemove the `/es` suffix from your import path: `import { autoTable } from 'jspdf-autotable'`. -
Property 'autoTable' does not exist on type 'jsPDF' (TypeScript error)
cause TypeScript compiler error indicating that the `jsPDF` object does not have the `autoTable` method. This typically means the `jspdf-autotable` plugin's type augmentations haven't been correctly picked up by TypeScript, or the plugin hasn't been enabled in your code.fixEnsure `jspdf-autotable` is installed and `applyPlugin(jsPDF)` is called in your application's initialization logic. If the issue persists, verify your `tsconfig.json` correctly includes `jspdf` and `jspdf-autotable` in the `types` array or `typeRoots` (though usually installing the packages is sufficient for type discovery). -
Error: Deprecated option 'styles.columnWidth' used. Use 'columnStyles.0.cellWidth' instead.
cause You are attempting to use an old option or style property that was deprecated in earlier versions and completely removed in v4.0.0.fixRefer to the `jspdf-autotable` documentation for v4.0.0+ options and styles. Update your table configuration to use the current properties, for instance, replacing `styles.columnWidth` with `columnStyles.<column_index_or_dataKey>.cellWidth`.
Warnings
- breaking Since v5.0.0, the `jspdf-autotable` plugin is no longer automatically applied to jsPDF instances in non-browser environments (e.g., Node.js). If you are using `doc.autoTable()` in such an environment, you must explicitly call `applyPlugin(jsPDF)` or `applyPlugin(doc)` to enable this method.
- breaking With v4.0.0, several long-time deprecated options and styling properties were completely removed. Additionally, the `autoTable` TypeScript type was renamed to `autoTableInstanceType`. This version also upgraded its peer dependency to jsPDF v3.0, which dropped support for Internet Explorer.
- gotcha Since v5.0.0, `jspdf-autotable` automatically selects the correct ESM build file for your environment. Explicitly importing from `jspdf-autotable/es` is no longer necessary and may lead to import errors.
- gotcha jsPDF-AutoTable has a peer dependency on `jspdf`, requiring versions `^2 || ^3 || ^4`. Using an incompatible version of `jspdf` (e.g., v1.x or v5.x if released) can lead to runtime errors or unexpected behavior due to API changes.
Install
-
npm install jspdf-autotable -
yarn add jspdf-autotable -
pnpm add jspdf-autotable
Imports
- autoTable
const autoTable = require('jspdf-autotable').autoTableimport { autoTable } from 'jspdf-autotable' - applyPlugin
import autoTable from 'jspdf-autotable'
import { applyPlugin } from 'jspdf-autotable' - HookData
import { type HookData } from 'jspdf-autotable' - autoTableInstanceType
import { type autoTableInstanceType } from 'jspdf-autotable'
Quickstart
import { jsPDF } from 'jspdf';
import { autoTable, applyPlugin } from 'jspdf-autotable';
// Apply the plugin to the jsPDF class. This makes `doc.autoTable()` available
// and is required in non-browser environments since v5.0.0 if using that syntax.
applyPlugin(jsPDF);
const doc = new jsPDF();
console.log('Generating PDF...');
// Example 1: Generate a simple table using the direct `autoTable` function call
autoTable(doc, {
head: [['ID', 'Name', 'Email', 'Country']],
body: [
['1', 'David', 'david@example.com', 'Sweden'],
['2', 'Castille', 'castille@example.com', 'Spain'],
['3', 'Alice', 'alice@example.com', 'Canada'],
['4', 'Bob', 'bob@example.com', 'Germany'],
['5', 'Charlie', 'charlie@example.com', 'France'],
],
startY: 20,
didDrawPage: (data) => {
// Optional: Add a footer with page numbers on each page
doc.setFontSize(10);
const pageText = `Page ${data.pageNumber} of ${data.pageCount}`;
const x = data.settings.margin.left;
const y = doc.internal.pageSize.height - 10;
doc.text(pageText, x, y);
},
});
// Example 2: Generate a more complex table with merged cells and styles
// using the `doc.autoTable()` method (enabled by `applyPlugin(jsPDF)`)
doc.addPage();
doc.setFontSize(16);
doc.text('Detailed Report', 10, 15);
doc.autoTable({
head: [
[{ content: 'Sales Summary Q1', colSpan: 4, styles: { halign: 'center', fillColor: [200, 220, 255] } }],
['Product', 'Region', 'Revenue', 'Notes']
],
body: [
['Laptop Pro', 'North', '1,200,000', 'Best seller'],
['Desktop Evo', { content: 'South (Merged)', colSpan: 2, styles: { fillColor: [255, 240, 200] } }, 'Stable'],
['Tablet Mini', 'West', '450,000', 'New release'],
[{ content: 'Total Sales', colSpan: 2, styles: { halign: 'right', fontStyle: 'bold' } }, '2,000,000', { content: 'Overall', styles: { fontStyle: 'bold' } }]
],
startY: 25,
styles: { font: 'helvetica', fontSize: 10, cellPadding: 2 },
headStyles: { fillColor: [100, 150, 200], textColor: 255, fontStyle: 'bold' },
bodyStyles: { textColor: 50 },
footStyles: { fillColor: [200, 200, 200] },
alternateRowStyles: { fillColor: [240, 240, 240] },
});
doc.save('autotable_example.pdf');
console.log('PDF generated as autotable_example.pdf');