Javascript 如何设置使用jsPDF生成pdf的列宽
在过去的两天里,我一直在为使用jsPDF生成PDF设置列宽 我能够从html表中使用jsPDF库生成pdf,但我遇到了列重叠的问题。因为我想在工作表中显示20列。我已将选项“纵向”更改为“横向”,并在导出脚本选项以及html表格宽度时设置宽度5000Javascript 如何设置使用jsPDF生成pdf的列宽,javascript,jquery,html,jspdf,Javascript,Jquery,Html,Jspdf,在过去的两天里,我一直在为使用jsPDF生成PDF设置列宽 我能够从html表中使用jsPDF库生成pdf,但我遇到了列重叠的问题。因为我想在工作表中显示20列。我已将选项“纵向”更改为“横向”,并在导出脚本选项以及html表格宽度时设置宽度5000 请帮助我从jsPDF设置pdf列宽。谢谢4天前我也遇到了同样的问题,并且能够解决。。我在下面提供了示例代码供您理解 我假设需要将html表(以下称为table1,包含3行或更多行)导出为PDF格式。我们将为这3+行设置单元格/列宽以及其他字体和字体
请帮助我从jsPDF设置pdf列宽。谢谢4天前我也遇到了同样的问题,并且能够解决。。我在下面提供了示例代码供您理解 我假设需要将html表(以下称为table1,包含3行或更多行)导出为PDF格式。我们将为这3+行设置单元格/列宽以及其他字体和字体类型 第一行将是标题-因此应用粗体。。我在第二行和第三行应用了不同的样式,以了解可用的样式。。如果您想为每一列应用不同的列宽,您也可以这样做 希望下面的代码是可读的和自解释的。如果你有任何问题,请告诉我
$(document).on("click", "#btnExportToPDF", function () {
var table1 =
tableToJson($('#table1').get(0)),
cellWidth = 35,
rowCount = 0,
cellContents,
leftMargin = 2,
topMargin = 12,
topMarginTable = 55,
headerRowHeight = 13,
rowHeight = 9,
l = {
orientation: 'l',
unit: 'mm',
format: 'a3',
compress: true,
fontSize: 8,
lineHeight: 1,
autoSize: false,
printHeaders: true
};
var doc = new jsPDF(l, '', '', '');
doc.setProperties({
title: 'Test PDF Document',
subject: 'This is the subject',
author: 'author',
keywords: 'generated, javascript, web 2.0, ajax',
creator: 'author'
});
doc.cellInitialize();
$.each(table1, function (i, row)
{
rowCount++;
$.each(row, function (j, cellContent) {
if (rowCount == 1) {
doc.margins = 1;
doc.setFont("helvetica");
doc.setFontType("bold");
doc.setFontSize(9);
doc.cell(leftMargin, topMargin, cellWidth, headerRowHeight, cellContent, i)
}
else if (rowCount == 2) {
doc.margins = 1;
doc.setFont("times ");
doc.setFontType("italic"); // or for normal font type use ------ doc.setFontType("normal");
doc.setFontSize(8);
doc.cell(leftMargin, topMargin, cellWidth, rowHeight, cellContent, i);
}
else {
doc.margins = 1;
doc.setFont("courier ");
doc.setFontType("bolditalic ");
doc.setFontSize(6.5);
doc.cell(leftMargin, topMargin, cellWidth, rowHeight, cellContent, i); // 1st=left margin 2nd parameter=top margin, 3rd=row cell width 4th=Row height
}
})
})
doc.save('sample Report.pdf'); })
function tableToJson(table) {
var data = [];
// first row needs to be headers
var headers = [];
for (var i=0; i<table.rows[0].cells.length; i++) {
headers[i] = table.rows[0].cells[i].innerHTML.toLowerCase().replace(/ /gi,'');
}
// go through cells
for (var i=1; i<table.rows.length; i++) {
var tableRow = table.rows[i];
var rowData = {};
for (var j=0; j<tableRow.cells.length; j++) {
rowData[ headers[j] ] = tableRow.cells[j].innerHTML;
}
data.push(rowData);
}
return data; }
$(文档)。在(“单击”上,“#btnExportToPDF”,函数(){
变量表1=
tableToJson($('#table1').get(0)),
单元宽度=35,
行计数=0,
细胞内容物,
leftMargin=2,
topMargin=12,
topMarginTable=55,
headerRowHeight=13,
行高=9,
l={
方向:“l”,
单位:毫米,
格式:“a3”,
是的,
字体大小:8,
线宽:1,
自动调整大小:false,
打印头:true
};
var doc=新的jsPDF(l,,'','');
doc.setProperties({
标题:“测试PDF文档”,
主题:“这就是主题”,
作者:'作者',
关键词:“生成的、javascript、web 2.0、ajax”,
创建者:“作者”
});
doc.cellInitialize();
$。每个(表1,函数(i,行)
{
行计数++;
$.each(行,函数(j,cellContent){
如果(行计数==1){
单据页边距=1;
setFont文件(“helvetica”);
文件setFontType(“粗体”);
文件setFontSize(9);
文档单元格(左边距、上边距、单元格宽度、标题宽度、单元格内容、i)
}
else if(rowCount==2){
单据页边距=1;
setFont文件(“时代”);
doc.setFontType(“italic”);//或用于普通字体类型------doc.setFontType(“normal”);
文件设置字体大小(8);
文档单元格(左边距、上边距、单元格宽度、行高、单元格内容、i);
}
否则{
单据页边距=1;
setFont文件(“信使”);
文件setFontType(“粗体斜体”);
文件setFontSize(6.5);
doc.cell(leftMargin,topMargin,cellWidth,rowHeight,cellContent,i);//1st=左页边距2nd参数=上页边距3rd=行单元格宽度4th=行高
}
})
})
doc.save('sample Report.pdf');})
函数tableToJson(表){
var数据=[];
//第一行必须是标题
var头=[];
对于(var i=0;iYou是受欢迎的gco。我很高兴它帮助了您:)并感谢您的支持voting@Vinu什么版本的jsPDF将允许所有这些选项?我使用的版本只允许以下函数jsPDF(方向、单位、格式、压缩PDF){/**String-orientation,String-unit,String-format,Boolean compressed*/
@RyanGill,我使用的是一些旧版本,从我的项目中使用的JSPdf文件中找不到确切的版本!!但函数与您提到的函数JSPdf(orientation,unit,format,compressPdf)相同{}@Vinu Hi!单击按钮时如何运行该命令?未捕获的TypeError:无法读取未定义-->函数tableToJson(table)-->for(var I=0;I)的属性“0”