我有同样的问题,这是我解决它的方式。
//inputs starts here
var T_X =20 //Starting X of Table
var T_Y =25 //Starting Y of Table
var T_W =170 //Width of Table
var T_H =10 //Height of each cell
var Px =7 // Scale of width in each columns
var Fs =17 //Font size
var Tp =7 //Y of all cells text, must change with `Fs`
var c = new Array() //Input Each Row by Adding new c[i]
c[0] = new Array("1","2","3","4","5","6","7");
c[1] = new Array("a","b","c","d","e","f","g");
c[2] = new Array("A","B","C","D","E","F","G");
//c[3] = new Array("Alpha","Beta","Gamma","Delta","Epsilon","Zeta","Eta");
//inputs ends here
//From This Part to bottom, there is no more inputs, no need to read them.
//i found this function here "http://stackoverflow.com/questions/118241/calculate-text-width-with-javascript"
String.prototype.width = function() {
var f = Px+'px arial',
o = $('<div>' + this + '</div>')
.css({'position': 'absolute', 'float': 'left', 'white-space': 'nowrap', 'visibility': 'hidden', 'font': f})
.appendTo($('body')),
w = o.width();
o.remove();
return w;
}
var doc = new jsPDF();
doc.text(85, 15, "Table example");
doc.setDrawColor(50,215,50);
doc.setLineWidth(0.5);
var i
var rows = c[0].length
var cols = c.length
var max = new Array()
for(i=0;i<cols;i++)
max[i]=0
doc.setFontSize(Fs);
for(i=0;i<rows;i+=1)
{
doc.line(T_X, T_Y+T_H*i,T_W + T_X, T_Y+T_H*i);
for(var j=0;j<cols;j+=1)
if(c[j][i].width()>max[j])
max[j]=c[j][i].width();
}
doc.line(T_X, T_Y+T_H*rows,T_W + T_X, T_Y+T_H*i);
var m = 0
for(i=0;i<cols;i+=1)
{
for(var j=0;j<rows;j+=1)
doc.text(T_X + 0.5 + m, Tp+T_Y+T_H*j,c[i][j]);
doc.line(T_X + m, T_Y ,T_X + m, T_Y + rows * T_H);
m += max[i];
}
doc.line(T_X + T_W, T_Y ,T_X + T_W, T_Y + rows * T_H);
希望对您有所帮助,但是为时已晚!