【问题标题】:JqGrid data export to PDFJqG​​rid 数据导出为 PDF
【发布时间】:2019-10-14 19:55:20
【问题描述】:

我正在尝试将 jqgrid 导出为 pdf 文档。 我尝试了这段代码,但缺少的是将 html 保存到 pdf 文件。 如何添加?

function ExportPDF() {
                 mya = $("#orwellRadnici").getDataIDs(); // Get All IDs
                    var data = $("#" + table).getRowData(mya[0]); // Get First row to get the
                    // labels
                    var colNames = new Array();
                    var ii = 0;
                    for (var i in data) {
                        colNames[ii++] = i;
                    } // capture col names

                    var html = "<html><head>"
                    + "<style script=&quot;css/text&quot;>"
                    + "orwellRadnici.tableList_1 th {border:1px solid black; text-align:center; "
                    + "vertical-align: middle; padding:5px;}"
                    + "orwellRadnici.tableList_1 td {border:1px solid black; text-align: left; vertical-align: top; padding:5px;}"
                    + "</style>"
                    + "</head>"
                    + "<body style=&quot;page:land;&quot;>";


                    for (var k = 0; k < colNames.length; k++) {
                        html = html + "<th>" + colNames[k] + "</th>";
                    }
                    html = html + "</tr>"; // Output header with end of line
                    for (i = 0; i < mya.length; i++) {
                        html = html + "<tr>";
                        data = $("#orwellRadnici").getRowData(mya[i]); // get each row
                        for (var j = 0; j < colNames.length; j++) {
                            html = html + "<td>" + data[colNames[j]] + "</td>"; // output each Row as
                            // tab delimited
                        }
                        html = html + "</tr>"; // output each row with end of line
                    }
                    html = html + "</table></body></html>"; // end of line at the end                    
                    html = html.replace(/'/g, '&apos;');

                }

有没有更简单的方法来做到这一点?

【问题讨论】:

  • 我也试过了,但我得到了空白的 pdf 文件。为什么? onClickButton : function(e) { try { var doc = new jsPDF(); var specialElementHandlers = { '#orwellRadnici': function (element, renderer) { return true; } } doc.fromHTML( $('#orwellRadnici').html(), 15, 15, { 'width': 170, 'elementHandlers': specialElementHandlers }, doc.save('Kadrovska.pdf') ); } 捕捉 (e) { 警报(e); }},
  • 如果你使用 Guriddo jqGrid JS 有一个用于 PDF 导出的内置方法。

标签: mvcjqgrid


【解决方案1】:

我找到了更好更简单的解决方案:

<script type="text/javascript" language="javascript" src="//cdn.rawgit.com/bpampuch/pdfmake/0.1.26/build/pdfmake.min.js">   </script>
    <script type="text/javascript" language="javascript" src="//cdn.rawgit.com/bpampuch/pdfmake/0.1.26/build/vfs_fonts.js"></script>

grid.jqGrid('navGrid', '#ptoolbar', { pdf: true } );

    grid.jqGrid('navButtonAdd', '#ptoolbar', {
    id:'ExportToPDF',
    caption:'',
    title:'Export To Pdf',
    onClickButton : function(e)
    {
        try {
           // ExportPDF();

            $("#orwellRadnici").jqGrid("exportToPdf",{
                    title: 'PDF Export',
                    orientation: 'landscape',
                    pageSize: 'A3',
                    description: 'Events',
                    customSettings: null,
                    download: 'download',
                    includeLabels : true,
                    includeGroupHeader : true,
                    includeFooter: true,
                    fileName : "File.pdf"
                })

        } catch (e) {
            alert(e);
        }
    },
    buttonicon: 'ui-icon-print'
});

就是这样!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-01
    • 2013-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-11
    相关资源
    最近更新 更多