Juanjo Salvador
Juanjo Salvador

Reputation: 1093

Ionic print to PDF the current page

I have an Ionic app (AngularJS), and I need to print (as PDF) an app's view. Currently I'm using cordova-plugin-printer but when I try to print the page, I get an error.

This is my code. Printer is available (using Android 4.4.2 and 5.1 on emulator), but always there is an error getting the page. And nothing works.

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

I need to print a table with rows like this:

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

And I'm afraid of my plugin doesn't support the curly brackets syntax...

Upvotes: 0

Views: 2861

Answers (3)

niteen bhende
niteen bhende

Reputation: 29

Try this in your code.

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

I think You forget to declare $cordovaPrinter in your Controller.

Upvotes: 1

Juanjo Salvador
Juanjo Salvador

Reputation: 1093

Finally I found a solution creating a PDF (I followed Ashteya Biharisingh's tutorial) and wrinting it to a file using $cordovaFile.

Upvotes: 0

Hyderali S
Hyderali S

Reputation: 36

Use Document.html instead of Document

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

}

Upvotes: 1

Related Questions