jsPDF-AutoTable

5.0.7 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates generating PDF tables using `jspdf-autotable` with both the direct `autoTable` function call and the `doc.autoTable()` method (enabled after applying the plugin). It includes basic data-driven table generation, a more complex example with merged cells and custom styling, and adds a page number footer to each page.

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');

view raw JSON →