Search code examples
colorscellhighlightfilljspdf

How to highlight/fill a specific cell with colour in jsPDF - setFillColor not working with table generation (jspdf.plugin.cell.js)


I want to highlight/fill a specific cell with color in a table generated using jsPDF, which is output to PDF.

Open this JSFiddle. As a base case, it draws a single table cell and sets both the fill colour and text colour to black. Uncomment line 12 and run it, which causes a PDF containing a single table cell to be saved to your download location. Open it and look - the text colour is black, but the cell fill colour is white.

The important line is line 6 - no matter which RGB values I feed in, the cell colour doesn't change.

doc.setFillColor(0,0,0); 

How do I set a cell's fill colour? I've had a look through jspdf.plugin.cell.js, and there isn't anything overriding the fill colour. setFillColor is used in the function jsPDFAPI.printHeaderRow, which I followed as an example for my code.

Has anyone encountered this before?

Please let me know if I can provide any more details or explanation.


Solution

  • I haven't debugged it fully, but it works if I set doc.printingHeaderRow to true - http://jsfiddle.net/sp5qr4fd/4/

    doc.printingHeaderRow = true;