5
votes

Using DataTables and Buttons (NOT TableTools, which is retired) extension. Some cells have progressbars and small icons. Is there a way to export these images (or at least their titles) to PDF? Found some possible hacks on this page, but all of them were for retired TableTools.

Checked https://datatables.net/reference/button/pdf and https://datatables.net/reference/api/buttons.exportData%28%29 but couldn't find any method to achieve this goal. Tested by adding this code:

stripHtml: false

but whole HTML code (like img src=...) was included in PDF file instead of images.

If exporting images isn't possible, is there a way to export at least alt or title attribute of each image? That would be enough.

4

4 Answers

13
votes

I assume you are using pdfHtml5. dataTables is using pdfmake in order to export pdf files. When pdfmake is used from within a browser it needs images to be defined as base64 encoded dataurls.

Example : You have rendered a <img src="myglyph.png"> in the first column of some of the rows - those glyphs should be included in the PDF. First create an Image reference to the glyph :

var myGlyph = new Image();
myGlyph.src = 'myglyph.png';

In your customize function you must now

1) build a dictionary with all images that should be included in the PDF
2) replace text nodes with image nodes to reference images

buttons : [
    { 
    extend : 'pdfHtml5',
    customize: function(doc) {

        //ensure doc.images exists
        doc.images = doc.images || {};

        //build dictionary
        doc.images['myGlyph'] = getBase64Image(myGlyph);
        //..add more images[xyz]=anotherDataUrl here

        //when the content is <img src="myglyph.png">
        //remove the text node and insert an image node
        for (var i=1;i<doc.content[1].table.body.length;i++) {
            if (doc.content[1].table.body[i][0].text == '<img src="myglyph.png">') {
                delete doc.content[1].table.body[i][0].text;
                doc.content[1].table.body[i][0].image = 'myGlyph';
            }
        }
    },
    exportOptions : {
        stripHtml: false
    }
}

Here is a an example of a getBase64Image function

function getBase64Image(img) {
    var canvas = document.createElement("canvas");
    canvas.width = img.width;
    canvas.height = img.height;
    var ctx = canvas.getContext("2d");
    ctx.drawImage(img, 0, 0);
    return canvas.toDataURL("image/png");
}

If you just want to show the title of images in the PDF - or in any other way want to manipulate the text content of the PDF - then it is a little bit easier. The content of each column in each row can be formatted through the exportOptions.format.body callback :

buttons : [
    { 
    extend : 'pdfHtml5',
    exportOptions : {
        stripHtml: false
        format: {
            body: function(data, col, row) {
                var isImg = ~data.toLowerCase().indexOf('img') ? $(data).is('img') : false;
                if (isImg) {
                    return $(data).attr('title');
                }
                return data;
            }
        }
    }
]

The reason format.body cannot be used along with images is that is only let us pass data back to the text node part of the PDF document.

See also

6
votes

Since no suggestions received, I had to make a hack in order to get PDF file formatted the way I want.

In case someone has the same issue, you can use hidden span to display image alt/title near image itself. I'm sure it's not the best practice, but it will do the trick. So the code will look like:

<img src='image.png' alt='some_title'/><span class='hidden'>some_title</span>

This way datatables will show only the image, while PDF file will contain text you need.

1
votes

This is my CODE!

HTML

<div class="dt-btn"></div>
<table>
  <thead><tr><th>No</th><th>IMAGE</th><th>NOTE</th></tr></thead>
  <tbody>
    <tr>
      <td>{{$NO}}</td>
      <td>{{$imgSRC}}</td>
      <td>{{$NAME}}<br />
          {{$GRADE}}<br />
          {{$PROFILE}}<br />
          {{$CODE}}<br />
      </td>
    </tr>
  </tbody>
</table>

JAVASCRIPT

$.extend( true, $.fn.dataTable.defaults, {
buttons: [{
  text: '<i class="bx bx-download font-medium-1"></i><span class="align-middle ml-25">Download PDF</span>',
  className: 'btn btn-light-secondary mb-1 mx-1 dnPDF',
  extend: 'pdfHtml5',
  pageSize: 'A4',
  styles: {
   fullWidth: { fontSize: 11, bold: true, alignment: 'left', margin: [0,0,0,0] }
  },
  action: function ( e, dt, node, config ) {
    var that = this;
    setTimeout( function () {
      $.fn.dataTable.ext.buttons.pdfHtml5.action.call(that, e, dt, node, config);
      $( ".donePDF" ).remove();
      $( ".dnPDF" ).prop("disabled", false);
    }, 50);
  },
  customize: function(doc) {
    doc.defaultStyle.fontSize = 11;
    doc.defaultStyle.alignment = 'left';
    doc.content[1].table.dontBreakRows = true;
    if (doc) {
      for (var i = 1; i < doc.content[1].table.body.length; i++) {
        // 1st Column - display IMAGE
        var imgtext = doc.content[1].table.body[i][0].text;
        delete doc.content[1].table.body[i][0].text;
        jQuery.ajax({
          type: "GET",
          dataType: "json",
          url: "{{route('base64')}}",
          data: { src: imgtext },
          async: false,
          success: function(resp) {
            //console.log(resp.data);
            doc.content[1].table.body[i][0] = {
                margin: [0, 0, 0, 3],
                alignment: 'center',
                image: resp.data,
                width: 80,
                height: 136
            };
          }
        });
        // 2nd Column - display NOTE(4 line)
        var bodyhtml = doc.content[1].table.body[i][1].text;
        var bodytext = bodyhtml.split("\n");
        var bodystyle = []
        for (var j = 0; j < bodytext.length; j++) {
          switch(j) {
            case 0:
              // NAME
              var _text = { margin:[0, 0, 0, 3], color:"#000000", fillColor:'#ffffff', bold:true, fontSize:13, alignment:'center', text:bodytext[j] };
              break;
            case 1:
              // GRADE
              var _text = { margin:[0, 0, 0, 3], color:"blue", fillColor:'#ffffff', bold:false, fontSize:11, alignment:'left', text:bodytext[j] };
              break;
            case 3:
              // CODE
              var _text = { margin:[0, 0, 0, 3], color:"#000000", fillColor:'#ffffff', bold:true, fontSize:11, alignment:'left', text:bodytext[j] };
              break;
            default:
              // OTHERS
              var _text = { margin:[0, 0, 0, 3], color:"#000000", fillColor:'#ffffff', bold:false, fontSize:11, alignment:'left', text:bodytext[j] };
              break;
          }
          bodystyle[j] = _text;
        };
        doc.content[1].table.body[i][1] = bodystyle;
      }
    }
  },
  exportOptions: {
    columns: [ 1, 2 ],
    stripNewlines: false,
    stripHtml: true,
    modifier: {
      page: 'all' // 'all', 'current'
    },
  }
}],
columns: [
  { className: 'iNo', orderable: true, visible: true},
  { className: 'iIMG', orderable: false, visible: false },
  { className: 'iPDF', orderable: false, visible: false, responsivePriority: 10001 } ]
});
var table = $('#table').DataTable();
table.buttons().container().appendTo('.dt-btn');
$('.dnPDF').on('click', function(){
  $(this).append('<span class="spinner-border spinner-border-sm donePDF" role="status" aria-hidden="true"></span>').closest('button').attr('disabled','disabled');
});
$.fn.dataTable.Buttons.defaults.dom.container.className = '';
$.fn.dataTable.Buttons.defaults.dom.button.className = 'btn';

PHP

public function base64(Request $request)
{
  $request->validate([
    'src' => 'required|string'
  ]);
  $fTYPE = pathinfo($request->src, PATHINFO_EXTENSION);
  $fDATA = @file_get_contents($request->src);
  $imgDATA = base64_encode($fDATA);
  $imgSRC = 'data:image/' . $fTYPE . ';base64,'.$imgDATA;
  $error = ($imgDATA!='') ? 0 : 1;
  $msg = ($error) ? 'Error' : 'Success';
  return response()->json([ 'msg' => $msg, 'error'=> $error, 'data' => $imgSRC]);
}

[Sample][1]: https://i.stack.imgur.com/35Wlm.jpg

0
votes

In addition to davidkonrad's answer. I created dynamically all base64 images and used them in Datatables pdfmake like this:

for (var i = 1; i < doc.content[2].table.body.length; i++) {
    if (doc.content[2].table.body[i][1].text.indexOf('<img src=') !== -1) {
        html = doc.content[2].table.body[i][1].text;

        var regex = /<img.*?src=['"](.*?)['"]/;
        var src = regex.exec(html)[1];


        var tempImage = new Image();
        tempImage.src = src;

        doc.images[src] = getBase64Image(tempImage)

        delete doc.content[2].table.body[i][1].text;
        doc.content[2].table.body[i][1].image = src;
        doc.content[2].table.body[i][1].fit = [50, 50];
    }

    //here i am removing the html links so that i can use stripHtml: true,
    if (doc.content[2].table.body[i][2].text.indexOf('<a href="details.php?') !== -1) {
        html = $.parseHTML(doc.content[2].table.body[i][2].text);
        delete doc.content[2].table.body[i][1].text;
        doc.content[2].table.body[i][2].text = html[0].innerHTML;
    }

}