Using window.print() or alternative on Android devices
Solution 1
It is clearly stated in this Documentation, "The command is supported on iOS, Chrome on Windows and Safari and Chrome on Mac. It is not supported on Android."
Android phones don't have native support for printing yet, so window.print()
will not work. Which means you need to use third-party app to do the printing. You could find some alternatives in this article.
Solution 2
I'm working on a simular 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 haven't had the time to check if this works, i don't have an android device with me at the moment. I Would love to have some feedback on this ;-)
Solution 3
⚠️ [Deprecated] : Google Cloud Print will no longer be supported as of December 31, 2020. Please see the support article for help migrating.
Use Google Cloud Print (GCP) - there is no app required. The user must have set up a printer via GCP though.
This example uses 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>
Comments
-
MarshallOfSound almost 2 years
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.I know for a fact that most if not all of these browsers can print because you can use mobile Chrome's menu to choose "print".
Why doesn't
window.print()
trigger the behavior you would expect (opening the clients print menu)? And is there an Android alternative towindow.print()
? -
MarshallOfSound over 9 yearsLike I said, chrome for android has a "print" option in its menu. This has to be somehow accessible from javascript and as for converting to PDF could you possible elaborate on that.
-
Dudeist over 9 yearsThat's true. But this option is more for cloud print or "print to file" like a pdf or eps where "direct" print can be disabled. Here I found answer stackoverflow.com/questions/9268840/…, can be many reasons why it not working in most of browsers on mobile devices, but in fact, it is just not implemented or disabled. Instead you can still print with share options or save as pdf etc
-
MarshallOfSound about 9 yearsThis is a nice solution, actually more or less what I went with in the end, but my main concern was that I can do the equivilent of File -> Print on my android device (Menu button -> Print) but I can not trigger this with the semi standard
window.print()
function -
Dave about 7 yearsI could not find the Print command on Chrome android. I can find share ... > Printer.
-
Dave about 7 yearsI tried with andriod 7. window.print() does nothing. silently ignored.
-
Karthi about 7 yearsyea i installed google cloud print app, when i click the google cloud print button, the dialog box will open and i choose printer then click the print nothing happen. now what should i do?
-
Karthi about 7 yearsyea i installed google cloud print app, when i click the google cloud print button, the dialog box will open and i choose printer then click the print nothing happen. now what should i do?
-
Admin almost 7 yearsI have not been able to find the
mobile
character string in any user agent character string so far. Also, the trouble is that sometimes the web browser on the mobile electronic device may emulate the desktop mode, so theandroid
character string does not show up. I could not test with the Google Chrome web browser because it did not seem to work properly. Theandroid
character string did show up on the mobile electronic device while using the Firefox web browser and the web Browser web browser made by Litter Penguin. -
JFK almost 6 yearsFirefox on android still doesn't support it though.
-
Gabriel G over 3 yearsgcp is not avaiable by 2021
-
Amit almost 3 yearsNow that google cloud print has stopped its support. How do we handle this is android?
-
Amit almost 3 yearsNow that google cloud print has stopped its support. How do we handle this is android?