1

几天以来,我试图为我的问题找到解决方案:在@Oliver 的帮助下,我得到了 AJAX 工作。这是我获取数据并返回 JSON 的 PHP 代码:

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);
}

在这里,没问题。我需要使用 jsPDF 来生成 PDF 文件。我也需要 autoTable(如果您不知道,请使用 jsPDF 插件)。在这个 PDF 文件中,我必须在带有 autoTable 的表中插入 JSON 数据。

编辑:这是我生成PDF 文件的 JS :

$.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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1N9Uno8e8Nl3q1mrc/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\r', null, null, 'right');
              pdf.setFontSize(8);
              pdf.setFont("helvetica");
              pdf.text(189, 8, '\r\r\r\rle ' + 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');

所以,从我的 AJAX 请求中,我得到一个数组,在这个数组中,每一行都是一个数组。我搜索一个解析我的 JSON 的解决方案,并填充 autoTable 以创建一个表,所以请。如果你有帮助我的想法,请。感谢您的回答。

4

1 回答 1

0

SOLUCE : 1- 发出 AJAX 请求并返回 JSON 2- 在 AJAX 中取得成功,解析 JSON 数据,如下所示:

var rows = [];
                                                                                                jQuery(json).each(function(i, item){ // FOR EACH ROW
                                                                                                    jQuery(item).each(function(i, item){ // EACH VALUE IN THE ROW -> INSERT DATA IN AN ARRAY WHO WILL BE USED BY JSPDF AUTOTABLE LATER
                                                                                                        rows.push([
                                                                                                            item.chantier_ref, item.article_codet, item.description, item.quantite, item.prixU_commande+' €', item.livraison_add1 +' '+item.livraison_add2 +' '+item.livraison_add3 + item.livraison_cp +' '+item.livraison_ville, item.livraison_gps_lat+' °C', item.livraison_gps_lon+' °C'
                                                                                                        ]);
                                                                                                    });
                                                                                                });
于 2017-03-27T12:41:46.710 回答