如何在 jquery/javascript 中跳过浏览器默认打印预览并将内容直接打印到打印机?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27633386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to skip browser default print preview and print content directly to printer in jquery/javascript?
提问by Mujassir Nasir
As, we know in C# windows form application we can print content direclty to print without any preview. I want to print content in jquery/javascript when user click a button, browser does not show any print preview and content printed to printer
因为,我们知道在 C# windows 窗体应用程序中,我们可以直接打印内容而无需任何预览。我想在用户单击按钮时在 jquery/javascript 中打印内容,浏览器不显示任何打印预览和打印到打印机的内容
采纳答案by Chankey Pathak
Browsers will not allow that to happen. You can use the below plugin to provide beautiful previews.
浏览器不会允许这种情况发生。您可以使用以下插件提供漂亮的预览。
https://github.com/etimbo/jquery-print-preview-plugin
https://github.com/etimbo/jquery-print-preview-plugin
Demo: http://etimbo.github.io/jquery-print-preview-plugin/example/index.html
演示:http: //etimbo.github.io/jquery-print-preview-plugin/example/index.html
回答by Mujassir Nasir
After hours of research I found following solutions to resolve my issue.
经过数小时的研究,我找到了以下解决方案来解决我的问题。
Maybe you could setup your printers with Google Clound Print then use the cloud printing API to silently submit jobs to them. It looks like you can specify the printer id when you submit the job. You might need to use something like html2canvas to rasterize the webpage.
也许您可以使用 Google Clound Print 设置您的打印机,然后使用云打印 API 以静默方式向他们提交作业。看起来您可以在提交作业时指定打印机 ID。您可能需要使用 html2canvas 之类的东西来光栅化网页。
Found here Select a printer and silently print
在这里找到选择打印机并静默打印
In chrome (v18+) we have the --kiosk --kiosk-printing switches. One can print automatically to default printer without print confirmation.
在 chrome (v18+) 中,我们有 --kiosk --kiosk-printing 开关。无需打印确认即可自动打印到默认打印机。
You can see it from this video http://www.youtube.com/watch?v=D6UHjuvI7IE
你可以从这个视频中看到它http://www.youtube.com/watch?v=D6UHjuvI7IE
回答by Marin Vartan
it is posible in IE with VBScript script, just put it into your page.
它可以在 IE 中使用 VBScript 脚本,只需将其放入您的页面即可。
<script type="text/VBScript" language="VBScript">
Sub Print()
OLECMDID_PRINT = 6
OLECMDEXECOPT_DONTPROMPTUSER = 2
OLECMDEXECOPT_PROMPTUSER = 1
call WB.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER,1)
End Sub
document.write "<object ID='WB' WIDTH=0 HEIGHT=0 CLASSID='CLSID:8856F961-340A-11D0-A96B-00C04FD705A2'></object>"
</script>
<script type="text/javascript">
setTimeout(function() {
window.print();
self.close();
}, 1000);
</script>

