Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic 5 with Angular 9: How to Print from Desktop (not mobile device)

I'm dealing with an ionic 5 app that was built as a web app. There's no mobile component to it.

I need to allow users to print a page. When I have a print button and associated with a function like the one below, it only prints the viewable area.

printPage() {
  window.print();
}

Given the amount of content on the screen, I can tell that this document should be 2-3 pages long.

I see that there's a cordova-plugin-printer plugin, but it's meant for printing from a mobile device.

What's the proper way to get the entire DOM printed?

like image 777
Ben Downey Avatar asked Jul 30 '20 18:07

Ben Downey


People also ask

Is Ionic only for mobile?

Supported platforms For Android, Ionic supports Android 4.4 and up. For iOS, Ionic supports iOS 10 and up. Ionic 2 supports the Universal Windows Platform for building Windows 10 apps. Ionic Framework, based on Angular.

Can I use Ionic for desktop?

This project combines the Electron Framework with the Ionic Framework and provides a starter for building out an app that can run on either the desktop (macOS, Windows and Linux), a browser or mobile devices (iOS and Android). You can use this application to build and run on one or all of these platforms.


2 Answers

That might happen, because of a scrollable div, or something like that, nested in the body.

To get around this, you could manually select what to print by doing something like this answer from another StackOverflow question:

printPage() {
    var mywindow = window.open('', 'PRINT', 'height=400,width=600');

    mywindow.document.write('<html><head><title>' + document.title  + '</title>');
    mywindow.document.write('</head><body>');
    mywindow.document.write(document.getElementById('#printArea').innerHTML);
    mywindow.document.write('</body></html>');

    mywindow.document.close(); // necessary for IE >= 10
    mywindow.focus(); // necessary for IE >= 10*/

    mywindow.print();
    mywindow.close();
}

Link to the original answer: https://stackoverflow.com/a/2255438/9150652

like image 183
MauriceNino Avatar answered Oct 18 '22 21:10

MauriceNino


I recently faced a similar situation and I ended up creating an angular component in my project, which is inspired by e-ngx-print.

The only limitation is that you will have to pass a *.css file to this component for the section you want to print.

I have created a stackblitz for you, have a look and see if it's useful for you.

like image 28
GoWin Avatar answered Oct 18 '22 22:10

GoWin