Reputation: 55
I want to print the page on load in both Windows and Andriod using any browser including google chrome.
I have tried to print on crome in Andriod phone using window.print() but it was giving error,
<script type="text/javascript">
setTimeout(function () {
if (typeof(window.print) != 'undefined') {
window.print();
window.close();
}
}, 1500);
</script>
So I tried some help from google cloud print and what I found is that, it's been deprecated since 31th Dec 2020. Here is my code for that,
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
var ua = navigator.userAgent.toLowerCase();
var isAndroid = ua.indexOf("android") > -1; //&& ua.indexOf("mobile");
setTimeout(function () {
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 {
if (typeof(window.print) != 'undefined') {
window.print();
window.close();
}
}
}, 1500);
</script>
Can anyone help me in this. I want to print the URL on load in both Windows and in Andriod.
Upvotes: 1
Views: 780
Reputation: 193
I implemented as follow:
<iframe id="print-frame" class="print-frame" title="Print" src="https://www.w3schools.com" onload="print"></iframe>
print-frame {display: none}
function print() {
var frame = document.getElementById('print-frame');
if (!frame) return;
var frameWindow = frame.contentWindow;
if (!frameWindow) return;
frameWindow.print();
}
iframe is hidden but onload it opens print dialog. On windows it's enough for printing.
for android I use PaperCut https://www.papercut.com/products/free-software/mobility-print/
Install server part on PC where printers are connected, share printers in admin web UI.
On Android install MobilityPrint app, turn on Print service 'Mobility Print' then in Chrome browser select Share->Print and select any MobilityPrint service printer
Upvotes: 0