数据表以 100% 宽度导出 PDF [英] DataTables Export PDF with 100% Width
问题描述
我正在尝试将我的表格导出为 100% 宽度的 PDF.我尝试了以下方法,但没有成功
I am attempting to export my tables to PDF with a 100% width. I have tried the following but I have been unsuccessful
var settings={};
settings.buttons = [
{
extend:'pdfHtml5',
text:'Export PDF',
orientation:'landscape',
customize : function(doc){
doc.styles['table'] = { width:100% }
}
}
];
$('.dTable').dataTable(settings);
推荐答案
找到了一种更简单快捷的方法.
Found an easier and quicker way to do it.
{
extend: 'pdf',
customize: function (doc) {
doc.content[1].table.widths =
Array(doc.content[1].table.body[0].length + 1).join('*').split('');
}
}
这里发生的事情是这样的:
What happens here is as so:
doc.content[1].table.widths
是每列的宽度数组,如果每个都是一个 '*'
则表示表格将 100% 适合页面,列均匀分布.
doc.content[1].table.widths
is an array of widths for each column, and if each of them is a '*'
it means that the table will fit 100% of the page with the columns distributed evenly.
Array(doc.content[1].table.body[0].length + 1)
以我的表格所有列的长度创建一个数组.
Array(doc.content[1].table.body[0].length + 1)
creates an array in the length of all the columns of my table.
.join('*')
从数组中的所有单元格创建一个字符串,每个单元格都有一个 '*'
.
.join('*')
creates a string from all the cells in the array with a '*'
for each.
.split('');
将其拆分回数组.
希望我一路上帮助了某人.
Hope I helped someone along the way.
这篇关于数据表以 100% 宽度导出 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!