Installations
npm install watanabe-jspdf-autotable
Developer Guide
Typescript
Yes
Module System
CommonJS
Node Version
15.2.0
NPM Version
7.0.8
Score
53.4
Supply Chain
96.8
Quality
74.3
Maintenance
100
Vulnerability
99.3
License
Releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (90.86%)
HTML (6.77%)
JavaScript (2.38%)
Developer
Download Statistics
Total Downloads
931
Last Day
6
Last Week
8
Last Month
15
Last Year
227
GitHub Statistics
2,349 Stars
1,128 Commits
624 Forks
57 Watching
3 Branches
48 Contributors
Package Meta Information
Latest Version
3.5.13
Package Id
watanabe-jspdf-autotable@3.5.13
Unpacked Size
141.86 kB
Size
35.61 kB
File Count
7
NPM Version
7.0.8
Node Version
15.2.0
Total Downloads
Cumulative downloads
Total Downloads
931
Last day
500%
6
Compared to previous day
Last week
166.7%
8
Compared to previous week
Last month
87.5%
15
Compared to previous month
Last year
50.3%
227
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Take the developer survey!
jsPDF-AutoTable - Table plugin for jsPDF
Generate PDF tables with Javascript
This jsPDF plugin adds the ability to generate PDF tables either by parsing HTML tables or by using Javascript data directly. Check out the demo or examples.
Installation
Get jsPDF and this plugin by doing one of these things:
npm install jspdf jspdf-autotable
- Download jspdf and jspdf-autotable from github
- Use a CDN, for example: https://unpkg.com/jspdf and https://unpkg.com/jspdf-autotable
Usage
1import jsPDF from 'jspdf' 2import 'jspdf-autotable' 3 4const doc = new jsPDF() 5 6// It can parse html: 7// <table id="my-table"><!-- ... --></table> 8doc.autoTable({ html: '#my-table' }) 9 10// Or use javascript directly: 11doc.autoTable({ 12 head: [['Name', 'Email', 'Country']], 13 body: [ 14 ['David', 'david@example.com', 'Sweden'], 15 ['Castille', 'castille@example.com', 'Spain'], 16 // ... 17 ], 18}) 19 20doc.save('table.pdf')
You can also use the exported autoTable method. This works better with typescript and alternative jsPDF versions.
1import jsPDF from 'jspdf' 2// import jsPDF = require('jspdf') // // typescript without esModuleInterop flag 3// import jsPDF from 'yworks-pdf' // using yworks fork 4// import jsPDF from 'jspdf/dist/jspdf.node.debug' // for nodejs 5import autoTable from 'jspdf-autotable' 6 7const doc = new jsPDF() 8autoTable(doc, { html: '#my-table' }) 9doc.save('table.pdf')
The third usage option is with downloaded or CDN dist files
1<script src="jspdf.min.js"></script> 2<script src="jspdf.plugin.autotable.min.js"></script> 3<script> 4 var doc = new jsPDF() 5 doc.autoTable({ html: '#my-table' }) 6 doc.save('table.pdf') 7</script>
Checkout more examples in examples.js which is also the source code for the demo documents.
Options
Below is a list of all options supported in the plugin. All of them are used in the examples.
Content options
The only thing required is either the html or body option. If you want more control over the columns you can specify the columns property. If columns are not set they will be automatically computed based on the content of the html content or head, body and foot.
html: string|HTMLTableElement
A css selector (for example "#table") or an html table element.head: CellDef[][]
For example [['ID', 'Name', 'Country']]body: CellDef[][]
For example [['1', 'Simon', 'Sweden'], ['2', 'Karl', 'Norway']]foot: CellDef[][]
For example [['ID', 'Name', 'Country']]columns: ColumnDef[]
For example [{header: 'ID', dataKey: 'id'}, {header: 'Name', dataKey: 'name'}]. Only use this option if you want more control over the columns. If not specified the columns will be automatically generated based on the content in html or head/body/footincludeHiddenHtml: boolean = false
If hidden html withdisplay: none
should be included or not when the content comes from an html table
CellDef: string|{content: string, rowSpan: number, colSpan: number, styles: StyleDef}
Note that cell styles can also be set dynamically with hooks.
ColumnDef: string|{header?: string, dataKey: string}
The header property is optional and the values of any content in head
will be used if not set. Normally it's easier to use the html or head/body/foot style of initiating a table, but columns can be useful if your body content comes directly from an api or if you would like to specify a dataKey on each column to make it more readable to style specific columns in the hooks or columnStyles.
Usage with colspan, rowspan and inline cell styles:
1doc.autoTable({ 2 body: [ 3 [{ content: 'Text', colSpan: 2, rowSpan: 2, styles: { halign: 'center' } }], 4 ], 5})
Styling options
theme: 'striped'|'grid'|'plain'|'css' = 'striped'
styles: StyleDef
headStyles: StyleDef
bodyStyles: StyleDef
footStyles: StyleDef
alternateRowStyles: StyleDef
columnStyles: {&columnDataKey: StyleDef}
Note that the columnDataKey is normally the index of the column, but could also be thedataKey
of a column if content initialized with the columns property
StyleDef
:
font: 'helvetica'|'times'|'courier' = 'helvetica'
fontStyle: 'normal'|'bold'|'italic'|'bolditalic' = 'normal'
overflow: 'linebreak'|'ellipsize'|'visible'|'hidden' = 'normal'
fillColor: Color? = null
textColor: Color? = 20
cellWidth: 'auto'|'wrap'|number = 'auto'
minCellWidth: number? = 10
minCellHeight: number = 0
halign: 'left'|'center'|'right' = 'left'
valign: 'top'|'middle'|'bottom' = 'top'
fontSize: number = 10
cellPadding: Padding = 10
lineColor: Color = 10
lineWidth: number = 0
// If 0, no border is drawn
Color
:
Either false for transparent, hex string, gray level 0-255 or rbg array e.g. [255, 0, 0]
false|string|number|[number, number, number]
Padding
:
Either a number or object {top: number, right: number, bottom: number, left: number}
Styles work similar to css and can be overridden by more specific styles. Overriding order:
- Theme styles
styles
headStyles
,bodyStyles
andfootStyles
alternateRowStyles
columnStyles
Styles for specific cells can also be applied using either the hooks (see hooks section above) or the styles
property on the cell definition object (see content section above).
Example usage of column styles (note that the 0 in the columnStyles below should be dataKey if columns option used)
1// Example usage with columnStyles, 2doc.autoTable({ 3 styles: { fillColor: [255, 0, 0] }, 4 columnStyles: { 0: { halign: 'center', fillColor: [0, 255, 0] } }, // Cells in first column centered and green 5 margin: { top: 10 }, 6 body: [ 7 ['Sweden', 'Japan', 'Canada'], 8 ['Norway', 'China', 'USA'], 9 ['Denmark', 'China', 'Mexico'], 10 ], 11}) 12 13// Example usage of columns property. Note that America will not be included even though it exist in the body since there is no column specified for it. 14doc.autoTable({ 15 columnStyles: { europe: { halign: 'center' } }, // European countries centered 16 body: [ 17 { europe: 'Sweden', america: 'Canada', asia: 'China' }, 18 { europe: 'Norway', america: 'Mexico', asia: 'Japan' }, 19 ], 20 columns: [ 21 { header: 'Europe', dataKey: 'europe' }, 22 { header: 'Asia', dataKey: 'asia' }, 23 ], 24})
Other options
startY: number = null
Where the table should start to be printed (basically a margin top value only for the first page)margin: Margin = 40
pageBreak: 'auto'|'avoid'|'always'
If set toavoid
the plugin will only split a table onto multiple pages if table height is larger than page height.rowPageBreak: 'auto'|'avoid' = 'auto'
If set toavoid
the plugin will only split a row onto multiple pages if row height is larger than page height.tableWidth: 'auto'|'wrap'|number = 'auto'
showHead: 'everyPage'|'firstPage'|'never' = 'everyPage''
showFoot: 'everyPage'|'lastPage'|'never' = 'everyPage''
tableLineWidth: number = 0
tableLineColor: Color = 200
The table line/border color
Margin
:
Either a number or object {top: number, right: number, bottom: number, left: number}
Hooks
You can customize the content and styling of the table by using the hooks. See the custom styles example for usage of the hooks.
didParseCell: (HookData) => {}
- Called when the plugin finished parsing cell content. Can be used to override content or styles for a specific cell.willDrawCell: (HookData) => {}
- Called before a cell or row is drawn. Can be used to call native jspdf styling functions such asdoc.setTextColor
or change position of text etc before it is drawn.didDrawCell: (HookData) => {}
- Called after a cell has been added to the page. Can be used to draw additional cell content such as images withdoc.addImage
, additional text withdoc.addText
or other jspdf shapes.didDrawPage: (HookData) => {}
- Called after the plugin has finished drawing everything on a page. Can be used to add headers and footers with page numbers or any other content that you want on each page there is an autotable.
All hooks functions get passed an HookData object with information about the state of the table and cell. For example the position on the page, which page it is on etc.
HookData
:
table: Table
pageNumber: number
The page number specific to this tablesettings: object
Parsed user supplied optionsdoc
The jsPDF document instance of this tablecursor: { x: number, y: number }
To draw each table this plugin keeps a cursor state where the next cell/row should be drawn. You can assign new values to this cursor to dynamically change how the cells and rows are drawn.
For cell hooks these properties are also passed:
cell: Cell
row: Row
column: Column
section: 'head'|'body'|'foot'
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
1// Example with an image drawn in each cell in the first column 2doc.autoTable({ 3 didDrawCell: (data) => { 4 if (data.section === 'body' && data.column.index === 0) { 5 var base64Img = 'data:image/jpeg;base64,iVBORw0KGgoAAAANS...' 6 doc.addImage(base64Img, 'JPEG', data.cell.x + 2, data.cell.y + 2, 10, 10) 7 } 8 }, 9})
API
doc.autoTable({ /* options */ })
jsPDF.autoTableSetDefaults({ /* ... */ })
Use for setting global defaults which will be applied for all tables
If you want to know something about the last table that was drawn you can use doc.lastAutoTable
. It has a doc.lastAutoTable.finalY
property among other things that has the value of the last printed y coordinate on a page. This can be used to draw text, multiple tables or other content after a table.
In addition to the exported autoTable(doc, options) method you can also use applyPlugin to add the autoTable api to any jsPDF instance.
import jsPDF from 'jspdf/dist/jspdf.node.debug'
import { applyPlugin } from 'jspdf-autotable'
applyPlugin(jsPDF)
Contributions
Contributions are always welcome, especially on open issues. If you have something major you want to add or change, please post an issue about it first to discuss it further. The workflow for contributing would be something like this:
- Start watcher with
npm start
- Make code changes
- Make sure all examples works
- Commit and submit pull request
If you don't use Prettier on autosave, please run yarn run format-all
before opening your PR
Release workflow (write access required)
npm version <semver|major|minor|patch>
npm run deploy
- Verify release at https://simonbengtsson.github.io/jsPDF-AutoTable
Pull requests locally
git fetch origin pull/478/head:pr478
git checkout pr478
Release prerelease
npm version prerelease
git push && git push --tags && npm publish --tag alpha
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE.txt:0
- Info: FSF or OSI recognized license: MIT License: LICENSE.txt:0
Reason
SAST tool detected but not run on all commits
Details
- Info: SAST configuration detected: CodeQL
- Warn: 3 commits out of 9 are checked with a SAST tool
Reason
4 commit(s) and 4 issue activity found in the last 90 days -- score normalized to 6
Reason
security policy file detected
Details
- Info: security policy file detected: SECURITY.md:1
- Warn: no linked content found
- Info: Found disclosure, vulnerability, and/or timelines in security policy: SECURITY.md:1
- Info: Found text in security policy: SECURITY.md:1
Reason
Found 4/25 approved changesets -- score normalized to 1
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Info: jobLevel 'contents' permission set to 'read': .github/workflows/codeql-analysis.yml:17
- Info: jobLevel 'actions' permission set to 'read': .github/workflows/codeql-analysis.yml:16
- Warn: no topLevel permission defined: .github/workflows/codeql-analysis.yml:1
- Warn: no topLevel permission defined: .github/workflows/nodejs.yml:1
- Warn: no topLevel permission defined: .github/workflows/playground.yml:1
- Warn: no topLevel permission defined: .github/workflows/release.yml:1
- Warn: no topLevel permission defined: .github/workflows/stale.yml:1
- Info: no jobLevel write permissions found
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/codeql-analysis.yml:27: update your workflow using https://app.stepsecurity.io/secureworkflow/simonbengtsson/jsPDF-AutoTable/codeql-analysis.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/codeql-analysis.yml:30: update your workflow using https://app.stepsecurity.io/secureworkflow/simonbengtsson/jsPDF-AutoTable/codeql-analysis.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/codeql-analysis.yml:35: update your workflow using https://app.stepsecurity.io/secureworkflow/simonbengtsson/jsPDF-AutoTable/codeql-analysis.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/codeql-analysis.yml:38: update your workflow using https://app.stepsecurity.io/secureworkflow/simonbengtsson/jsPDF-AutoTable/codeql-analysis.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/nodejs.yml:11: update your workflow using https://app.stepsecurity.io/secureworkflow/simonbengtsson/jsPDF-AutoTable/nodejs.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/nodejs.yml:12: update your workflow using https://app.stepsecurity.io/secureworkflow/simonbengtsson/jsPDF-AutoTable/nodejs.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/playground.yml:10: update your workflow using https://app.stepsecurity.io/secureworkflow/simonbengtsson/jsPDF-AutoTable/playground.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/playground.yml:11: update your workflow using https://app.stepsecurity.io/secureworkflow/simonbengtsson/jsPDF-AutoTable/playground.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release.yml:15: update your workflow using https://app.stepsecurity.io/secureworkflow/simonbengtsson/jsPDF-AutoTable/release.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release.yml:16: update your workflow using https://app.stepsecurity.io/secureworkflow/simonbengtsson/jsPDF-AutoTable/release.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/stale.yml:10: update your workflow using https://app.stepsecurity.io/secureworkflow/simonbengtsson/jsPDF-AutoTable/stale.yml/master?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/nodejs.yml:16
- Info: 0 out of 11 GitHub-owned GitHubAction dependencies pinned
- Info: 1 out of 2 npmCommand dependencies pinned
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
24 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-p3vf-v8qc-cwcr
- Warn: Project is vulnerable to: GHSA-gx9m-whjm-85jf
- Warn: Project is vulnerable to: GHSA-mmhx-hmjr-r674
- Warn: Project is vulnerable to: GHSA-4vvj-4cpr-p986
- Warn: Project is vulnerable to: GHSA-qwcr-r2fm-qrc7
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-pxg6-pf52-xh8x
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-rv95-896h-c2vc
- Warn: Project is vulnerable to: GHSA-qw6h-vgh9-j6wx
- Warn: Project is vulnerable to: GHSA-cxjh-pqwp-8mfp
- Warn: Project is vulnerable to: GHSA-c7qv-q95q-8v27
- Warn: Project is vulnerable to: GHSA-78xj-cgh5-2h22
- Warn: Project is vulnerable to: GHSA-2p57-rm9w-gvfp
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-mwcw-c2x4-8c55
- Warn: Project is vulnerable to: GHSA-9wv6-86v2-598j
- Warn: Project is vulnerable to: GHSA-rhx6-c78j-4q9w
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
- Warn: Project is vulnerable to: GHSA-m6fv-jmcg-4jfg
- Warn: Project is vulnerable to: GHSA-cm22-4g7w-348p
- Warn: Project is vulnerable to: GHSA-f5x3-32g6-xq36
- Warn: Project is vulnerable to: GHSA-wr3j-pwj9-hqq6
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
Score
3.8
/10
Last Scanned on 2024-12-16
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More