raghu datta raghu datta - 1 month ago 7
Javascript Question

Saving table data as pdf in PhoneGap application Android

I am a beginner in PhoneGap applications. I am trying to create an application in Android which stores cow data as text in SQLite, and displays them in table form in my application. I was successful in doing it. But I would like to save the table data as PDF to my mobile. I had searched in Google how to save table data and found below code:

<button onclick="printData()">Print me</button>

<script>
function printData()
{
var divToPrint=document.getElementById("printTable");
newWin= window.open("");
newWin.document.write(divToPrint.outerHTML);
newWin.print();
newWin.close();
}

$('button').on('click',function(){
printData();
})


I used this code in my application but I am still unable to save my table data as PDF. Even after clicking button nothing happens. Please give me a suggestion.

Thanks in advance!!
Raghu Datta

Answer

The code you tried to use will not do what you need - it is for sending your table to a printer and it won't work in Cordova.

You should try adding this plugin to your app: https://www.npmjs.com/package/cordova-pdf-generator

And then do something like this:

<button onclick="printData()">Print me</button>

<script>
function printData()
{
   var divToPrint=document.getElementById("printTable");

   pdf.htmlToPDF({
            data: divToPrint.outerHTML,
            documentSize: "A4",
            landscape: "portrait",
            type: "share" //use share to open the open-with-menu. 
        }, this.success, this.failure);
}

$('button').on('click',function(){
printData();
})
</script>
Comments