在 PhoneGap 应用程序中将 table 数据保存为 pdf Android

Saving table data as pdf in PhoneGap application Android

我是 PhoneGap 应用程序的初学者。我正在尝试在 Android 中创建一个应用程序,它将奶牛数据作为文本存储在 SQLite 中,并在我的应用程序中以 table 形式显示它们。我成功地做到了。但我想将 table 数据以 PDF 格式保存到我的手机中。我在 Google 中搜索了如何保存 table 数据并找到了以下代码:

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

我在我的应用程序中使用了此代码,但我仍然无法将我的 table 数据保存为 PDF。即使点击按钮后也没有任何反应。请给我一个建议。

提前致谢!! 拉古达塔

您尝试使用的代码无法满足您的需求 - 它用于将您的 table 发送到打印机,它在 Cordova 中不起作用。

您应该尝试将此插件添加到您的应用中:https://www.npmjs.com/package/cordova-pdf-generator

然后做这样的事情:

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