我可以使用以下脚本从 html 表生成 PDF 文件: 但是我得到的所有列数据都是逐行的。
请帮助我以表格格式的方式生成 PDF 文件。(带有列边框、边距或填充、标题)在此脚本中
我使用 jsPDF lib 脚本生成 HTML 表格到 PDF。
var pdf = new jsPDF('p', 'pt', 'letter')
, source = $('#TableId')[0]
, specialElementHandlers = {
// element with id of "bypass" - jQuery style selector
'#bypassme': function(element, renderer){
return true
}
}
, margins = {
top: 20,
bottom: 20,
left: 30,
width: 922
};
pdf.fromHTML(
source // HTML string or DOM elem ref.
, margins.left // x coord
, margins.top // y coord
, {
'width': margins.width // max width of content on PDF
, 'elementHandlers': specialElementHandlers
},
function (dispose) {
pdf.save('Test.pdf');
},
margins
)
编辑:
我也尝试过下面的这个示例函数,但我得到的只是空的 pdf 文件。
function exportTabletoPdf()
{
var doc = new jsPDF('p','pt', 'a4', true);
var header = [1,2,3,4];
doc.table(10, 10, $('#test').get(0), header, {
left:10,
top:10,
bottom: 10,
width: 170,
autoSize:false,
printHeaders: true
});
doc.save('sample-file.pdf');
}
最佳答案
我花了很多时间寻找我的表格的良好表示,然后我找到了这个插件 ( https://github.com/simonbengtsson/jsPDF-AutoTable ),它工作得很好,包括主题、rowspan、colspan、从 html 中提取数据,与 json 一起工作,你还可以个性化您的标题并使其水平显示。 下图是一个例子:
关于jquery - 使用带有格式化表格数据的 jsPDF 创建 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23018171/