1
votes

I don't get how I use the themes for jsPDF-AutoTable. . .

This is my Code to generate the PDF:

function tbl1ToPDF(){
            var table = tableToJson($('#tbl1').get(0));
            var doc = new jsPDF('l','pt','letter',true);
            $.each(table, function(i, row){
                $.each(row, function(j,cell){
                    if(i == 0)
                    {
                        doc.cell(10,10,150,50,cell,i, 'center');
                    }
                    else
                    {
                    doc.cell(10,10,150,120,cell,i,'center');
                    }
                });
            });
            doc.save('Sofort.pdf');
        }

And this is my tableToJson function:

function tableToJson(table) {
        var data = [];
        var headers = [];
        for (var i = 0; i < table.rows[0].cells.length; i++) {
            headers[i] = table.rows[0].cells[i].innerHTML.toLowerCase().replace(/ /gi, '');
        }
        data.push(headers);
        // go through cells
        for (var i = 1; i < table.rows.length; i++) {
            var tableRow = table.rows[i];
            var rowData = {};
            for (var j = 0; j < tableRow.cells.length; j++) {
                rowData[headers[j]] = tableRow.cells[j].innerHTML;
            }
            data.push(rowData);
        }
        return data;
    }

My Table is dynamic. I generate it after pressing a Button but the struct of the Table looks like this:

<h3>Header</h3>
<table id="tbl1">
  <thead>
    <tr>
      <th>Nr</th>
      <th>Name</th>
      <th>Unit</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>1</td>
      <td>test</td>
      <td>test</td>
    </tr>
    <tr>
      <td>2</td>
      <td>test</td>
      <td>test</td>
    </tr>
  </tbody>
</table>
<input type="button" onclick="tbl1ToPDF" value="download">

Can you please help me applying a theme to my PDF? I never did this before and I really need help!

Thanks you!

1
It doesn't seem like you are using only jspdf, not jspdf-autotable? Check the jspdf autotable readme if you want to use it's theme.Simon Bengtsson
Yeah! It was just an example. I read the readme but I did not understand what to do. But after many attemps I finally did it and it was easier then I thought...Nicolai

1 Answers

2
votes

In order to use jsPDF-Autotable plugin you need to call it inside the code like so:

var doc = new jsPDF();
doc.autoTable();

Once you called autoTable, you can apply some option to it like "theme" this way:

doc.autoTable({theme: 'grid'});

For example, I have an HTML table (3 columns) with id = "table-report-p2p" I get the data from it with autoTableHtmlToJson() and then I applied some options.

This is the code that works for me:

var elem = document.getElementById("table-report-p2p");
var res = doc.autoTableHtmlToJson(elem);
doc.autoTable(res.columns, res.data, {
    theme: 'grid',
    startY: 150,
    margin: {horizontal: 10},
    pageBreak: 'auto',
    rowPageBreak: 'avoid',
    columnStyles: {0: {cellWidth: 35, minCellHeight: 53},1: {cellWidth: 70},2: {cellWidth: 84}}
});