Using window.print () or an alternative on Android devices

On Android devices (I tested Nexus 5, Nexus 10, Galaxy S4, and Galaxy Tab 3), the window.print() command window.print() nothing in JavaScript. As far as I can tell, it doesn't even log an error.

I know that most, if not all of these browsers can print, because you can use the Chrome mobile menu to select "print".

Why window.print() not trigger the behavior you expect (opening the client print menu)? And is there an Android alternative for window.print() ?

+8
source share
7 answers

This Documentation clearly states: " The command is supported on iOS, Chrome on Windows, and Safari and Chrome on Mac. It is not supported on Android ."

Android phones have no built-in print support yet, so window.print() will not work. This means that you need to use a third-party print application. You can find some alternatives in this article .

+5
source

Use Google Cloud Print (GCP) - no application required. The user must configure the printer through GCP.

This example uses the GCP gadget

 <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Print</title> </head> <body> <div> <p>On android devices (I have tested Nexus 5, Nexus 10, Galaxy S4 and Galaxy Tab 3) the window.print() command in javascript doesn't do anything, as far as I can tell it doesn't even register an error.</p> <p>I know for a fact that most if not all of these browsers can print because you can use mobile chromes menu to choose "print". My questions is, why doesn't window.print() trigger the behavior you would expect (opening the clients print menu). And is there an android alternative to window.print()?</p> </div> <div id="gcpPrint"></div> <script src="https://www.google.com/cloudprint/client/cpgadget.js"> </script> <script> var gadget = new cloudprint.Gadget(); gadget.setPrintButton(cloudprint.Gadget.createDefaultPrintButton("gcpPrint")); gadget.setPrintDocument("text/html", "Print", document.documentElement.innerHTML); </script> </body> </html> 
+3
source

I am working on a simulation problem and came up with this solution:

 $(document).ready(function($) { var ua = navigator.userAgent.toLowerCase(); var isAndroid = ua.indexOf("android") > -1; //&& ua.indexOf("mobile"); $('button.print').click(function(e) { e.preventDefault(); if (isAndroid) { // https://developers.google.com/cloud-print/docs/gadget var gadget = new cloudprint.Gadget(); gadget.setPrintDocument("url", $('title').html(), window.location.href, "utf-8"); gadget.openPrintDialog(); } else { window.print(); } return false; }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <button class="print">Print this page</button> 

I didn’t have time to check if this works, I don’t have an Android device right now. I would like some feedback on this; -)

+3
source

Now window.print() works on Android devices.

+1
source

I think the direct print () method is disabled on devices by default. I have not seen so many phones or other Android devices with a printer, but via USB it should be possible, of course.

Instead, it is recommended that you save the content / page in pdf format and print it through the cloud print service. A.

0
source

Currently, the window.print () function works fine on my Android 5.0.1 device with both Chrome and the default browser.

0
source

Download Adobe Acrobat to your phone, and you can use windows.print () on your mobile phone.

0
source

Source: https://habr.com/ru/post/977571/


All Articles