简体   繁体   English

在SAPUI5中使用响应表的实时数据生成PDF文件

[英]Generate PDF file with real time data of responsive table in SAPUI5

I have created a variable in javascript (assume named it as tableDetail) of type string which contains html tags. 我已经在包含HTML标记的string类型的javascript中创建了一个变量(假设将其命名为tableDetail)。

for example 例如

var tableDetail = “<table>”;
…some code
tableDetail += “<tr>”
For loop{
    // logic to create dynamic tables column
tableDetail += “<th>column_data</th>”
}
tableDetail += “<tr>”
some more loops {
    // for each rows - data creation
tableDetail += “<td>row_data</td>”
}
tableDetail += “</tr></table>”

and at the end of all loops, I am getting entire string in tableDetail variable which contains table tag + data. 在所有循环的结尾,我在tableDetail变量中获取了整个字符串,该变量包含表标签+数据。 Below is the code that i get when i hover on variable in debugging mode(inspect element) 以下是我在调试模式下将变量悬停在变量上时得到的代码(检查元素)

"<table width='100%' border='1' cellspacing='0'><tr><th>CurrencyCode</th></tr><tr><td>EUR</td></tr>………"

So, by using tableDetail variable I want to create a PDF on a button click in javascript. 因此,通过使用tableDetail变量,我想在单击javascript的按钮上创建PDF。

Is it possible? 可能吗? and How can I achieve this? 以及我该如何实现?

Thanking you in advance. 预先感谢您。

Finally after little changes in my code i have accomplished the objective for creating sap.m.table data into PDF in SAPUI5 最终,在我的代码进行了少量更改之后,我就完成了在SAPUI5中将sap.m.table数据创建为PDF的目标。

In controller 在控制器中

handlePdf : function(){
    var tabledata = oTable.getModel().getData();
    this.JSONToPDFConvertor(tabledata);     
},

JSONToPDFConvertor: function(JSONData){

    var arrData = typeof JSONData != 'object' ? JSON.parse(JSONData) : JSONData;
    var columns = new Array;
    for (var index in arrData[0]) {
        //Now convert each value to string and comma-seprated
        columns.push(index);
    }
    var rows = new Array;
    console.log(arrData);
    for(var i=0;i<arrData.length;i++){
        rows[i]=new Array;

        for(var j=0;j<arrData.length;){

            for (var index in arrData[0]){

                rows[i][j]=arrData[i][index];
                j++;
            }
        }
    }
    if(columns.length<4){
        var doc = new jsPDF('p', 'pt');
    }else{
        var doc = new jsPDF('l','pt');
    }
    doc.autoTable(columns, rows);

    doc.save('table.pdf');

},

please include following script in index.html – 请在index.html中添加以下脚本–

<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.0.272/jspdf.debug.js"></scri‌​pt>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/2.0.16/jspdf.plugin.‌​autotable.js"></script>

It is not Possible to do in javascript it doest have behavior like that,you can use some plugin which could get ur html data and convert it to pdf. 这是不可能的,它没有像Javascript这样的行为,您可以使用一些可以获取html数据并将其转换为pdf的插件。

here is the link for that you can try this. 这是您可以尝试的链接。 http://code.google.com/p/dompdf/ . http://code.google.com/p/dompdf/

最好将内容打印到任意页面上必须进行一些处理,如果您使用一些简单的PHP,这可能是最好的-尝试以下操作: HTML to PDF Converter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM