gpt4 book ai didi

jspdf - 如何使用 JsPDF-autoTable 在每个页面的顶部添加 Logo ?

转载 作者:行者123 更新时间:2023-12-02 00:13:06 28 4
gpt4 key购买 nike

JsPdf-autoTable是一款非常棒的软件,并且非常简单就能让基础功能发挥作用。

现在我想在每个页面的顶部添加一个 Logo 和一些文本,并在每个页面上添加一个 page X of Y 页脚,但我不知道如何操作。我想从 URL(我网站上的本地文件)获取 Logo 图像。

某处有一个简单的例子吗?我认为我是用 JsPdf 来做的,而不是 JsPdf-autoTable。

最佳答案

该方法接受一个参数:

doc.autoTable(options);

options,是一个对象,可以在文档 here 中查看其属性.

选项对象接受 hooks 。您正在寻找 didDrawPage Hook ,它将允许插入页眉和页脚。

didDrawPage: (HookData) => {} - Called after the plugin has finished drawing everything on a page. Can be used to add headers and footers with page numbers or any other content that you want on each page there is an autotable.

注意:didDrawPage Hook 在低于 v3 的版本中名为 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 在每个页面的顶部添加 Logo ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42913631/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com