JsPdf-autoTable是一款很棒的软件,非常简单,可以使基础知识正常运行。
现在,我想在每页顶部添加徽标和一些文本,并在每页上添加page X of Y
页脚,但是我不知道该怎么做。我想从URL(我网站上的本地文件)中获取徽标的图像。
那里有一个简单的例子吗?我认为我是使用JsPdf而不是JsPdf-autoTable来完成的。
最佳答案
该方法接受一个参数:
doc.autoTable(options);
options
是一个对象,您可以在文档here中检查其属性。此
options
对象接受hooks。您正在寻找didDrawPage
Hook ,它将允许插入页眉和页脚。注意:在低于v3的版本中,
didDrawPage
Hook 被命名为addPageContent
。demo中已经有一个有关如何执行此操作的示例。代码如下:
var doc = new jsPDF();
var totalPagesExp = "{total_pages_count_string}";
doc.autoTable({
head: headRows(),
body: bodyRows(40),
didDrawPage: function (data) {
// Header
doc.setFontSize(20);
doc.setTextColor(40);
doc.setFontStyle('normal');
if (base64Img) {
doc.addImage(base64Img, 'JPEG', data.settings.margin.left, 15, 10, 10);
}
doc.text("Report", data.settings.margin.left + 15, 22);
// Footer
var str = "Page " + doc.internal.getNumberOfPages()
// Total page number plugin only available in jspdf v1.0+
if (typeof doc.putTotalPages === 'function') {
str = str + " of " + totalPagesExp;
}
doc.setFontSize(10);
// jsPDF 1.4+ uses getWidth, <1.4 uses .width
var pageSize = doc.internal.pageSize;
var pageHeight = pageSize.height ? pageSize.height : pageSize.getHeight();
doc.text(str, data.settings.margin.left, pageHeight - 10);
},
margin: {top: 30}
});
// Total page number plugin only available in jspdf v1.0+
if (typeof doc.putTotalPages === 'function') {
doc.putTotalPages(totalPagesExp);
}
return doc;
base64Img
变量必须具有您的图像,我仅对base64图像进行了测试,假设您可以添加超链接,并且如果您在本地具有该图像,它将可以正常工作。代码段:
(function() {
function imgToBase64(url, callback) {
if (!window.FileReader) {
callback(null);
return;
}
var xhr = new XMLHttpRequest();
xhr.responseType = 'blob';
xhr.onload = function() {
var reader = new FileReader();
reader.onloadend = function() {
callback(reader.result.replace('text/xml', 'image/jpeg'));
};
reader.readAsDataURL(xhr.response);
};
xhr.open('GET', url);
xhr.send();
}
var base64Img;
// Convert the image to base64
// imgToBase64("https://placehold.it/50x50", function(base64) {
// base64Img = base64;
// });
// Static base64 for example purposes
base64Img = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyBAMAAADsEZWCAAAAG1BMVEXMzMyWlpaqqqq3t7exsbGcnJy+vr6jo6PFxcUFpPI/AAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAQUlEQVQ4jWNgGAWjgP6ASdncAEaiAhaGiACmFhCJLsMaIiDAEQEi0WXYEiMCOCJAJIY9KuYGTC0gknpuHwXDGwAA5fsIZw0iYWYAAAAASUVORK5CYII='
var headRows = function() {
return [{
id: "ID",
name: "Name",
}];
};
var bodyRows = function(rowCount) {
rowCount = rowCount || 10;
let body = [];
for (var i = 1; i <= rowCount; i++) {
body.push({
id: i,
name: "Name " + i
});
}
return body;
}
function generate() {
var doc = new jsPDF();
var totalPagesExp = "{total_pages_count_string}";
doc.autoTable({
head: headRows(),
body: bodyRows(40),
didDrawPage: function(data) {
// Header
doc.setFontSize(20);
doc.setTextColor(40);
doc.setFontStyle('normal');
if (base64Img) {
doc.addImage(base64Img, 'JPEG', data.settings.margin.left, 15, 10, 10);
}
doc.text("Example", data.settings.margin.left + 15, 22);
// Footer
var str = "Page " + doc.internal.getNumberOfPages()
// Total page number plugin only available in jspdf v1.0+
if (typeof doc.putTotalPages === 'function') {
str = str + " of " + totalPagesExp;
}
doc.setFontSize(10);
// jsPDF 1.4+ uses getWidth, <1.4 uses .width
var pageSize = doc.internal.pageSize;
var pageHeight = pageSize.height ? pageSize.height : pageSize.getHeight();
doc.text(str, data.settings.margin.left, pageHeight - 10);
},
margin: {
top: 30
}
});
// Total page number plugin only available in jspdf v1.0+
if (typeof doc.putTotalPages === 'function') {
doc.putTotalPages(totalPagesExp);
}
doc.save("headerandfooterexample.pdf");
}
document.getElementById('download-btn').addEventListener('click', generate);
})()
<script src="https://unpkg.com/jspdf@latest/dist/jspdf.min.js"></script>
<script src="https://unpkg.com/jspdf-autotable"></script>
<button id="download-btn">Download PDF</button>
关于jspdf - 如何使用JsPDF-autoTable在每页顶部添加徽标?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42913631/