Skip to content
Advertisement

Can I export two dataTables into a single pdf with pdfmake

HTML I want to export the below two dataTables into a single pdf with single export button. Both the dataTables have different columns so it cannot be merged.

JavaScript

CSS

JavaScript

Java Script I want make changes in this dataTable jQuery functions in such a way that if I click on single button both the dataTables data should be exported in same pdf file.

JavaScript

Advertisement

Answer

Here is an approach which creates one PDF containing the data from both DataTables in the web page:

  1. The first data table is created with no button:
JavaScript
  1. The second table uses the customize: function ( doc ) { ... } syntax – the same as I linked to in my comments.

Within that function, we access the data for the first table:

JavaScript
  1. We build arrays for the headings row and the data rows. This gives us the raw data for that smaller table.

  2. We convert this raw data into the format needed to build a PDF table and store it in a tbl1_rows array:

JavaScript
  1. Finally we clone the PDF table data from the main table, then replace its data with our tbl1_rows data – and then we insert this new table object into the PDF:
JavaScript

The end result:

enter image description here

So, basically, the approach is to just create one PDF for the main table – and splice in the extra data for that additional table, that you also want to display in the same PDF.

Here is the full code for reference:

JavaScript
JavaScript

Acknowledgements

I took some of the above approach from the official DataTables source code for PDF exports:

buttons.html5.js


Update

For future visitors to this question:

If you want to further format the report, you can take a look at the PDFMake documentation and use their playground to try different things for yourself.

For example, if you want to add a space between the two tables, you can assign a bottom margin to the first table:

JavaScript

More examples: You can change the row striping and cell borders. See How to remove header in PDF export result from DataTable and change color table fill to white?

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement