jquery 数据表导出到PDF和Excel与图像

k7fdbhmy  于 2023-05-06  发布在  jQuery
关注(0)|答案(2)|浏览(199)

我必须生成一个表格并将其导出为HTML。我最初尝试使用php和phpexcel来做这件事,但我的客户端服务器太慢了(有时我的表有2500行)。现在我尝试通过jquery/ AJAX 获取表并安装js插件来导出。我目前使用的数据表和代码视图如下所示:

<!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>

工作示例:https://jsbin.com/senufanozo/edit?html,output
它工作正常,2500行,这不是这个代码的问题,但我不能导出图像。pdf和excel中的图片是非常需要的。我怎么能导出它?
另外如何旋转PDF页面到横向?

drkbr07n

drkbr07n1#

可能回答得有点晚但我会给你一个解决方案。
首先,为了将PDF的方向从Portrait更改为Landscape(假设您使用pdfHtml5),您需要将方向定义为orientation: 'landscape',,这将有助于您以横向导出PDF文件。
使用PDF导出图像需要自定义,并且要对图像进行编码,您不能将图像导出为普通的HTML标记,您应该首先将其编码为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;

编码后的图像,您需要自定义您的PDF导出如下

{
            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
        }
        }

这个应该可以了

pu82cl6c

pu82cl6c2#

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,
          }
       },

相关问题