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.
<div class="container" id="exTable"> <div class=" mt-3"> <h2>Basic Table Example1</h2> <table class="table" id="example1" style="width:100%"> <thead class="table-dark"> <tr> <th>User Priority</th> <th>Average MTTR(Hrs)</th> <th>Total Number Of Tickets</th> </tr> </thead> <tbody> <tr> <td>1-Critical</td> <td>164.12</td> <td>182</td> </tr> <tr> <td>2-High</td> <td>190.23</td> <td>498</td> </tr> <tr> <td>3-Moderate</td> <td>908.93</td> <td>1511</td> </tr> <tr> <td>4-Low</td> <td>211.43</td> <td>1817</td> </tr> </tbody> </table> </div> <div class="table-responsive mt-3"> <h2>Basic Table Example2</h2> <table class="table display nowrap" id="example2"> <thead class="table-dark"> <tr> <th>Tower</th> <th>CC</th> <th>Cluster</th> <th>Assignment Group</th> <th>Ticket Priority</th> <th>Average MTTR(Hrs)</th> <th>Total Number Of Tickets</th> <th>Incidents resolved Using KO's</th> </tr> </thead> <tbody> <tr> <td>MANUFACTURING</td> <td>RICHMOND GROUPS</td> <td>Workbrain - Infor HCM Version 6.1.2 - Enterprise Workforce Management</td> <td>SN PM USA Workbrain WFM and ETM</td> <td>1 - Critical</td> <td>4.89</td> <td>5</td> <td>0</td> </tr> <tr> <td>MANUFACTURING</td> <td>RICHMOND GROUPS</td> <td>Workbrain - Infor HCM Version 6.1.2 - Enterprise Workforce Management</td> <td>SN PM USA Workbrain WFM and ETM</td> <td>1 - Critical</td> <td>4.89</td> <td>5</td> <td>0</td> </tr> <tr> <td>MANUFACTURING</td> <td>RICHMOND GROUPS</td> <td>Workbrain - Infor HCM Version 6.1.2 - Enterprise Workforce Management</td> <td>SN PM USA Workbrain WFM and ETM</td> <td>1 - Critical</td> <td>4.89</td> <td>5</td> <td>0</td> </tr> <tr> <td>MANUFACTURING</td> <td>RICHMOND GROUPS</td> <td>Workbrain - Infor HCM Version 6.1.2 - Enterprise Workforce Management</td> <td>SN PM USA Workbrain WFM and ETM</td> <td>1 - Critical</td> <td>4.89</td> <td>5</td> <td>0</td> </tr> <tr> <td>MANUFACTURING</td> <td>RICHMOND GROUPS</td> <td>Workbrain - Infor HCM Version 6.1.2 - Enterprise Workforce Management</td> <td>SN PM USA Workbrain WFM and ETM</td> <td>1 - Critical</td> <td>4.89</td> <td>5</td> <td>0</td> </tr> <tr> <td>MANUFACTURING</td> <td>RICHMOND GROUPS</td> <td>Workbrain - Infor HCM Version 6.1.2 - Enterprise Workforce Management</td> <td>SN PM USA Workbrain WFM and ETM</td> <td>1 - Critical</td> <td>4.89</td> <td>5</td> <td>0</td> </tr> <tr> <td>MANUFACTURING</td> <td>RICHMOND GROUPS</td> <td>Workbrain - Infor HCM Version 6.1.2 - Enterprise Workforce Management</td> <td>SN PM USA Workbrain WFM and ETM</td> <td>1 - Critical</td> <td>4.89</td> <td>5</td> <td>0</td> </tr> <tr> <td>MANUFACTURING</td> <td>RICHMOND GROUPS</td> <td>Workbrain - Infor HCM Version 6.1.2 - Enterprise Workforce Management</td> <td>SN PM USA Workbrain WFM and ETM</td> <td>1 - Critical</td> <td>4.89</td> <td>5</td> <td>0</td> </tr> <tr> <td>MANUFACTURING</td> <td>RICHMOND GROUPS</td> <td>Workbrain - Infor HCM Version 6.1.2 - Enterprise Workforce Management</td> <td>SN PM USA Workbrain WFM and ETM</td> <td>1 - Critical</td> <td>4.89</td> <td>5</td> <td>0</td> </tr> <tr> <td>MANUFACTURING</td> <td>RICHMOND GROUPS</td> <td>Workbrain - Infor HCM Version 6.1.2 - Enterprise Workforce Management</td> <td>SN PM USA Workbrain WFM and ETM</td> <td>1 - Critical</td> <td>4.89</td> <td>5</td> <td>0</td> </tr> <tr> <td>MANUFACTURING</td> <td>RICHMOND GROUPS</td> <td>Workbrain - Infor HCM Version 6.1.2 - Enterprise Workforce Management</td> <td>SN PM USA Workbrain WFM and ETM</td> <td>1 - Critical</td> <td>4.89</td> <td>5</td> <td>0</td> </tr> <tr> <td>MANUFACTURING</td> <td>RICHMOND GROUPS</td> <td>Workbrain - Infor HCM Version 6.1.2 - Enterprise Workforce Management</td> <td>SN PM USA Workbrain WFM and ETM</td> <td>1 - Critical</td> <td>4.89</td> <td>5</td> <td>0</td> </tr> <tr> <td>MANUFACTURING</td> <td>RICHMOND GROUPS</td> <td>Workbrain - Infor HCM Version 6.1.2 - Enterprise Workforce Management</td> <td>SN PM USA Workbrain WFM and ETM</td> <td>1 - Critical</td> <td>4.89</td> <td>5</td> <td>0</td> </tr> <tr> <td>MANUFACTURING</td> <td>RICHMOND GROUPS</td> <td>Workbrain - Infor HCM Version 6.1.2 - Enterprise Workforce Management</td> <td>SN PM USA Workbrain WFM and ETM</td> <td>1 - Critical</td> <td>4.89</td> <td>5</td> <td>0</td> </tr> <tr> <td>MANUFACTURING</td> <td>RICHMOND GROUPS</td> <td>Workbrain - Infor HCM Version 6.1.2 - Enterprise Workforce Management</td> <td>SN PM USA Workbrain WFM and ETM</td> <td>1 - Critical</td> <td>4.89</td> <td>5</td> <td>0</td> </tr> </tbody> </table> </div> </div>
CSS
table{ table-layout:fixed; } td,th{ font-size:10px;overflow:hidden; }
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.
$(document).ready(function () { $('#example1').DataTable({ dom: 'Bfrtip', buttons: [ 'pdfHtml5' ] }); }); $(document).ready(function () { $('#example2').DataTable({ scrollX: true, pageLength : 5, lengthMenu: [[5, 10, 20, -1], [5, 10, 20, 'Todos']], dom: 'Bfrtip', buttons: [ 'pdfHtml5' ] }); });
Advertisement
Answer
Here is an approach which creates one PDF containing the data from both DataTables in the web page:
- The first data table is created with no button:
$('#example1').DataTable();
- 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:
var table1 = $('#example1').DataTable();
We build arrays for the
headings
row and thedata
rows. This gives us the raw data for that smaller table.We convert this raw data into the format needed to build a PDF table and store it in a
tbl1_rows
array:
var tbl1_rows = []; // the data from the first table
- 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:
var clone = structuredClone(doc.content[1]); clone.table.body = tbl1_rows; doc.content.splice(1, 0, clone);
The end result:
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:
$(document).ready(function() { $('#example1').DataTable(); $('#example2').DataTable({ dom: 'Brftip', buttons: [ { text: 'PDF Export', extend: 'pdfHtml5', customize: function ( doc ) { var table1 = $('#example1').DataTable(); var headings = table1.columns().header().to$().map(function(i,e) { return e.innerHTML;}).get();; var data = table1.rows().data(); var tbl1_rows = []; // the data from the first table // PDF header row for the first table: tbl1_rows.push( $.map( headings, function ( d ) { return { text: typeof d === 'string' ? d : d+'', style: 'tableHeader' }; } ) ); // PDF body rows for the first table: for ( var i = 0, ien = data.length ; i < ien ; i++ ) { tbl1_rows.push( $.map( data[i], function ( d ) { if ( d === null || d === undefined ) { d = ''; } return { text: typeof d === 'string' ? d : d+'', style: i % 2 ? 'tableBodyEven' : 'tableBodyOdd' }; } ) ); } // create a clone of the main PDF table (where // content[0] is the PDF's main heading, and content[1] // is the main table: var clone = structuredClone(doc.content[1]); //replace the main table data with the tbl1_rows data: clone.table.body = tbl1_rows; // insert that cloned table into the main PDF doc, between // the PDF title and the main table: doc.content.splice(1, 0, clone); } } ] }); } );
<!doctype html> <html> <head> <meta charset="UTF-8"> <title>Demo</title> <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.12.1/css/jquery.dataTables.css"/> <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/buttons/2.2.3/css/buttons.dataTables.css"/> <script type="text/javascript" src="https://code.jquery.com/jquery-3.6.0.js"></script> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jszip/2.5.0/jszip.js"></script> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/pdfmake.js"></script> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/vfs_fonts.js"></script> <script type="text/javascript" src="https://cdn.datatables.net/1.12.1/js/jquery.dataTables.js"></script> <script type="text/javascript" src="https://cdn.datatables.net/buttons/2.2.3/js/dataTables.buttons.js"></script> <script type="text/javascript" src="https://cdn.datatables.net/buttons/2.2.3/js/buttons.html5.js"></script> <link rel="stylesheet" type="text/css" href="https://datatables.net/media/css/site-examples.css"> </head> <body> <div style="margin: 20px;"> <div class="container" id="exTable"> <div class=" mt-3"> <h2>Basic Table Example1</h2> <table class="table" id="example1" style="width:100%"> <thead class="table-dark"> <tr> <th>User Priority</th> <th>Average MTTR(Hrs)</th> <th>Total Number Of Tickets</th> </tr> </thead> <tbody> <tr> <td>1-Critical</td> <td>164.12</td> <td>182</td> </tr> <tr> <td>2-High</td> <td>190.23</td> <td>498</td> </tr> <tr> <td>3-Moderate</td> <td>908.93</td> <td>1511</td> </tr> <tr> <td>4-Low</td> <td>211.43</td> <td>1817</td> </tr> </tbody> </table> </div> <div class="table-responsive mt-3"> <h2>Basic Table Example2</h2> <table class="table display nowrap" id="example2"> <thead class="table-dark"> <tr> <th>Tower</th> <th>CC</th> <th>Cluster</th> <th>Assignment Group</th> <th>Ticket Priority</th> <th>Average MTTR(Hrs)</th> <th>Total Number Of Tickets</th> <th>Incidents resolved Using KO's</th> </tr> </thead> <tbody> <tr> <td>MANUFACTURING 01</td> <td>RICHMOND GROUPS</td> <td>Workbrain - Infor HCM Version 6.1.2 - Enterprise Workforce Management</td> <td>SN PM USA Workbrain WFM and ETM</td> <td>1 - Critical</td> <td>4.89</td> <td>5</td> <td>0</td> </tr> <tr> <td>MANUFACTURING 02</td> <td>RICHMOND GROUPS</td> <td>Workbrain - Infor HCM Version 6.1.2 - Enterprise Workforce Management</td> <td>SN PM USA Workbrain WFM and ETM</td> <td>1 - Critical</td> <td>4.89</td> <td>5</td> <td>0</td> </tr> <tr> <td>MANUFACTURING 03</td> <td>RICHMOND GROUPS</td> <td>Workbrain - Infor HCM Version 6.1.2 - Enterprise Workforce Management</td> <td>SN PM USA Workbrain WFM and ETM</td> <td>1 - Critical</td> <td>4.89</td> <td>5</td> <td>0</td> </tr> <tr> <td>MANUFACTURING 04</td> <td>RICHMOND GROUPS</td> <td>Workbrain - Infor HCM Version 6.1.2 - Enterprise Workforce Management</td> <td>SN PM USA Workbrain WFM and ETM</td> <td>1 - Critical</td> <td>4.89</td> <td>5</td> <td>0</td> </tr> <tr> <td>MANUFACTURING 05</td> <td>RICHMOND GROUPS</td> <td>Workbrain - Infor HCM Version 6.1.2 - Enterprise Workforce Management</td> <td>SN PM USA Workbrain WFM and ETM</td> <td>1 - Critical</td> <td>4.89</td> <td>5</td> <td>0</td> </tr> <tr> <td>MANUFACTURING 06</td> <td>RICHMOND GROUPS</td> <td>Workbrain - Infor HCM Version 6.1.2 - Enterprise Workforce Management</td> <td>SN PM USA Workbrain WFM and ETM</td> <td>1 - Critical</td> <td>4.89</td> <td>5</td> <td>0</td> </tr> <tr> <td>MANUFACTURING 07</td> <td>RICHMOND GROUPS</td> <td>Workbrain - Infor HCM Version 6.1.2 - Enterprise Workforce Management</td> <td>SN PM USA Workbrain WFM and ETM</td> <td>1 - Critical</td> <td>4.89</td> <td>5</td> <td>0</td> </tr> <tr> <td>MANUFACTURING 08</td> <td>RICHMOND GROUPS</td> <td>Workbrain - Infor HCM Version 6.1.2 - Enterprise Workforce Management</td> <td>SN PM USA Workbrain WFM and ETM</td> <td>1 - Critical</td> <td>4.89</td> <td>5</td> <td>0</td> </tr> <tr> <td>MANUFACTURING 09</td> <td>RICHMOND GROUPS</td> <td>Workbrain - Infor HCM Version 6.1.2 - Enterprise Workforce Management</td> <td>SN PM USA Workbrain WFM and ETM</td> <td>1 - Critical</td> <td>4.89</td> <td>5</td> <td>0</td> </tr> <tr> <td>MANUFACTURING 10</td> <td>RICHMOND GROUPS</td> <td>Workbrain - Infor HCM Version 6.1.2 - Enterprise Workforce Management</td> <td>SN PM USA Workbrain WFM and ETM</td> <td>1 - Critical</td> <td>4.89</td> <td>5</td> <td>0</td> </tr> <tr> <td>MANUFACTURING 11</td> <td>RICHMOND GROUPS</td> <td>Workbrain - Infor HCM Version 6.1.2 - Enterprise Workforce Management</td> <td>SN PM USA Workbrain WFM and ETM</td> <td>1 - Critical</td> <td>4.89</td> <td>5</td> <td>0</td> </tr> <tr> <td>MANUFACTURING 12</td> <td>RICHMOND GROUPS</td> <td>Workbrain - Infor HCM Version 6.1.2 - Enterprise Workforce Management</td> <td>SN PM USA Workbrain WFM and ETM</td> <td>1 - Critical</td> <td>4.89</td> <td>5</td> <td>0</td> </tr> <tr> <td>MANUFACTURING 13</td> <td>RICHMOND GROUPS</td> <td>Workbrain - Infor HCM Version 6.1.2 - Enterprise Workforce Management</td> <td>SN PM USA Workbrain WFM and ETM</td> <td>1 - Critical</td> <td>4.89</td> <td>5</td> <td>0</td> </tr> <tr> <td>MANUFACTURING 14</td> <td>RICHMOND GROUPS</td> <td>Workbrain - Infor HCM Version 6.1.2 - Enterprise Workforce Management</td> <td>SN PM USA Workbrain WFM and ETM</td> <td>1 - Critical</td> <td>4.89</td> <td>5</td> <td>0</td> </tr> <tr> <td>MANUFACTURING 15</td> <td>RICHMOND GROUPS</td> <td>Workbrain - Infor HCM Version 6.1.2 - Enterprise Workforce Management</td> <td>SN PM USA Workbrain WFM and ETM</td> <td>1 - Critical</td> <td>4.89</td> <td>5</td> <td>0</td> </tr> </tbody> </table> </div> </div> </body> </html>
Acknowledgements
I took some of the above approach from the official DataTables source code for PDF exports:
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:
clone.margin = [ 0, 0, 0, 10 ];
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?