0

I have created a variable in javascript (assume named it as tableDetail) of type string which contains html tags.

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

Is it possible? and How can I achieve this?

Thanking you in advance.

Prashob Thekkyal
  • 410
  • 1
  • 11
  • 27
  • _Is it possible? and How can I achieve this?_ Have you looked for anything? – Jai Oct 29 '15 at 10:13
  • Possible duplicate of [Create PDF with Table using Javascript](http://stackoverflow.com/questions/21305661/create-pdf-with-table-using-javascript) – Shailendra Sharma Oct 29 '15 at 10:14
  • @ShailendraSharma but in Bytescout PDF i.e. Bytescout pdf.textSetBox(50, 50, 200, 200), in that they are specifying x,y coordinates to print text. but i want something like http://howtodoinjava.com/2014/07/29/create-pdf-files-in-java-itext-tutorial/ look for Creating tables in PDFs topic. In this link they are not using any x,y coordinates value to specify where my text should display. if u see my variable it is exactly what we use to create table is html. i just want to use the same table format for pdf content. – Prashob Thekkyal Oct 29 '15 at 11:01

3 Answers3

1

Finally after little changes in my code i have accomplished the objective for creating sap.m.table data into PDF in SAPUI5

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 –

<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>
Prashob Thekkyal
  • 410
  • 1
  • 11
  • 27
0

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.

here is the link for that you can try this. http://code.google.com/p/dompdf/.

0

Best to print your content to an arbitrary page There has to be some processing, which probably is best if you look to some simple PHP - try this: HTML to PDF Converter

Ryan
  • 427
  • 2
  • 15
  • More on this: http://stackoverflow.com/questions/391005/convert-html-css-to-pdf-with-php?rq=1 – Ryan Oct 29 '15 at 10:53