UNPKG

10.9 kBMarkdownView Raw
1# AutoTable - Table plugin for jsPDF
2
3[![Join the chat at https://gitter.im/simonbengtsson/jsPDF-AutoTable](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/simonbengtsson/jsPDF-AutoTable?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
4
5**Generate PDF tables with javascript**
6
7Check out the [demo](https://simonbengtsson.github.io/jsPDF-AutoTable/) to get an overview of what can be done with this plugin. Example uses include participant tables, start lists, result lists etc.
8
9![sample javascript table pdf](samples.png)
10
11### Install
12Download and include [jspdf.plugin.autotable.js](https://raw.githubusercontent.com/simonbengtsson/jsPDF-AutoTable/master/dist/jspdf.plugin.autotable.js) and [jspdf.min.js](https://raw.githubusercontent.com/MrRio/jsPDF/master/dist/jspdf.min.js).
13
14```html
15<script src="bower_components/jspdf/dist/jspdf.min.js"></script>
16<script src="bower_components/jspdf-autotable/dist/jspdf.plugin.autotable.js"></script>
17```
18
19You can also get the plugin with a package manager:
20- `bower install jspdf-autotable`
21- `npm install jspdf-autotable` (only client side usage)
22- `meteor add jspdf:autotable`
23
24It is also available on cdnjs:
25```html
26<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/2.0.16/jspdf.plugin.autotable.js"></script>
27```
28
29### Usage
30
31```javascript
32var columns = ["ID", "Name", "Country"];
33var rows = [
34 [1, "Shaw", "Tanzania", ...],
35 [2, "Nelson", "Kazakhstan", ...],
36 [3, "Garcia", "Madagascar", ...],
37 ...
38];
39
40// Only pt supported (not mm or in)
41var doc = new jsPDF('p', 'pt');
42doc.autoTable(columns, rows);
43doc.save('table.pdf');
44```
45
46### Usage with options
47
48```javascript
49var columns = [
50 {title: "ID", dataKey: "id"},
51 {title: "Name", dataKey: "name"},
52 {title: "Country", dataKey: "country"},
53 ...
54];
55var rows = [
56 {"id": 1, "name": "Shaw", "country": "Tanzania", ...},
57 {"id": 2, "name": "Nelson", "country": "Kazakhstan", ...},
58 {"id": 3, "name": "Garcia", "country": "Madagascar", ...},
59 ...
60];
61
62// Only pt supported (not mm or in)
63var doc = new jsPDF('p', 'pt');
64doc.autoTable(columns, rows, {
65 styles: {fillColor: [100, 255, 255]},
66 columnStyles: {
67 id: {fillColor: 255}
68 },
69 margin: {top: 60},
70 beforePageContent: function(data) {
71 doc.text("Header", 40, 30);
72 }
73});
74doc.save('table.pdf');
75```
76
77This plugin exports umd which means that you can use it together with build tools such as webpack, browserify and rollupjs. Simply make sure that you require the module and it should be registered correctly. The jspdf library itself has spotty support for module bundlers so you do have to make sure that you use a compatibly version of jspdf. I have had troubles with all official releases up to and including 1.2.61, but successfully tested with version 0.9.1 and 0.9.2 of a fork provided by [pavestru](https://github.com/pavestru/jsPDF).
78
79See other examples in `/examples/examples.js` which is also the source code for the [demo](https://simonbengtsson.github.io/jsPDF-AutoTable/) documents.
80
81### Options
82All options below are used in `examples.js` so be sure to check it out if in doubt.
83
84```javascript
85{
86 // Styling
87 theme: 'striped', // 'striped', 'grid' or 'plain'
88 styles: {},
89 headerStyles: {},
90 bodyStyles: {},
91 alternateRowStyles: {},
92 columnStyles: {},
93
94 // Properties
95 startY: false, // false (indicates margin top value) or a number
96 margin: 40, a number, array or object
97 pageBreak: 'auto', // 'auto', 'avoid' or 'always'
98 tableWidth: 'auto', // 'auto', 'wrap' or a number,
99
100 // Hooks
101 createdHeaderCell: function (cell, data) {},
102 createdCell: function (cell, data) {},
103 drawHeaderRow: function (row, data) {},
104 drawRow: function (row, data) {},
105 drawHeaderCell: function (cell, data) {},
106 drawCell: function (cell, data) {},
107 beforePageContent: function (data) {},
108 afterPageContent: function (data) {}
109 };
110```
111
112### Note on units
113Only pt are supported at this stage. The goal is to support all units supported by jspdf including mm and in but currently there is not timeplan for that.
114
115### Styles
116Styles work similar to css and can be overriden 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.
117
118```javascript
119{
120 cellPadding: 5,
121 fontSize: 10,
122 font: "helvetica", // helvetica, times, courier
123 lineColor: 200,
124 lineWidth: 0.1,
125 fontStyle: 'normal', // normal, bold, italic, bolditalic
126 overflow: 'ellipsize', // visible, hidden, ellipsize or linebreak
127 fillColor: 255,
128 textColor: 20,
129 halign: 'left', // left, center, right
130 valign: 'middle', // top, middle, bottom
131 fillStyle: 'F', // 'S', 'F' or 'DF' (stroke, fill or fill then stroke)
132 rowHeight: 20,
133 columnWidth: 'auto' // 'auto', 'wrap' or a number
134}
135```
136All colors can either be specified as a number (255 white and 0 for black) or an array [red, green, blue].
137
138Every style above can be changed on a cell by cell basis. However to have different rowHeights for cells in the same row or different columnWidths for cells in the same column is unsupported.
139
140Many of the styles has a matching jspdf set method. For example `fillStyle` corresponds to `doc.setFillStyle()`. More information about those can be found in the jspdf documentation.
141
142### Properties
143- `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.
144- `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}.
145- `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.
146- `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 pageand if set to 'wrap' it will only be as wide as its content is.
147
148### Hooks
149There are 8 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 alaways provided in the `data` parameter:
150- `pageCount` - The number of pages it currently spans
151- `settings` - The user options merged with the default options
152- `table` - Information about the table such as the rows, columns and dimensions
153- `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.
154
155### Helper functions
156- `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.
157- `autoTableEndPosY()` Use it if you want to know where on the page the the last row were drawn (see `multiple tables` example)
158- `autoTableAddPage` Use in the hooks to continue the table on the next page. Adds a new header automatically.
159
160### Upgrade to Version 2.0 from 1.x
161- Use the hooks (or styles and themes) instead of `renderCell`, `renderHeaderCell`, `renderFooter`and `renderHeader`
162- Custom column width now specified with the style columnWidth
163- Use `tableWidth` instead of `extendWidth`
164- Use `columnWidth: 'wrap'` instead of `overflowColumns`
165- Use `pageBreak` instead of `avoidPageSplit`
166- Use `margin` instead of `margins`
167- `autoTableHtmlToJson` now always returns an object
168- Use `API.autoTableEndPosY()` instead of `API.autoTableEndPos()`
169- Use column.x instead of cursor.x
170
171### Other pdf libraries
172
173#### [pdfmake (javascript)](https://github.com/bpampuch/pdfmake)
174I much prefer the coding style of jspdf over pdfmake, however the tables features of pdfmake are great.
175
176#### [Included jsPDF table plugin](https://github.com/MrRio/jsPDF/blob/master/jspdf.plugin.cell.js)
177No up to date documentation of how to use it (?) and has bugs. You might find it useful however.
178
179#### [fpdf (php)](http://www.fpdf.org/) and [pdfbox (java)](https://pdfbox.apache.org/)
180No included table features and have to be used server side.
181
182### Questions and issues
183If you have questions regarding how to use this plugin, please post on stackoverflow with the `jspdf-autotable` tag and I will try to answer them. If you think you have found a problem with the plugin feel free to create an issue on Github. However, try to replicate the issue on `codepen` or some similar service first. Here is a [codepen](http://codepen.io/someatoms/pen/EjwPEb) with `jspdf` and `jspdf-autotable` included that you can fork.
184
185### Contributions
186Contributions are always welcome, especially on open issues or for items in the future work section below. If you have something major you want to add or change, please post an issue about it first.
187
188- Make your changes in `src/main.js`
189- Build dist files with `npm run build`
190- Test the examples in examples.html to make sure everything looks alright
191- Submit pull request
192
193### Contributing workflow
194- Make code changes
195- Build with `npm run build`
196- Test that the examples works
197- Commit and submit pull request
198
199### Release workflow (write access to repo required)
200- Test and commit code changes
201- Run `npm version <semver|major|minor|patch> -m <optional-commit-message>`
202- Manually verify files and look over the examples
203- Deploy with `npm run deploy`
204
205### Known issues
206- The style `valign: center` gets inexact when using `overflown: linebreak` and there is around five or more rows
207
208### Future work (pull request welcome)
209- Header option `always`, `single`, `none`
210- Improve examples page (especially for firefox and mobile browsers that doesn't embed pdfs)
211- Easier way to add page numbers
212- Manually be able to add a new page with headers etc from the hooks
213- Attach html element to each data cell when using `autoTableToJson`