离线打印到PDF当前页面

时间:2017-03-21 15:41:36

标签: angularjs cordova ionic-framework printing

我有一个Ionic应用程序(AngularJS),我需要打印(作为PDF)一个应用程序的视图。目前我正在使用cordova-plugin-printer但是当我尝试打印页面时,我收到了错误。

这是我的代码。打印机可用(在模拟器上使用Android 4.4.2和5.1),但始终在获取页面时出错。没有任何作用。

$scope.print = function() {
    if($cordovaPrinter.isAvailable()) {
        var page = location.href;
        $cordovaPrinter.print(page, "Document");
    } else {
        alert("Printing is not available on device");
    }
}

我需要打印一个包含这样的行的表:

<tr ng-repeat="data in tableData">
    <td>{{ data.id }}</td>
    <td>{{ data.field_one}}</td>
    <td>{{ data.field_two }}</td>
</tr>

我害怕我的插件不支持大括号语法...

3 个答案:

答案 0 :(得分:1)

使用Document.html代替Document

$scope.print = function() {
if ($cordovaPrinter.isAvailable()) {
    var page = location.href;
    $cordovaPrinter.print(page, "Document.html");
} else {
    alert("Printing is not available on device");
}

}

答案 1 :(得分:1)

在您的代码中尝试此操作。

var page = "//html tag as u want to print table//';
$cordovaPrinter.print(page, 'Document.html');

我想您忘记在控制器中声明$cordovaPrinter

答案 2 :(得分:0)

最后,我找到了一个创建PDF的解决方案(我跟着Ashteya Biharisingh's tutorial)并使用$cordovaFile将其写入文件。