Search code examples
javascriptjspdf-autotable

jsPDF Autotable API?


Just found the Autotable plugin for jsPDF and there seems to be a little bit of a learning curve for me as I try to use this. I see the examples.js that comes with the file, but is there a full API somewhere where I can see all methods?

For example the didParseCell section there is this code:

  // Use for customizing texts or styles of specific cells after they have been formatted by this plugin.
// This hook is called just before the column width and other features are computed.
  didParseCell: function (data) {
  if (data.row.index === 5) {
    data.cell.styles.fillColor = [40, 170, 100]
  }

  if (
    (data.row.section === 'head' || data.row.section === 'foot') &&
    data.column.dataKey === 'expenses'
  ) {
    data.cell.text = '' // Use an icon in didDrawCell instead
  }

  if (data.column.dataKey === 'city') {
    data.cell.styles.font = 'mitubachi'
    if (data.row.section === 'head') {
      data.cell.text = 'シティ'
    }
    if (data.row.index === 0 && data.row.section === 'body') {
      data.cell.text = 'とうきょう'
    }
  }
},

I'd like to know what methods/properties I can access for "data". Is there a full API that lists these somewhere?


Solution

  • There is readme.md available as documentation. For more details they are suggesting to read the class definitions for HookData, Table, Row, Cell etc. defined in models.ts:

    To see what is included in the Table, Row, Column and Cell types, either log them to the console or take a look at src/models.ts


    For ready reference here are the definitions:

    class HookData {
      table: Table
      pageNumber: number
      pageCount: number // Deprecated, use pageNumber instead
      settings: Settings
      doc: jsPDFDocument
      cursor: Pos | null
    }
    
    class CellHookData extends HookData {
      cell: Cell
      row: Row
      column: Column
      section: 'head' | 'body' | 'foot'
    }
    
    class Table {
      id?: string | number
      settings: Settings
      styles: StylesProps
      hooks: HookProps
      columns: Column[]
      head: Row[]
      body: Row[]
      foot: Row[]
      pageNumber = 1
      finalY?: number
      startPageNumber?: number
    }
    
    class Row {
      readonly raw: HTMLTableRowElement | RowInput
      readonly element?: HTMLTableRowElement
      readonly index: number
      readonly section: Section
      readonly cells: { [key: string]: Cell }
      spansMultiplePages: boolean
    
      height = 0
    }
    
    class Cell {
      raw: HTMLTableCellElement | CellInput
      styles: Styles
      text: string[]
      section: Section
      colSpan: number
      rowSpan: number
    
      contentHeight = 0
      contentWidth = 0
      wrappedWidth = 0
      minReadableWidth = 0
      minWidth = 0
    
      width = 0
      height = 0
      x = 0
      y = 0
    }
    
    class Column {
      raw: ColumnInput | null
      dataKey: string | number
      index: number
    
      wrappedWidth = 0
      minReadableWidth = 0
      minWidth = 0
      width = 0
    }