javascript - jsPDF自动表格标题文本不起作用以及如何将html呈现到标题
问题描述
使用许多在线示例我试图将标题文本打印到表格的顶部。我不能做的一件事是更新 jsPDF 和自动表格版本。这是由于代码最终出现的一些内部冲突。问题 1 是为什么标题没有呈现到顶部。第二个问题是如何将 html 呈现到标题的顶部,而不仅仅是文本。目前 doc.text 仅将字符串作为参数。
我想在头部呈现类似:“主标题小标题”的内容
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.2.60/jspdf.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/2.0.15/jspdf.plugin.autotable.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/2.0.15/jspdf.plugin.autotable.src.js"></script>
</head>
<body>
<button onclick="generatePdf()">Generate pdf</button>
<script>
function generatePdf(){
var columns = [{
title: "ID",
dataKey: "id"
},
{
title: "Name",
dataKey: "name"
},
{
title: "Country",
dataKey: "country"
},
];
var rows = [{
"id": 1,
"name": "Shaw",
"country": "Tanzania"
},
{
"id": 2,
"name": "Nelson",
"country": "Kazakhstan"
},
{
"id": 3,
"name": "Garcia",
"country": "Madagascar"
},
];
var doc = new jsPDF('p', 'pt');
var headerTxt ="This is test header"
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(headerTxt, data.settings.margin.left, 50);
};
var options = {
beforePageContent: header,
margin: {
top: 80
},
startY: doc.autoTableEndPosY() + 20
};
doc.autoTable(columns, rows, options)
doc.save('table.pdf');
}
</script>
</body>
</html>
解决方案
不推荐使用 BeforePageContent .. 使用:
didDrawPage : header
作为替代。
推荐阅读
- python - 如何使用matplotlib检测是否有交集?
- wordpress - 如何尝试转换这个可能包含一些奇怪代码的 API 查询参数?
- flutter - 颤振过高的速率
- python - FileNotFoundError:[Errno 2] 没有这样的文件或目录:'aria2c'
- extjs - Sencha Cmd [ERR] no !/ in spec
- lua - Roblox Studio 价值问题
- cmake - 在cmake中获取可执行文件之间的依赖关系
- java - 您的 SQL 语法有错误 检查与您的 MariaDB 服务器版本相对应的手册以获取正确的语法使用
- oracle - SQL Developer 不提供 Oracle 10g 的输出
- javascript - 在 oidc-client 中调用 UserManager 类