npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jackfox-jspdf-autotable

v2.3.6

Published

Generate pdf tables with javascript (jsPDF plugin)

Downloads

2

Readme

AutoTable - Table plugin for jsPDF

Generate PDF tables with javascript

Check out the demo to get an overview of what can be done with this plugin. Example uses include participant tables, start lists, result lists etc.

sample javascript table pdf

Install

Download and include jspdf.plugin.autotable.js and jspdf.min.js.

<script src="bower_components/jspdf/dist/jspdf.min.js"></script>
<script src="bower_components/jspdf-autotable/dist/jspdf.plugin.autotable.js"></script>

You can also get the plugin with a package manager:

  • bower install jspdf-autotable
  • npm install jspdf-autotable (only client side usage)

It is also available on cdnjs:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/2.3.5/jspdf.plugin.autotable.js"></script>

Note! If you are using meteor, use the npm release. Do not use the jspdf:autotable package on atmosphere as it is currently outdated.

Browser support

Tested with IE10, IE11 and modern browsers (chrome, edge, firefox and safari).

Usage

var columns = ["ID", "Name", "Country"];
var rows = [
    [1, "Shaw", "Tanzania", ...],
    [2, "Nelson", "Kazakhstan", ...],
    [3, "Garcia", "Madagascar", ...],
    ...
];

// Only pt supported (not mm or in)
var doc = new jsPDF('p', 'pt');
doc.autoTable(columns, rows);
doc.save('table.pdf');

You can also use it with webpack, requirejs and other module bundlers (examples).

Usage with options

var columns = [
    {title: "ID", dataKey: "id"},
    {title: "Name", dataKey: "name"}, 
    {title: "Country", dataKey: "country"}, 
    ...
];
var rows = [
    {"id": 1, "name": "Shaw", "country": "Tanzania", ...},
    {"id": 2, "name": "Nelson", "country": "Kazakhstan", ...},
    {"id": 3, "name": "Garcia", "country": "Madagascar", ...},
    ...
];

// Only pt supported (not mm or in)
var doc = new jsPDF('p', 'pt');
doc.autoTable(columns, rows, {
    styles: {fillColor: [100, 255, 255]},
    columnStyles: {
    	id: {fillColor: 255}
    },
    margin: {top: 60},
    addPageContent: function(data) {
    	doc.text("Header", 40, 30);
    }
});
doc.save('table.pdf');

See other examples in /examples/examples.js which is also the source code for the demo documents.

Usage with Angular 2 (angular cli v1.0.0-beta.14)

  • In an angular cli project run npm install jspdf-autotable --save
  • Add the jspdf and jspdf-autotable files to the scripts section in angular-cli.json (see below)
  • Declare jsPDF as a global variable declare var jsPDF: any;, and use as normal in any component
// angular-cli.json
"scripts": [
    "../node_modules/jspdf/dist/jspdf.min.js",
    "../node_modules/jspdf-autotable/dist/jspdf.plugin.autotable.js"
],
// app.component.ts or any other component
import { Component } from '@angular/core';

declare var jsPDF: any; // Important

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app works!';
  
  constructor() {
    var doc = new jsPDF('p', 'pt');
    doc.autoTable(columns, data);
    doc.save("table.pdf");
  }
}

Options

All options below are used in examples.js so be sure to check it out if in doubt.

{
    // Styling
    theme: 'striped', // 'striped', 'grid' or 'plain'
    styles: {},
    headerStyles: {},
    bodyStyles: {},
    alternateRowStyles: {},
    columnStyles: {},

    // Properties
    startY: false, // false (indicates margin top value) or a number
    margin: 40, // a number, array or object
    pageBreak: 'auto', // 'auto', 'avoid' or 'always'
    tableWidth: 'auto', // 'auto', 'wrap' or a number, 
    showHeader: 'everyPage' // 'everyPage', 'firstPage', 'never',
    tableLineColor: 200, // number, array (see color section below)
    tableLineWidth: 0,

    // Hooks
    createdHeaderCell: function (cell, data) {},
    createdCell: function (cell, data) {},
    drawHeaderRow: function (row, data) {},
    drawRow: function (row, data) {},
    drawHeaderCell: function (cell, data) {},
    drawCell: function (cell, data) {},
    addPageContent: function (data) {}
 };

Styles

Styles work similar to css and can be overridden by more specific styles. The overriding order is as follows: Default styles <- theme styles <- styles <- headerStyles and bodyStyles <- alternateRowStyles and columnStyles. It is also possible to override specific cell or row styles using for example the createdCell hook. Checkout the Custom style example for more information.

{
	cellPadding: 5, // a number, array or object (see margin below)
    fontSize: 10,
    font: "helvetica", // helvetica, times, courier
    lineColor: 200,
    lineWidth: 0,
    fontStyle: 'normal', // normal, bold, italic, bolditalic
    overflow: 'ellipsize', // visible, hidden, ellipsize or linebreak
    fillColor: false, // false for transparent or a color as described below
    textColor: 20,
    halign: 'left', // left, center, right
    valign: 'middle', // top, middle, bottom
    columnWidth: 'auto' // 'auto', 'wrap' or a number
}

All colors can either be specified as a number (255 white and 0 for black) or an array [red, green, blue] e.g. [255, 255, 255].

Every style above can be changed on a cell by cell basis except for columnWidth.

Many of the styles has a matching jspdf set method. For example lineWidth corresponds to doc.setLineWidth(). More information about those can be found in the jspdf documentation.

Properties

  • startY Indicates where the table should start to be drawn on the first page (overriding the margin top value). It can be used for example to draw content before the table. Many examples use this option, but the above use case is presented in the With content example.
  • margin Similar to margin in css it sets how much spacing it should be around the table on each page. The startY option can be used if the margin top value should be different on the first page. The margin option accepts both a number, an array [top, right, bottom, left] and an object {top: 40, right: 40, bottom: 40, left: 40}. If you want to use the default value and only change one side you can specify it like this: {top: 60}.
  • pageBreak This option defines the behavior of the table when it will span more than one page. If set to 'always' each table will always start on a new page. If set to 'avoid' it will start on a new page if there is not enough room to fit the entire table on the current page. If set to 'auto' it will add a new page only when the next row doesn't fit.
  • tableWidth This option defines the fixed width of the table if set to a number. If set to 'auto' it will be 100% of width of the page and if set to 'wrap' it will only be as wide as its content is.
  • showHeader Set to firstPage, everyPage or never

Hooks

There are 9 different hooks that gets called at various times during the drawing of the table. If applicable, information about the current cell, row or column are provided to the hook function. In addition to that the following general information is always provided in the data parameter:

  • pageCount - The number of pages it currently spans
  • settings - The user options merged with the default options
  • table - Information about the table such as the rows, columns and dimensions
  • doc - The current jspdf instance
  • cursor - The position at which the next table cell will be drawn. This can be assigned new values to create column and row spans. Checkout the Colspan and Rowspan example for more information.

OBS! Only the drawCell hook can be used with the native style jspdf style changes such as doc.setLineWidth. If you use the other hooks for changing styles, they will be overridden.

Helper functions

  • autoTableHtmlToJson(tableElem, includeHiddenElements) Use it to generate the javascript objects required for this library from an html table (see from html example). If includeHiddenElements is set to true hidden rows and columns will be included otherwise excluded.
  • doc.autoTableSetDefaults({ ... }). Use for setting default options for all tables on the specific document. Settings and styles will be overridden in the following order global < document < table. Hooks will be added and not overridden.
  • jsPDF.autoTableSetDefaults({ ... }) Use for setting global defaults which will be applied for all document and tabels.

If you want to know something about the last table that was drawn you can use doc.autoTable.previous. It has a doc.autoTable.previous.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.

Other pdf libraries

  • pdfmake (javascript) I much prefer the coding style of jspdf over pdfmake, however the tables features of pdfmake are great. And pdfmake have proper support for utf-8 which jspdf lacks.
  • Included jsPDF table plugin No up to date documentation of how to use it (?) and has bugs.
  • fpdf (php) and pdfbox (java) No included table features and have to be used server side.

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:

  • Make code changes
  • Start watcher with npm start (will build files on file changes)
  • Test that the examples works in examples/index.html
  • Commit and submit pull request

Release workflow (write access to repo required)

  • Test and commit code changes
  • Run npm version <semver|major|minor|patch> -m <optional-commit-message>
  • Manually verify files and look over the examples
  • Deploy with npm run deploy