【问题标题】:Saving table data as pdf in PhoneGap application Android在PhoneGap应用程序Android中将表格数据保存为pdf
【发布时间】:2016-10-18 21:29:57
【问题描述】:

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

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

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

提前致谢!! 拉古达塔

【问题讨论】:

    标签: javascript android cordova pdf phonegap-build


    【解决方案1】:

    您尝试使用的代码无法满足您的需求 - 它用于将您的表格发送到打印机,但在 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>
    

    【讨论】:

    • 非常感谢 Orilux 的回复。我不知道如何将插件添加到我的 phonegap 应用程序中。请你给我建议一下。
    • 这取决于您的环境。假设您在 Windows 上,请确保您首先安装了 NPM(请参阅此处:blog.teamtreehouse.com/install-node-js-npm-windows)。然后打开命令提示符窗口并转到您的应用程序的文件夹。首先在命令提示符下运行它:“npm install cordova-pdf-generator”。之后运行:“cordova plugins add node_modules/cordova-pdf-generator”。之后构建并运行您的应用程序。
    • 嗨@Orilux 同时给出“cordova 插件添加 node_modules/cordova-pdf-generator”获取窗口未定义错误请帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多