Exporting Datatable to PDF

Exporting Datatable to PDF

fobinatorfobinator Posts: 4Questions: 1Answers: 0

Trying to use the export feature on Datatables and I need it to render HTML. So I found this library https://www.npmjs.com/package/html-to-pdfmake which works with PDFMake. But when I'm trying to use it my exported data just shows [object Object].

exportOptions: {
            format: {
              body: function (data, row, column, node) {
                if(column === 1)
                {
                  console.log(JSON.stringify(htmlToPdfmake(data)[0]));
                }
                return column === 1 ? htmlToPdfmake(data)[0] : data;
              }
            }

I tried debugging here
http://pdfmake.org/playground.html

using the console output
{"text":"Hello world","nodeName":"B","bold":true,"style":["html-b"]},

And this seems to work in the playground but not in the datatables export. I either get the literal string or [object Object]

Answers

  • colincolin Posts: 12,116Questions: 0Answers: 2,060

    We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • fobinatorfobinator Posts: 4Questions: 1Answers: 0

    Hi Colin,

    Thank you for the response, I was in the middle of adding a test case but it took me a minute to find out how to get the buttons to show up on live.datatables

    http://live.datatables.net/radoruye/1/edit?html,js,console,output

    I want the Position column to output HTML but I'm not sure what's wrong. If I do something like

        var val = htmlToPdfmake("<i>ATAETATAES</i>");
        var dd = {content:val};
        pdfMake.createPdf(dd).download();
    

    it seems to work fine.

  • colincolin Posts: 12,116Questions: 0Answers: 2,060

    Thanks for the test case, I'm not too sure on that. This thread might - it's showing how to make links - so not the same but still HTML content. This might point in the right direction, if not, let us know,

    Colin

  • fobinatorfobinator Posts: 4Questions: 1Answers: 0

    Thank you! I think I got it working - fingers crossed for all use cases. I had to use the customize method in order to get this working

              customize: function (doc) {
                for (var row = 1; row < doc.content[1].table.body.length; row++) {
                    doc.content[1].table.body[row][1] = (htmlToPdfmake("<b>Strong</b>"));
                  }
    

    But additionally, in my case I still had to use exportOptions to get the data with the tags (not sure if there is a better way to do this, already set stripHtml flag to false)

              exportOptions: {
                format: {
                  body: function (data, row, column, node) {
                    return column === 1 ? data : data;
                    //return column === 1 ? htmlToPdfmake(data)[0] : data;
                  }
                }
              },
    

    Then I would just pass

                for (var row = 1; row < pdf.content[1].table.body.length; row++) {
                  console.log(pdf.content[1].table.body[row][1]);
                  pdf.content[1].table.body[row][1] = htmlToPdfmake(pdf.content[1].table.body[row][1].text);
                }
    

    in my customize method.

    Anyway, thank you for the help @Colin and I hope this helps others

  • fobinatorfobinator Posts: 4Questions: 1Answers: 0
Sign In or Register to comment.