3
votes

I must to generate a table and export it to html. I initially tried do this use php and phpexcel but my client server is to slow (some times my table has 2500rows). Now I'm try get table via jquery/ajax and install js plugin to export. I am currently using datatables and my code view like this:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.15/css/dataTables.material.min.css" />

<script src="https://cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.3.1/js/dataTables.buttons.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
<script src="//cdn.rawgit.com/bpampuch/pdfmake/0.1.27/build/pdfmake.min.js"></script>
<script src="//cdn.rawgit.com/bpampuch/pdfmake/0.1.27/build/vfs_fonts.js"></script>
<script src="//cdn.datatables.net/buttons/1.3.1/js/buttons.html5.min.js"></script>


  <style>img{max-width:100px;max-height:100px;}</style>

  <table class="table table-striped table-bordered table-hover zaawansowana_edycja " style="font-size:12px;">
    <thead class="konfiguratorHeaderTable">
        <tr class="obr getdataKrzeselko">
            <td></td>
            <td data-krzeselkoid="15">
                <img class="img-responsive" src="http://www.ikea.com/PIAimages/0238233_PE377690_S5.JPG">
            </td>
            <td data-krzeselkoid="3">
                <img class="img-responsive" src="http://www.ikea.com/PIAimages/0238233_PE377690_S5.JPG">
            </td>
            <td data-krzeselkoid="5">
                <img class="img-responsive" src="http://www.ikea.com/PIAimages/0238233_PE377690_S5.JPG">
            </td>
            <td data-krzeselkoid="4">
                <img class="img-responsive" src="http://www.ikea.com/PIAimages/0238233_PE377690_S5.JPG">
            </td>
            <td data-krzeselkoid="8">
                <img class="img-responsive" src="http://www.ikea.com/PIAimages/0238233_PE377690_S5.JPG">
            </td>
            <td data-krzeselkoid="2">
                <img class="img-responsive" src="http://www.ikea.com/PIAimages/0238233_PE377690_S5.JPG">
            </td>
            <td data-krzeselkoid="14">
                <img class="img-responsive" src="http://www.ikea.com/PIAimages/0238233_PE377690_S5.JPG">
            </td>
            <td data-krzeselkoid="17">
                <img class="img-responsive" src="http://www.ikea.com/PIAimages/0238233_PE377690_S5.JPG">
            </td>
            <td data-krzeselkoid="11">
                <img class="img-responsive" src="http://www.ikea.com/PIAimages/0238233_PE377690_S5.JPG">
            </td>
            <td data-krzeselkoid="12">
                <img class="img-responsive" src="http://www.ikea.com/PIAimages/0238233_PE377690_S5.JPG">
            </td>
        </tr>
        <tr>
            <td></td>
            <td>
                t1 </td>
            <td>
                t2 </td>
            <td>
                t2 </td>
            <td>
                t3 </td>
            <td>
                t4 </td>
            <td>
                twer </td>
            <td>
                gsdf </td>
            <td>
                dfst </td>
            <td>
                fd </td>
            <td>
                ef </td>
        </tr>
    </thead>
    <tbody id="insertHereTbodyContent" style="height: 263px;">
        <tr class="konfiguratorAttr" key="39|39">
            <td> asdasd </td>
            <td class="inputRow" konfigurator="1" key="39|39">
                <input type="hidden" name="produkt[39|39][krzeselka][436][id_krzesla]" value="15"> 72.4000 </td>
            <td class="inputRow" konfigurator="1" key="39|39">
                <input type="hidden" name="produkt[39|39][krzeselka][437][id_krzesla]" value="3"> 38.4000 </td>
            <td class="inputRow" konfigurator="1" key="39|39">
                <input type="hidden" name="produkt[39|39][krzeselka][4495452651249][id_krzesla]" value="5"> 41.0000 </td>
            <td class="inputRow" konfigurator="1" key="39|39">
                <input type="hidden" name="produkt[39|39][krzeselka][4495452651306][id_krzesla]" value="4"> 29.0000 </td>
            <td class="inputRow" konfigurator="1" key="39|39">
                <input type="hidden" name="produkt[39|39][krzeselka][4495452651363][id_krzesla]" value="8"> 40.0000 </td>
            <td class="inputRow" konfigurator="1" key="39|39">
                <input type="hidden" name="produkt[39|39][krzeselka][4495452651417][id_krzesla]" value="2"> 30.9100 </td>
            <td class="inputRow" konfigurator="1" key="39|39">
                <input type="hidden" name="produkt[39|39][krzeselka][4495452651603][id_krzesla]" value="14"> 36.0000 </td>
            <td class="inputRow" konfigurator="1" key="39|39">
                <input type="hidden" name="produkt[39|39][krzeselka][4495452651681][id_krzesla]" value="17"> 23.0000 </td>
            <td class="inputRow" konfigurator="1" key="39|39">
                <input type="hidden" name="produkt[39|39][krzeselka][4495452651735][id_krzesla]" value="11"> 39.6050 </td>
            <td class="inputRow" konfigurator="1" key="39|39">
                <input type="hidden" name="produkt[39|39][krzeselka][4495452651780][id_krzesla]" value="12"> 32.0000 </td>
        </tr>
        <tr class="konfiguratorAttr" key="2|39">
            <td> asd / zxcvz </td>
            <td class="inputRow" konfigurator="1" key="2|39">
                <input type="hidden" name="produkt[2|39][krzeselka][438][id_krzesla]" value="15"> 69.3710 </td>
            <td class="inputRow" konfigurator="1" key="2|39">
                <input type="hidden" name="produkt[2|39][krzeselka][439][id_krzesla]" value="3"> 15.0000 </td>
            <td class="inputRow" konfigurator="1" key="2|39">
                <input type="hidden" name="produkt[2|39][krzeselka][4495452652035][id_krzesla]" value="5"> 41.0000 </td>
            <td class="inputRow" konfigurator="1" key="2|39">
                <input type="hidden" name="produkt[2|39][krzeselka][4495452652101][id_krzesla]" value="4"> 29.0000 </td>
            <td class="inputRow" konfigurator="1" key="2|39">
                <input type="hidden" name="produkt[2|39][krzeselka][4495452652239][id_krzesla]" value="8"> 40.0000 </td>
            <td class="inputRow" konfigurator="1" key="2|39">
                <input type="hidden" name="produkt[2|39][krzeselka][4495452652254][id_krzesla]" value="2"> 30.9100 </td>
            <td class="inputRow" konfigurator="1" key="2|39">
                <input type="hidden" name="produkt[2|39][krzeselka][4495452652401][id_krzesla]" value="14"> 36.0000 </td>
            <td class="inputRow" konfigurator="1" key="2|39">
                <input type="hidden" name="produkt[2|39][krzeselka][4495452652392][id_krzesla]" value="17"> 23.0000 </td>
            <td class="inputRow" konfigurator="1" key="2|39">
                <input type="hidden" name="produkt[2|39][krzeselka][4495452652515][id_krzesla]" value="11"> 37.3748 </td>
            <td class="inputRow" konfigurator="1" key="2|39">
                <input type="hidden" name="produkt[2|39][krzeselka][4495452652524][id_krzesla]" value="12"> 32.0000 </td>
        </tr>
    </tbody>
    <tfoot>
        <tr>
            <td></td>
            <td>
                <button class="dodajTkanineDoKrzeselka" href="/excel/show-konfiguruj-excel-raw-table?id=8">+</button>
            </td>
            <td>
                <button class="dodajTkanineDoKrzeselka" href="/excel/show-konfiguruj-excel-raw-table?id=8">+</button>
            </td>
            <td>
                <button class="dodajTkanineDoKrzeselka" href="/excel/show-konfiguruj-excel-raw-table?id=8">+</button>
            </td>
            <td>
                <button class="dodajTkanineDoKrzeselka" href="/excel/show-konfiguruj-excel-raw-table?id=8">+</button>
            </td>
            <td>
                <button class="dodajTkanineDoKrzeselka" href="/excel/show-konfiguruj-excel-raw-table?id=8">+</button>
            </td>
            <td>
                <button class="dodajTkanineDoKrzeselka" href="/excel/show-konfiguruj-excel-raw-table?id=8">+</button>
            </td>
            <td>
                <button class="dodajTkanineDoKrzeselka" href="/excel/show-konfiguruj-excel-raw-table?id=8">+</button>
            </td>
            <td>
                <button class="dodajTkanineDoKrzeselka" href="/excel/show-konfiguruj-excel-raw-table?id=8">+</button>
            </td>
            <td>
                <button class="dodajTkanineDoKrzeselka" href="/excel/show-konfiguruj-excel-raw-table?id=8">+</button>
            </td>
            <td>
                <button class="dodajTkanineDoKrzeselka" href="/excel/show-konfiguruj-excel-raw-table?id=8">+</button>
            </td>
        </tr>
    </tfoot>
</table>

  <script>

    $('table').DataTable( {
        dom: 'Bfrtip',
        buttons: [
            {
                extend: 'excelHtml5',
                title: 'Data export'
            },
            {
                extend: 'pdfHtml5',
                title: 'Data export'
            }
        ]
    } );
  </script>

</body>
</html>

Working example: https://jsbin.com/senufanozo/edit?html,output

It works fine and 2500 rows it's not problem for this code but I can't export images. Pictures in pdf and excel are very much needed. How I can export it ?

Additionaly how rotate pdf page to landscape ?

2

2 Answers

2
votes

It might be a late answer however I'm gonna lead you to a solution.

First in order to change the orientation of PDF from Portrait to Landscape assuming your using pdfHtml5 you need to define the orientation as following orientation: 'landscape', this will help you export the PDF file in landscape.

Exporting images with PDF requires a customization and to encode your image you can't export the images as a normal HTML tag you shall at first encode it as base64 encoder

$image = 'http://localhost/img.jpg';

 // Read image path, convert to base64 encoding
 $type = pathinfo($image, PATHINFO_EXTENSION);
 $data = file_get_contents($image);

 $imgData = base64_encode($data);

 // Format the image SRC:  data:{mime};base64,{data};
 $src = 'data:image/' . $type . ';base64,'.$imgData;

After encoding the images you need to customize your PDF export as following

        {
            extend: 'pdfHtml5',
            orientation: 'landscape',
            customize: function (doc) {
               if (doc) {
                    for (var i = 1; i < doc.content[1].table.body.length; i++) {
                        doc.content[1].table.body[i][1] = {
                            image: '<?php echo $src; ?>,
                        };
                    }
                }
            },
            exportOptions: {
                stripHtml: false
        }
        }

This should do the trick.

1
votes
extend: 'pdfHtml5',
      customize: function(doc) {
       //find paths of all images, already in base64 format
       var arr2 = $('.img-fluid').map(function(){
                                return this.src;
                 }).get();
                 
             
 
     for (var i = 0, c = 1; i < arr2.length; i++, c++) {
                       doc.content[1].table.body[c][6] = {
                         image: arr2[i],
                         width: 100
                       }
                         }
     },   
         exportOptions: {
        columns: ':visible',
        stripHtml: true,
        }},