admin管理员组文章数量:1327446
I am trying to bine the "TABLE FROM HTML" with a "Header"... Looked at the examples. I can get each one working separately, but not together.
When I bine the two, I am having problems...Can you see what I am doing wrong here and why this doesnt work ??
Might be in my header VAR... Also, it there a way to exclude a column from the export ???
Using this
<html>
<body>
<button onclick="generate()">Download PDF</button>
<script src="jspdf/jspdf.min.js"></script>
<script src="jspdf/jspdf.plugin.autotable.src.js"></script>
<script>
function generate() {
var doc = new jsPDF('p', 'pt');
var res = doc.autoTableHtmlToJson(document.getElementById("basic-table"));
doc.autoTable(res.columns, res.data, options);
var header = function (data) {
doc.setFontSize(18);
doc.setTextColor(40);
doc.setFontStyle('normal');
doc.addImage(headerImgData, 'JPEG', data.settings.margin.left, 20, 50, 50);
doc.text("Testing Report", data.settings.margin.left + 55, 50);
};
var options = {
beforePageContent: header,
margin: {top: 80}
};
doc.autoTable(columns, data, options);
doc.save("table.pdf");
}
var headerImgData = 'data:image/jpeg;base64,/9... my dataimage';
</script>
<br/><br/>
<table id="basic-table">
<thead>
<tr>
<th title="Field #1">ID</th>
<th title="Field #2">First name</th>
<th title="Field #3">Last name</th>
<th title="Field #4">Email</th>
<th title="Field #5">Country</th>
<th title="Field #6">IP-address</th>
</tr>
</thead>
<tbody>
<tr>
<td align="right">1</td>
<td>Donna</td>
<td>Moore</td>
<td>[email protected]</td>
<td>China</td>
<td>211.56.242.221</td>
</tr>
<tr>
<td align="right">2</td>
<td>Janice</td>
<td>Henry</td>
<td>[email protected]</td>
<td>Ukraine</td>
<td>38.36.7.199</td>
</tr>
<tr>
<td align="right">3</td>
<td>Ruth</td>
<td>Wells</td>
<td>[email protected]</td>
<td>Trinidad and Tobago</td>
<td>19.162.133.184</td>
</tr>
<tr>
<td align="right">4</td>
<td>Jason</td>
<td>Ray</td>
<td>[email protected]</td>
<td>Brazil</td>
<td>10.68.11.42</td>
</tr>
<tr>
<td align="right">5</td>
<td>Jane</td>
<td>Stephens</td>
<td>[email protected]</td>
<td>United States</td>
<td>47.32.129.71</td>
</tr>
<tr>
<td align="right">6</td>
<td>Adam</td>
<td>Nichols</td>
<td>[email protected]</td>
<td>Canada</td>
<td>18.186.38.37</td>
</tr>
</tbody>
</table>
</body>
</html>
I am trying to bine the "TABLE FROM HTML" with a "Header"... Looked at the examples. I can get each one working separately, but not together.
When I bine the two, I am having problems...Can you see what I am doing wrong here and why this doesnt work ??
Might be in my header VAR... Also, it there a way to exclude a column from the export ???
Using this https://github./simonbengtsson/jsPDF-AutoTable
<html>
<body>
<button onclick="generate()">Download PDF</button>
<script src="jspdf/jspdf.min.js"></script>
<script src="jspdf/jspdf.plugin.autotable.src.js"></script>
<script>
function generate() {
var doc = new jsPDF('p', 'pt');
var res = doc.autoTableHtmlToJson(document.getElementById("basic-table"));
doc.autoTable(res.columns, res.data, options);
var header = function (data) {
doc.setFontSize(18);
doc.setTextColor(40);
doc.setFontStyle('normal');
doc.addImage(headerImgData, 'JPEG', data.settings.margin.left, 20, 50, 50);
doc.text("Testing Report", data.settings.margin.left + 55, 50);
};
var options = {
beforePageContent: header,
margin: {top: 80}
};
doc.autoTable(columns, data, options);
doc.save("table.pdf");
}
var headerImgData = 'data:image/jpeg;base64,/9... my dataimage';
</script>
<br/><br/>
<table id="basic-table">
<thead>
<tr>
<th title="Field #1">ID</th>
<th title="Field #2">First name</th>
<th title="Field #3">Last name</th>
<th title="Field #4">Email</th>
<th title="Field #5">Country</th>
<th title="Field #6">IP-address</th>
</tr>
</thead>
<tbody>
<tr>
<td align="right">1</td>
<td>Donna</td>
<td>Moore</td>
<td>[email protected]</td>
<td>China</td>
<td>211.56.242.221</td>
</tr>
<tr>
<td align="right">2</td>
<td>Janice</td>
<td>Henry</td>
<td>[email protected]</td>
<td>Ukraine</td>
<td>38.36.7.199</td>
</tr>
<tr>
<td align="right">3</td>
<td>Ruth</td>
<td>Wells</td>
<td>[email protected]</td>
<td>Trinidad and Tobago</td>
<td>19.162.133.184</td>
</tr>
<tr>
<td align="right">4</td>
<td>Jason</td>
<td>Ray</td>
<td>[email protected]</td>
<td>Brazil</td>
<td>10.68.11.42</td>
</tr>
<tr>
<td align="right">5</td>
<td>Jane</td>
<td>Stephens</td>
<td>[email protected]</td>
<td>United States</td>
<td>47.32.129.71</td>
</tr>
<tr>
<td align="right">6</td>
<td>Adam</td>
<td>Nichols</td>
<td>[email protected]</td>
<td>Canada</td>
<td>18.186.38.37</td>
</tr>
</tbody>
</table>
</body>
</html>
Share
Improve this question
edited Nov 17, 2015 at 14:13
Ronald
asked Nov 16, 2015 at 19:57
RonaldRonald
5572 gold badges10 silver badges28 bronze badges
3 Answers
Reset to default 3I'm not entirely sure what you want to acplish, but I made a best guess. Here is a codepen with the result. I changed your generate function to this:
function generate() {
var doc = new jsPDF('p', 'pt');
var res = doc.autoTableHtmlToJson(document.getElementById("basic-table"));
doc.autoTable(res.columns, res.data, {margin: {top: 80}});
var header = function(data) {
doc.setFontSize(18);
doc.setTextColor(40);
doc.setFontStyle('normal');
//doc.addImage(headerImgData, 'JPEG', data.settings.margin.left, 20, 50, 50);
doc.text("Testing Report", data.settings.margin.left, 50);
};
var options = {
beforePageContent: header,
startY: doc.autoTableEndPosY() + 20
};
doc.autoTable(res.columns, res.data, options);
doc.save("table.pdf");
}
Here is the way i exported my Table in PDF using jspdf(). I mentioned my array that would display in UI in the body part. Also mentioned the headers.
code -
<script src="jspdf.min.js"></script>
<script src="jspdf.plugin.autotable.min.js"></script>
import jsPDF from 'jspdf';
import 'jspdf-autotable';
capture(){
var doc = new jspdf('l', 'pt' , 'a4'); //landscape page
doc.autoTable({
body: this.responseData,
columns: [{header: 'version', dataKey: 'version'}, {header:
'sourceFileName',
dataKey: 'sourceFileName'},
{header: 'targetFileName', dataKey: 'targetFileName'}, {header: 'id',
dataKey: 'id'}
]
]
})
doc.save("table.pdf");
}
P.S : https://www.npmjs./package/jspdf-autotable
var jsPDF = require('jspdf');
require('jspdf-autotable');
use this to import
本文标签: javascriptProblems with JSPDF and AutoTableStack Overflow
版权声明:本文标题:javascript - Problems with JSPDF and AutoTable - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742211641a2433822.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论