forked from simonbengtsson/jsPDF-AutoTable
/
simple.html
76 lines (69 loc) · 1.81 KB
/
simple.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Simple example</title>
</head>
<style>
html {
box-sizing: border-box;
}
*, *:before, *:after {
box-sizing: inherit;
}
</style>
<body>
<button onclick="generate()">Generate pdf</button>
<script src="libs/jspdf.debug.js"></script>
<script src="libs/jspdf.plugin.autotable.js"></script>
<table id="table">
<thead>
<tr>
<th>ID</th>
<th>First name</th>
<th>Last name</th>
<th>Email</th>
<th>Country</th>
<th>IP-address</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Donna</td>
<td>Moore</td>
<td>dmoore0@furl.net</td>
<td>China</td>
<td>211.56.242.221</td>
</tr>
<tr>
<td>2</td>
<td>Janice</td>
<td>Henry</td>
<td>jhenry1@theatlantic.com</td>
<td>Ukraine</td>
<td>38.36.7.199</td>
</tr>
</tbody>
</table>
<script>
function generate() {
var doc = new jsPDF();
// DATA EXAMPLE - Use this initiation when you hard code data or get dynamic data from an API
var columns = ["ID", "Country", "Rank", "Capital"];
var data = [
[1, "Denmark", 7.526, "Copenhagen"],
[2, "Switzerland", 7.509, "Bern"],
[3, "Iceland", 7.501, "Reykjavík"],
[4, "Norway", 7.498, "Oslo"],
[5, "Finland", 7.413, "Helsinki"]
];
// HTML EXAMPLE - Comment out the data example and use this when you want to get a pdf of an html table
//var element = document.getElementById('table');
//var res = doc.autoTableHtmlToJson(element);
//doc.autoTable(res.columns, res.data)
doc.autoTable(columns, data);
}
</script>
</body>
</html>