我可以使用下面的脚本从 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
)
编辑:
我也尝试过下面的函数sample,但我得到的只是空的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,您还可以个性化您的标题并使它们水平。 下图是一个示例:
尝试从此方法中删除最后一个参数“true”:
var doc = new jsPDF('p','pt', 'a4', true);
使用 jspdf 导出 html div 内容,包括纯文本和表格数据 包含脚本https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js
function download_DIVPdf(divid) {
var pdf = new jsPDF('p', 'pt', 'letter');
var pdf_name = 'PostMode-'+om+'.pdf';
// source can be HTML-formatted string, or a reference
// to an actual DOM element from which the text will be scraped.
htmlsource = $('#'+divid)[0];
specialElementHandlers = {
// element with id of "bypass" - jQuery style selector
'#bypassme': function (element, renderer) {
// true = "handled elsewhere, bypass text extraction"
return true
}
};
margins = {
top: 80,
bottom: 60,
left: 40,
width: 522
};
pdf.fromHTML(
htmlsource, // 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(pdf_name);
}, margins);
}
$(".gridview td").each(function () {
var value = $(this).html();
doc.setFontSize(8);
if (count == 1) {
if (height > 278) {
doc.rect(10, inc, 24, 8);
doc.rect(34, inc, 111, 8);
doc.rect(145, inc, 15, 8);
doc.rect(160, inc, 20, 8);
doc.rect(180, inc, 23, 8);
doc.addPage(focus);
doc.setLineWidth(0.5);
inc = 15;
height = 18;
}
doc.rect(10, inc, 24, 8);
doc.text(value, 11, height);
}
if (count == 2) {
doc.rect(34, inc, 111, 8);
var splitdesc = doc.splitTextToSize(value, 100);
doc.text(splitdesc, 35, height);
}
if (count == 3) {
doc.rect(145, inc, 15, 8);
doc.text(value, 147, height);
qty = value;
}
if (count == 4) {
doc.rect(160, inc, 20, 8);
doc.text(value, 163, height);
amt = value;
}
if (count == 5) {
doc.rect(180, inc, 23, 8);
tot = parseInt(qty) * parseFloat(amt);
doc.text("" + tot, 182, height);
count = 0;
height = height + 8;
netamt = netamt + parseFloat(tot);
inc = parseInt(inc) + 8;
doc.rect(10, inc, 24, 8);
doc.rect(34, inc, 111, 8);
doc.rect(145, inc, 15, 8);
doc.rect(160, inc, 20, 8);
doc.rect(180, inc, 23, 8);
}
count = count + 1;
});
也许是因为你的标题是一个数字数组,而不是一个字符串数组。尝试这个基本的例子,效果很好:
var doc = new jsPDF();
var header = ['1', '2', '3', '4'];
var data = [{1: '1', 2: '2', 3: '3', 4: '4'}];
var config = {
autoSize : false,
printHeaders : true
}
doc.table(10, 10, data, header, config);
doc.output('dataurlnewwindow');