Home > Mobile >  How to set the PDF table to full width in datatable
How to set the PDF table to full width in datatable

Time:12-13

Thanks for the help in advance. I am exporting a pdf in datatable, the exporting works fine but the table which contains the data inside pdf doesn't takes the full width.Can anyone help me to sort this

Screenshot of issueenter image description here

`/*What i tried*/
 "columnDefs": [
    { "width": "20%", "targets": 0 },
    { "width": "20%", "targets": 0 },
    { "width": "20%", "targets": 0 },
    { "width": "20%", "targets": 0 },
 ],`

codepen link : https://codepen.io/ANANTHUC/pen/oNyRwXW

CodePudding user response:

According to this discussion on datatables.net, you can add an array of table widths using * to mean auto-fit.

In your code, this would be:

$("#example").DataTable({ 
  ...
  "buttons": [{
    ...
    customize: function(doc) {
       ...
       doc.content[0].table.widths = ['*', '*', '*', '*', '*' ];

as you have 5 columns. This will then auto-fit those columns and force the page table to 100%.

You can also specify exact/percentage columns widths using the same configuration, eg in your case this appears to work quite well:

doc.content[0].table.widths = ['5%','*','10%','10%','10%'];

but will obviously depend on your preference.

Updated fiddle: https://jsfiddle.net/puzsh205/

  • Related