Using window.print() or alternative on Android devices

44,170

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>
Share:
44,170
MarshallOfSound
Author by

MarshallOfSound

I'm cool

Updated on July 09, 2022

Comments

  • MarshallOfSound
    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 to window.print()?

  • MarshallOfSound
    MarshallOfSound over 9 years
    Like 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
    Dudeist over 9 years
    That'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
    MarshallOfSound about 9 years
    This 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
    Dave about 7 years
    I could not find the Print command on Chrome android. I can find share ... > Printer.
  • Dave
    Dave about 7 years
    I tried with andriod 7. window.print() does nothing. silently ignored.
  • Karthi
    Karthi about 7 years
    yea 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
    Karthi about 7 years
    yea 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
    Admin almost 7 years
    I 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 the android character string does not show up. I could not test with the Google Chrome web browser because it did not seem to work properly. The android 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
    JFK almost 6 years
    Firefox on android still doesn't support it though.
  • Gabriel G
    Gabriel G over 3 years
    gcp is not avaiable by 2021
  • Amit
    Amit almost 3 years
    Now that google cloud print has stopped its support. How do we handle this is android?
  • Amit
    Amit almost 3 years
    Now that google cloud print has stopped its support. How do we handle this is android?