单元格文本颜色拒绝更改jsdpdf-autotable

时间:2018-12-10 08:27:43

标签: jspdf jspdf-autotable

我已经按照步骤完成了有关此主题的一些答案,但是没有一个起作用。我有以下文本需要显示为红色文本:

<table id="heading1">
<tr>
<td class="hrow"><h4>1. DETAILS</h4></td>
</tr>
</table>

这是我为PDF编写的代码:

<script>
 exportGraph = function () {
 var pdfsize = 'a4';
 var pdf = new jsPDF('l', 'mm', pdfsize);
 var totalPagesExp = "{total_pages_count_string}";

 var res2 = pdf.autoTableHtmlToJson(document.getElementById("heading1"));

 pdf.autoTable(res2.columns, res2.data, {
    createdCell: function(cell, data) {
        var tdElement = cell.raw;
        if (tdElement.classList.contains("hrow")) {
            cell.styles.textColor = "[255,72,72]";
        }
    },
startY: 10,
margin: {left: 5 },
styles: { halign: 'left', fontsize: 12 }
});


pdf.save('Submission-Printout.pdf'); 

}

</script>

如您所见,在理论上我应该做的工作,但文字仍显示为非红色。有谁知道为什么它没有以红色显示?

1 个答案:

答案 0 :(得分:1)

这个例子怎么样?

function generate() {
  var doc = new jsPDF('p', 'pt', 'a4');

  var elem = document.getElementById('example');
  var data = doc.autoTableHtmlToJson(elem);
  doc.autoTable(data.columns, data.rows, {
     createdCell: function (cell, data) {
      if ($(cell.raw).hasClass("demo1")) {
        cell.styles.textColor = [200, 0, 0];
                    cell.styles.fontStyle = 'bolditalic';
            };
      if ($(cell.raw).hasClass("demo2")) {
        cell.styles.textColor = [0, 0, 205];
                    cell.styles.fontStyle = 'bold';
            };
       return false;
     }
}); 
 doc.save("table.pdf");
}