Search code examples
javascriptjspdfjspdf-autotable

Error while generating PDF out of HTML Table using jsPDF & autotable


I am using jsPDF & autotable to generate a PDF out of a Table in my DOM-Tree. My project is based on Wordpress and I registered and enqueued () both librarys via a CDN.

But when executing my pdf generation code, which is basically dummy code to test, it fails with the following error:

Uncaught TypeError: t.getDocument().getFillColor is not a function
    f https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/3.5.23/jspdf.plugin.autotable.min.js:10
    c https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/3.5.23/jspdf.plugin.autotable.min.js:10
    drawTable https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/3.5.23/jspdf.plugin.autotable.min.js:10
    drawTable https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/3.5.23/jspdf.plugin.autotable.min.js:10
    autoTable https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/3.5.23/jspdf.plugin.autotable.min.js:10

Since I couldn't find any questions like it and couldn't fix it myself, I wanted to share my problem.

const doc = new jsPDF({
            orientation: "landscape",
            unit: "mm",
            format: [300, 260]
        });

doc.text(25, 25, "Header");
doc.addPage();

doc.autoTable({ html: '#my-table' });
doc.save(pdfName);
<table id="my-table" class="table table-bordered table-custom">
     <thead id="test-thead">
         <tr>
            <td>Col 1</td>
            <td>Col 2</td>
         </tr>
     </thead>
     <tbody id="test-tbody">
         <tr>
            <td>Test 1</td>
            <td>Test 2</td>
        </tr>
     </tbody>
</table>

Solution

  • I've managed to get it working by calling an older version of autotable.js

        <script src="https://unpkg.com/[email protected]/dist/jspdf.plugin.autotable.js"></script>
    

    Seems to be in the latest version 3.5.23, it's missing a method for getFillColor.