在 Android 设备上(我测试过 Nexus 5、Nexus 10、Galaxy S4 和 Galaxy Tab 3),window.print()
JavaScript 中的命令没有任何作用。据我所知,它甚至没有记录错误。
我知道大多数(如果不是全部)这些浏览器都可以打印,因为您可以使用移动 Chrome 的菜单来选择“打印”。
为什么不window.print()
触发您期望的行为(打开客户端打印菜单)?有没有安卓的替代品window.print()
?
在 Android 设备上(我测试过 Nexus 5、Nexus 10、Galaxy S4 和 Galaxy Tab 3),window.print()
JavaScript 中的命令没有任何作用。据我所知,它甚至没有记录错误。
我知道大多数(如果不是全部)这些浏览器都可以打印,因为您可以使用移动 Chrome 的菜单来选择“打印”。
为什么不window.print()
触发您期望的行为(打开客户端打印菜单)?有没有安卓的替代品window.print()
?
我正在研究一个类似的问题并提出了这个解决方案:
$(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>
我没有时间检查这是否有效,我目前没有安卓设备。我很想对此有一些反馈;-)
使用Google 云打印(GCP) - 无需应用程序。用户必须通过 GCP 设置打印机。
此示例使用GCP 小工具
<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>
我认为,默认情况下,设备上禁用直接 print() 方法。我没有看到这么多带打印机的手机或其他 Android 设备,但是通过 USB 当然应该是可能的。
相反,建议将内容/页面保存为 pdf 并通过某些云打印服务进行打印。
现在,window.print()
正在Android设备上工作。
At this moment, window.print() functionality works perfectly on my Android 5.0.1 device with both, Chrome and the default browser.
在您的手机中下载 adobe acrobat,您可以在手机中使用 windows.print()。