javascript - 在pdf中使用jspdf.debug.js Wrap table head时

标签 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);

if fiddle 的相同代码 https://jsfiddle.net/neerajsonar/afas07Lf/

最佳答案

我正在使用 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. 然后我修改了函数:

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)并注释行 this.setFontStyle('bold');

关于javascript - 在pdf中使用jspdf.debug.js Wrap table head时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33730167/

相关文章:

javascript - 无法通过 ajax 将数据发布到 web2py Rest api - 可能存在 CORS 问题

JQuery Boxy 插件调整大小和访问

jquery - Bootstrap 弹出框边框 CSS

javascript - 使用 JsPDF 将 Canvas 转换为 Pdf

angular - 防止在jsPDF中将文本拆分为两页

javascript - 在哪里更改 jspdf.debug.js 中的默认 pdf 页面宽度和字体大小?

Javascript 验证表单检查文本区域长度

javascript - XMLHttpResponse 对象在每次调用 open() 时加载相同的内容

javascript - 在浏览器内检测鼠标

javascript - 如何测试一个元素是否具有特定的类?