weixin_33681778 2017-03-22 19:03 采纳率: 0%
浏览 72

JSON资料转成autoTable jsPDF

Since few days, I try to find a soluce for my problem : With help of @Oliver, I get AJAX working. This is my PHP code for get data and return JSON :

if (isset($_GET['nexans'])) {

  $nexans = json_decode($_GET['nexans'], true);

  $tab_req = array();


  foreach($nexans as $key => $value) {
    $req_jsonmultipdf = $maPdoFonction - > PDF_Multi($key, $value, $_SESSION['ssetablissement_id'], '4', 'NEXANS');
    $tab_req[] = $req_jsonmultipdf - > fetchAll(PDO::FETCH_ASSOC);
  }

  $retour = array(
    "success" => true,
    "data" => $tab_req
  );

  header('Content-Type: application/json; charset=utf-8');
  echo json_encode($tab_req);
}

Here, no problem. I need to use jsPDF for generate a PDF file. I need too autoTable (plugin for jsPDF if you don't know). In this PDF file, I must insert JSON data in a table with autoTable.

EDIT : This is my JS who generate the PDF file :

$.ajax({
      type: "GET",
      url: "../modules/ajax/A.php",
      data: {
        'A': _json
      },
      dataType: 'json',
      success: function(json) {
          var len = json.length;
          if (len > 0) {

            // Default export is a4 paper, portrait, using milimeters for units
            var pdf = new jsPDF();
            pdf.page = 1;

            var trame_eiffage = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABcAAAGBCAMAAAC6vNp4AAAAIVBMVEWBgYEAAAD/AAAAAAD/AAAAAAD+AAAAAAD+AAAAAAD/AAD1ONESAAAACXRSTlMAERESEu3t7u5UgpEXAAAAVUlEQVR4Ae3UKQ6AUABDwbIv9z8wFlHxE4Kbkc+3ubpPXdePLksXeJu6bJ3d6f4K/6br/g1/pfsrGDJ32bvc3VDX9bPL2gX8m+7f+IEf03U/hn/T9Qd/S7Do6Hk+MwAAAABJRU5ErkJggg==';
            pdf.addImage(trame_eiffage, 'PNG', 207, 3, 3, 60);

            /* Création de tableau avec des données JSON
                                                            Source : https://github.com/simonbengtsson/jsPDF-AutoTable
                                                     */
            var columns = ["Chantier", "Codet", "Désignation", "Q.", "Prix U", "Livraison à", "GPS : Lat.", "GPS : Lon."];

            pdf.autoTable(columns, json, {
              styles: {
                fillColor: [156, 154, 154],
              },
              headerStyles: {
                lineWidth: 0.35,
                lineColor: [0, 0, 0],
                valign: 'middle',
                halign: 'center',
                fontStyle: 'bold'
              },
              bodyStyles: {
                lineWidth: 0.35,
                lineColor: [0, 0, 0]
              },
              margin: {
                horizontal: 0,
                top: 55,
                bottom: 0
              },
              columnStyles: {
                0: {
                  columnWidth: 18,
                  halign: 'middle',
                  fontStyle: 'bold',
                  textColor: [0, 0, 0]
                },
                1: {
                  columnWidth: 17,
                  halign: 'middle',
                  fontStyle: 'bold',
                  textColor: [0, 0, 0]
                },
                2: {
                  columnWidth: 'auto',
                  halign: 'left',
                  fontStyle: 'bold',
                  textColor: [0, 0, 0]
                },
                3: {
                  columnWidth: 8,
                  halign: 'left',
                  fontStyle: 'bold',
                  textColor: [0, 0, 0]
                },
                4: {
                  columnWidth: 13,
                  halign: 'middle',
                  fontStyle: 'bold',
                  textColor: [0, 0, 0]
                },
                5: {
                  columnWidth: 'auto',
                  halign: 'left',
                  fontStyle: 'bold',
                  textColor: [0, 0, 0]
                },
                6: {
                  columnWidth: 'auto',
                  halign: 'left',
                  fontStyle: 'bold',
                  textColor: [0, 0, 0]
                },
                7: {
                  columnWidth: 'auto',
                  halign: 'left',
                  fontStyle: 'bold',
                  textColor: [0, 0, 0]
                }
              },

              showHeader: 'everyPage', // 'everyPage', 'firstPage', 'never',
              tableWidth: 183,
              margin: {
                top: 40,
                right: 13,
                bottom: 0,
                left: 13
              },
              pageBreak: 'auto', // 'auto', 'avoid'
              overflow: 'linebreak' // visible, hidden, ellipsize or linebreak
            });

            function header() {
              /* Encodage en base64 de l'image (obligatoire)
                                                                 Source : https://www.base64encode.org/
                                                            */
              var logo_eiffage = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMoAAABACAMAAABskBgkAAADAFBMVEUAAAABAQECAgIDAwMEBAQFBQUGBgYHBwcICAgJCQkKCgoLCwsMDAwNDQ0ODg4PDw8QEBARERESEhITExMUFBQVFRUWFhYXFxcYGBgZGRkaGhocHBwdHR0eHh4fHx8gICAhISEiIiIjIyMkJCQmJiYnJycoKCgpKSkqKiorKyssLCwtLS0uLi4vLy8wMDAxMTEyMjIzMzM1NTU2NjY3Nzc4ODg5OTk6Ojo8PDw9PT0+Pj4/Pz9AQEBBQUFCQkJFRUVGRkZHR0dISEhJSUlKSkpLS0tMTExOTk5PT09QUFBTU1NUVFRWVlZXV1dYWFhZWVlbW1tcXFxdXV1eXl5fX19gYGBiYmJjY2NkZGRlZWVmZmZnZ2doaGhpaWlqampra2tsbGxtbW1ubm5vb29wcHBycnJzc3N1dXV2dnZ3d3d5eXl7e3t8fHx9fX1+fn5/f3//AAD/AwP/BQX/Dw//ERH/EhL/ExP/FRX/Fhb/Hh7/Hx//ISH/JSX/Ojr/PDz/PT3/SUn/W1v/XFz/Xl7/YWGAgICBgYGCgoKDg4OEhISFhYWGhoaHh4eIiIiJiYmKioqMjIyNjY2Ojo6Pj4+QkJCRkZGSkpKTk5OVlZWWlpaXl5eYmJiZmZmampqcnJydnZ2enp6fn5+goKChoaGioqKjo6OkpKSlpaWmpqanp6eoqKipqamqqqqsrKytra2urq6vr6+wsLCxsbGysrKzs7O0tLT/gYH/jo7/kJAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABCr9+nAAABAHRSTlP///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////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dF1N9Uno8e8Nl3q1mrc/yay4rgVj2mJR33/GnKW8zyWmIOp998QqqpRCzadyGekTXqRDwkK68kZm8xXMx1LcjW/wzfPIl8nUm2wwxn9ZSu5Yj9YTDUJ7xtjxpTiykCkMcNLcqRnLBUaRY9nni77Sq7VYGKkYjecxudpdO+1B8zWzy8T+zbaDjZs5TSKmAdYkC9gEd6krqqjqLrjBwgMq8/TaqXTnwKqXjMqzGzKTAY2jkWB7V11C0ahNzlFoHTKBxyfOodqPFE/PXfjp2w9qQLuXNY/7oq8yDordMVFN9WaZozdRD3ea/+IZW8iVbK8YVOsaVNhx1j5RZLwR/iwxSgpQgJUh5uoKUIKW9esyk7ObYSPke4mfu0yeOVpD/haOIXAhMXkfH9RogP0py/PPxx7+XszSlbWBggE5K7sV4Sk73wMCgk1IycLqqgjyQ23CTmxa7KWv09OxMuUexD1MRT8mlWjiJ3FlI1tVTxVP2Z/ENtjIWi0lKb95aakpocyzW5qQUxqIVZWTOIfJsLHYzntKop2dnSk1TU5OkcAf6hlNmNDW1OCnhqZh1j8TnzrGZee3xlEo9PTtTbpHUFC4oaLY3WCUxj2TJEjrq3ZR9WXyDfZFIeRCGDvvEWLmQ00OuKTxPstpNiWVxSkEkErmoKVyvKSiORMo1hY0VJP8SHl8eCbkpec7p0axM4QUh/1JfxI9cdiKvukevdHS8+BPJfpJ98ttHwR4sSMmkICVI+R3wABzxqWr6hgAAAABJRU5ErkJggg==';

              pdf.setFont("helvetica");
              pdf.setFontType("bold");
              pdf.setFontSize(20);
              pdf.text(200, 15, 'DEMANDE D\'ACHAT', null, null, 'right');
              pdf.setFontSize(8);
              pdf.setFont("helvetica");
              pdf.text(189, 8, 'le ' + date_du_jour + ',', null, null, 'right');

              pdf.addImage(logo_eiffage, 'JPEG', 8, 8, 40, 14);
            };
            header();

            pdf.setFontSize(10);
            pdf.setFont("helvetica");
            pdf.setFontType("normal");
            pdf.text(18, 35, 'Voici le récapitulatif de votre demande d\'achat pour le fournisseur ');

            pdf.setFontSize(10);
            pdf.setFont("helvetica");
            pdf.setFontType("bold");
            pdf.text(121, 35, 'X');

            pdf.setFontSize(10);
            pdf.setFont("helvetica");
            pdf.setFontType("normal");
            pdf.text(136, 35, ' : ');

            // then use this as a counter.
            function footer() {
              pdf.setFontSize(8);
              pdf.setFont("helvetica");
              pdf.setFontType("bold");
              pdf.text(150, 285, 'Page ' + pdf.page);
              pdf.page++;
            };
            footer();

            pdf.save('A' + date_du_jour + '.pdf');

So, from my AJAX request, I get a array, and in this array, each row who is an array. I search a soluce for parse my JSON, and populate autoTable for create a table so please. If you have idea for help me please. Thanks for your answer.

  • 写回答

1条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 c++字符串分割问题
      • ¥15 关于#sql#的问题:没有用命令关闭cdc,手动把系统表开启的cdc右键删除了
      • ¥15 android freedom
      • ¥15 使用自定义的类型代替内置类型可行吗
      • ¥15 关于STM32的SPI和ENDAT接口编码器通信的问题
      • ¥15 关于#pdfbox#生成的PDF文件正常,转图片中文乱码的问题,如何解决?
      • ¥15 ADS中有关DAC控件的使用问题
      • ¥15 win11如何运行geoserver
      • ¥15 审稿意见请教-用ERNIE进行小样本分类的对比实验问题
      • ¥15 gem5搭建arm v8