5

I'm trying to work with DataTables Export feature, where I am able to export as CSV, xlxs, pdf. Now my current requirement is to export a custom pdf (change the font size, color, etc.). In the DataTable documentation it states, that we can integrate it with PDFmake, which I am unable to do so.

If anyone could please help in a way to integrate/use PDFmake with DataTables it would be really helpful.

Thanks in advance.

I'm initialising the DataTables

var table = $('#Table').DataTable( {
  lengthChange: true,
  buttons: [
    'copyHtml5',
    {
      extend: 'csvHtml5',
      title:  'FileName'
    },
    {
      extend: 'excelHtml5',
      title:  'FileName'
    },
    {
      extend: 'pdfHtml5',
      orientation: 'landscape',
      title:  'FileName',
      //download: 'open',
      pageSize: 'A3'
    }
  ]
});

I have all the necessary JS and CSS files required, how do I link the PDFMake in this?

3
  • Show us what you've done so far.. And what programming language is this? Please add more details. Commented Nov 6, 2015 at 8:03
  • Have you included all files as shown in the example datatables.net/extensions/buttons/examples/initialisation/… (the javascript tab) Commented Nov 6, 2015 at 18:58
  • Yes I have included all the required files, the buttons are working as it should, I just want to link it with PDFMake in order to customise the exported PDF.
    – Darshan
    Commented Nov 12, 2015 at 7:25

2 Answers 2

4

You can access the PdfMake object when you declare the DataTables and change the font like this:

window.pdfMake.fonts = {
        alef: {
            normal: 'Alef-Bold.ttf',
            bold: 'Alef-Bold.ttf',
            italics: 'Alef-Bold.ttf"',
            bolditalics: 'Alef-Bold.ttf',
        }
    };

This code assigns the custom font Alef to be used. A font that I assigned as the vfs.

(See https://github.com/bpampuch/pdfmake/wiki/Custom-Fonts---client-side if you are interested in how to make this)

For other customizations what you need is the customize option in the Button.

See here: https://datatables.net/reference/button/pdfHtml5

Here is an example of how to initialize the new font in the DataTable

 $("table").DataTable({
            buttons: [
                {
                    extend: 'pdf', className: 'btn green btn-outline',  text: 'Export PDF',
                    customize: function (doc) {
                        doc.defaultStyle = 
                            {
                                font: 'alef'
                            }

                          }
                       } 
                   ]
               });
2
0

Using the the 'dd' or 'document definition' that is shown in use on the pdfmake playground: http://pdfmake.org/playground.html In JS, create your own 'dd' and then when a button is clicked, simply have a similar line to this execute:

let pdf = pdfMake.createPdf(dd);

Then, with this, you can Open, Download...

pdf.Open();

I hope this helps.

Not the answer you're looking for? Browse other questions tagged or ask your own question.