使用jspdf.debug.js以pdf格式化表头

时间:2015-11-16 07:18:21

标签: javascript jquery jspdf

我使用jspdf.debug.js生成pdf格式的html表格。 表标题未正确包装,如下所示: image of the distorted heading jspdf.debug.js

下面是打印出thead的代码。虽然它没有在下一行中包装长文本

 jsPDFAPI.printHeaderRow = function (lineNumber, new_page) {

    if (!this.tableHeaderRow) {
        throw 'Property tableHeaderRow does not exist.';
    }

    var tableHeaderCell,
        tmpArray,
        i,
        ln;

    this.printingHeaderRow = true;
    if (headerFunction !== undefined) {
        var position = headerFunction(this, pages);
        setLastCellPosition(position[0], position[1], position[2], position[3], -1);
    }
    this.setFontStyle('bold');
    var tempHeaderConf = [];
    for (i = 0, ln = this.tableHeaderRow.length; i < ln; i += 1) {
        this.setFillColor(248,218,194);

        //this.maxWidth(10);
        //this.setWidth(10);
        // console.log("width"+this.width);
        /*changed neeraj color of table heading*/
        //this.setFillColor(200,200,200);


        tableHeaderCell = this.tableHeaderRow[i];
        if (new_page) {
            tableHeaderCell[1] = this.margins && this.margins.top || 0;
            tempHeaderConf.push(tableHeaderCell);
        }
        tmpArray = [].concat(tableHeaderCell);
        this.cell.apply(this, tmpArray.concat(lineNumber));
    }
    if (tempHeaderConf.length > 0){
        this.setTableHeaderRow(tempHeaderConf);
    }
    this.setFontStyle('normal');
    this.printingHeaderRow = false;
};
})(jsPDF.API);

相同的代码如果小提琴 https://jsfiddle.net/neerajsonar/afas07Lf/

1 个答案:

答案 0 :(得分:3)

我正在使用jsPDF版本1.0.272

jsPDFAPI.table = function (x,y, data, headers, config)(第2833行左右)

  1. if (printHeaders) {之后 我将true添加为calculateLineHeight()调用的最后一个参数: var lineHeight = this.calculateLineHeight(headerNames, columnWidths, headerPrompts.length?headerPrompts:headerNames,true);
  2. 接下来,在for循环中,我从最后一个参数中删除了String(): tableHeaderConfigs.push([x, y, columnWidths[header], lineHeight, (headerPrompts.length ? headerPrompts[i] : header)]);
  3. //Construct the data rows注释后的for循环中,我将false添加为calculateLineHeight()的最后一个参数: lineHeight = this.calculateLineHeight(headerNames, columnWidths, model,false);

  4. 我在calculateLineHeight函数中添加了另一个参数isHeader: jsPDFAPI.calculateLineHeight = function (headerNames, columnWidths, model, isHeader)

  5. 然后我修改了函数:

  6. jsPDFAPI.calculateLineHeight = function (headerNames, columnWidths, model, isHeader) { var header, lineHeight = 0; for (var j = 0; j < headerNames.length; j++) { header = headerNames[j]; if(isHeader){ model[j] = this.splitTextToSize(String(model[j]), columnWidths[model[j].toLowerCase().replace(/\s+/g, '')] - padding); var h = this.internal.getLineHeight() * model[j].length + padding; }else{ model[header] = this.splitTextToSize(String(model[header]), columnWidths[header] - padding); var h = this.internal.getLineHeight() * model[header].length + padding; } if (h > lineHeight) lineHeight = h; } return lineHeight; };

    如果您想要解开表格标题,请转到功能 jsPDFAPI.printHeaderRow = function (lineNumber, new_page)并对第{{​​1}}行

    进行评论