jsPDF-AutoTable

repository·main·Indexed 25 days ago

https://github.com/simonbengtsson/jspdf-autotable

A plugin for jsPDF that simplifies the generation of complex PDF tables. It supports direct JavaScript data input and automatic parsing of HTML tables. Key features include customizable themes (striped, grid, plain), advanced styling for cells and text, and a comprehensive hook system (didParseCell, willDrawCell, didDrawPage, etc.) to intercept and modify the table generation process. Compatible with module bundlers like webpack, browserify, and requirejs.

Tokens
4.4K
Snippets
5
Records
36
Agent score
82%

What's inside jspdf-autotable

  1. Use hooks to customize table rendering

    main

    Hooks allow you to intercept the rendering process to modify content or draw custom elements (like images) during specific lifecycle stages of the table generation.

    // Example: Drawing an image in the first column of every body row
    autoTable(doc, {
      didDrawCell: (data) => {
        if (data.section === 'body' && data.column.index === 0) {
          const base64Img = 'data:image/jpeg;base64,iVBORw0KGgoAAAANS...'
          doc.addImage(base64Img, 'JPEG', data.cell.x + 2, data.cell.y + 2, 10, 10)
        }
      },
    })
  2. Integrate autoTable into the jsPDF instance

    main

    Instead of calling the standalone autoTable function, you can attach the plugin methods directly to the jsPDF prototype using applyPlugin. This allows you to call doc.autoTable() on any instance.

    import { jsPDF } from 'jspdf'
    import { applyPlugin } from 'jspdf-autotable'
    
    applyPlugin(jsPDF)
    
    const doc = new jsPDF()
    doc.autoTable({ html: '#my-table' })
    doc.save('table.pdf')
  3. Generate PDF tables using jsPDF-AutoTable

    main

    jsPDF-AutoTable allows you to generate tables in two primary ways: by parsing an existing HTML table via a CSS selector or by providing JavaScript data directly (head, body, and foot).

    import { jsPDF } from 'jspdf'
    import { autoTable } from 'jspdf-autotable'
    
    const doc = new jsPDF()
    
    // Option 1: Parse HTML
    // <table id="my-table">...</table>
    autoTable(doc, { html: '#my-table' })
    
    // Option 2: Use JavaScript data
    autoTable(doc, {
      head: [['Name', 'Email', 'Country']],
      body: [
        ['David', 'david@example.com', 'Sweden'],
        ['Castille', 'castille@example.com', 'Spain'],
      ],
    })
    
    doc.save('table.pdf')
  4. Use jspdf-autotable with module bundlers

    main

    The plugin supports common module bundlers including browserify, webpack, and requirejs.

    To use these examples locally:

    • For webpack and browserify examples, you must run npm install first.
    • Open the respective index.html file for the bundler you wish to test; they are configured to work out of the box.

    Compatibility Note: Ensure you are using a modern version of jsPDF. Versions prior to jsPDF 1.3 may not work correctly with module bundlers.

  5. How options are merged and prioritized

    main

    jsPDF-AutoTable uses a hierarchical configuration system. Options can be defined at three levels: Global (jsPDF document level), Document (jsPDF-AutoTable document level), and Current (the specific table being generated).

    When generating a table, the plugin merges these levels using a priority order where the Current options override Document options, which in turn override Global options. This applies to settings, styles, and hooks.

  6. Use table hooks to intercept drawing and parsing

    main

    The HookProps interface allows you to register callback functions (hooks) that trigger at specific stages of the table generation process. Hooks can be used to modify cell content, styles, or page behavior.

    Cell Hooks

    These hooks provide access to CellHookData (containing the document, table, cell, row, column, and cursor):

    • didParseCell: Triggered after a cell is parsed.
    • willDrawCell: Triggered before a cell is drawn.
    • didDrawCell: Triggered after a cell is drawn.

    Page Hooks

    These hooks provide access to HookData (containing the document, table, and cursor):

    • willDrawPage: Triggered before a page is drawn.
    • didDrawPage: Triggered after a page is drawn.

    Note: Returning false from a hook can stop the execution of subsequent handlers in that sequence.

  7. Access the last drawn table via doc.lastAutoTable

    main
    After calling autoTable, you can access the metadata of the table just created via doc.lastAutoTable. A common use case is retrieving doc.lastAutoTable.finalY to determine where to start drawing the next element (like another table or text) to avoid overlapping.
  8. Configure table margins and starting position

    main

    Control the layout positioning of the table:

    • margin: Defines the page margins. Supports spacing syntax.
    • startY: Defines the vertical starting position of the table.
      • If a number is provided, the table starts at that Y coordinate.
      • If false or undefined, the plugin attempts to place the table after the previous table (if one exists) or at the top margin.