Search code examples
javascriptjspdftabulatorjspdf-autotable

Tabulator combine multiple tables in a single pdf


I am using http://tabulator.info/ to generate multiple tables on a single HTML document page.

When I am triggering the pdf-download via the button, a pdf document should get generated that includes the tables from the document. So far, downloading a single table works, but I don't know how I could potentially add more tables to the document before jsPDF finishes.

What I have tried out so far is that I grabbed the lastTable1 = doc.lastAutoTable object inside the documentProcessing function when triggering a download for table1. The plan is to pass it into table2.download() and add it via autoTable: function(doc){doc.autoTable(lastTable1)}.

While I do grab an object with this approach, I can not use it to reconstruct the autotable object (e.g. doc.autoTable(lastTable1) does not produce the same table again).

I have prepared a simple jsfiddle where I generate two tables and a download button. Just to illustrate that the reconstruction of the autotable object does not work, I have added it once more to the doc before creating the pdf.


Solution

  • There is no built in way for tabulator to do this.

    But the good news is that it should be easy to add something to handle this yourself.

    This Issue shows how you can use jsPDF to merge to PDFs into the same document.

    You could use the downloadReady callback built into tabulator to intercept the PDF file created from each table and then combine them using the method outlined in the above issue

    var table = new Tabulator("#example-table", {
        downloadReady:function(fileContents, blob){
            //fileContents - the unencoded contents of the file
            //blob - the blob object for the download
    
            mergeWithPreviousPDFs(blob); // call a function based on the previous issue that merges the new PDF with the previous PDF's
    
            return blob; //must return a blob to proceed with the download, return false to abort download
        }
    });
    

    if you return false from this callback the download will not be triggered, so you can intercept the PDF output on all but the final table to prevent download and then return the combined output from the last table to trigger the download.