HTML / Javascript 一键打印(无对话框)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9213660/
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
HTML / Javascript One Click Print (no dialogs)
提问by teynon
Is it possible to have a print option that bypasses the print dialog?
是否可以有一个绕过打印对话框的打印选项?
I am working on a closed system and would like to be able to pre-define the print dialog settings; and process the print as soon as I click the button.
我在封闭系统上工作,希望能够预定义打印对话框设置;并在我单击按钮后立即处理打印件。
From what I am reading, the way to do this varies for each browser. For example, IE would use ActiveX. Chrome / Firefox would require extensions. Based on this, it appears I'll have to write an application in C++ that can handle parameters passed by the browser to auto print with proper formatting (for labels). Then i'll have to rewrite it as an extension for Chrome / Firefox. End result being that users on our closed system will have to download / install these features depending on which browser they use.
根据我所阅读的内容,每个浏览器的执行方式各不相同。例如,IE 将使用 ActiveX。Chrome / Firefox 需要扩展。基于此,看来我必须用 C++ 编写一个应用程序,该应用程序可以处理浏览器传递的参数,以使用正确的格式(标签)自动打印。然后我将不得不将其重写为 Chrome / Firefox 的扩展。最终结果是,我们封闭系统上的用户必须根据他们使用的浏览器下载/安装这些功能。
I'm hoping there is another way to go about this, but this task most likely violates browser security issues.
我希望有另一种方法来解决这个问题,但这项任务很可能违反了浏览器安全问题。
采纳答案by teynon
I ended up implementing a custom application that works very similar to the Nexus Mod Manager. I wrote a C# application that registers a custom Application URI Scheme. Here's how it works:
我最终实现了一个与 Nexus Mod Manager 非常相似的自定义应用程序。我编写了一个注册自定义Application URI Scheme的 C# 应用程序。这是它的工作原理:
- User clicks "Print" on the website.
- Website links user to "CustomURL://Print/{ID}
- Application is launched by windows via the custom uri scheme.
- Application communicates with the pre-configured server to confirm the print request and in my case get the actual print command.
- The application then uses the C# RawPrinterHelperclass to send commands directly to the printer.
- 用户点击网站上的“打印”。
- 网站将用户链接到“CustomURL://Print/{ID}
- 应用程序由 windows 通过自定义 uri 方案启动。
- 应用程序与预先配置的服务器通信以确认打印请求,并在我的情况下获取实际的打印命令。
- 然后,应用程序使用C# RawPrinterHelper类将命令直接发送到打印机。
This approach required an initial download from the user, and a single security prompt from windows when launching the application the first time. I also implemented some Javascript magic to make it detect whether the print job was handled or not. If it wasn't it asks them to download the application.
这种方法需要用户的初始下载,以及第一次启动应用程序时来自 Windows 的单一安全提示。我还实现了一些 Javascript 魔法,让它检测打印作业是否被处理。如果不是,它会要求他们下载应用程序。
回答by ComputerMinute
I know this is a late reply, but here's a solution I'm using. I have only used this with IE, and have not tested it with any other browser.
我知道这是一个迟到的回复,但这是我正在使用的解决方案。我只在 IE 上使用过这个,没有在任何其他浏览器上测试过。
This Sub Print blow effectively replaces the default print function.
这种子打印功能有效地取代了默认的打印功能。
<script 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>
Then use Javascript's window.print(); ties to a hyperlink or a button to execute the print command.
然后使用 Javascript 的 window.print(); 与超链接或按钮相关联以执行打印命令。
If you want to automatically print when the page loads, then put the code below near tag.
如果要在页面加载时自动打印,请将代码放在标签附近。
<script type="text/javascript">
window.onload=function(){self.print();}
</script>
回答by Reaz Patwary
I found a awesome plugin by Firefox which solve this issue. try seamless printingplugin of firefox which will print something from a web application without showing a print dialog.
我发现 Firefox 的一个很棒的插件可以解决这个问题。尝试使用 firefox 的无缝打印插件,它可以从 Web 应用程序打印一些内容,而不会显示打印对话框。
- Open Firefox
- Search addon name seamless printing and install it
- After successful installation the printing window will get bypassed when user wants to print anything.
- 打开火狐
- 搜索插件名称无缝打印并安装
- 成功安装后,当用户想要打印任何内容时,打印窗口将被绕过。
回答by pater
The general answer is: NO you cannot do this in the general case but there some cases where you might do it. Check http://justtalkaboutweb.com/2008/05/09/javascript-print-bypass-printer-dialog-in-ie-and-firefox/
一般的答案是:不,在一般情况下您不能这样做,但在某些情况下您可以这样做。检查 http://justtalkaboutweb.com/2008/05/09/javascript-print-bypass-printer-dialog-in-ie-and-firefox/
If you where allowed to do such a thing anyway, it would be a security issue since a malware script could silently sent printing jobs to visitor's printer.
如果您无论如何都允许这样做,这将是一个安全问题,因为恶意软件脚本可以静默地将打印作业发送到访问者的打印机。
回答by Abhijeet Kale
I am writing this answer for firefox browser.
我正在为 Firefox 浏览器写这个答案。
Open File > Page Setup
Make all the headers and footers blank
Set the margins to 0 (zero)
In the address bar of Firefox, type about:config
Search for
print.always_print_silent
and double click itChange it from false to true
- This lets you skip the Print pop up box that comes up, as well as skipping the step where you have to click OK, automatically printing the right sized slip.
If
print.always_print_silent
does not come upRight click on a blank area of the preference window
Select new > Boolean
Enter "print.always_print_silent" as the name (without quotes)
Click OK
Select true for the value
You may also want to check what is listed for
print.print_printer
- You may have to choose Generic/Text Only (or whatever your receipt printer might be named)
打开文件 > 页面设置
将所有页眉和页脚设为空白
将边距设置为 0(零)
在 Firefox 的地址栏中,输入 about:config
搜索
print.always_print_silent
并双击它将其从 false 更改为 true
- 这使您可以跳过出现的打印弹出框,以及跳过必须单击确定的步骤,自动打印正确尺寸的单据。
如果
print.always_print_silent
上不来右键单击首选项窗口的空白区域
选择新建 > 布尔值
输入“print.always_print_silent”作为名称(不带引号)
单击确定
为值选择 true
您可能还想检查列出的内容
print.print_printer
- 您可能必须选择 Generic/Text Only(或任何您的收据打印机可能命名的)
回答by demaroar
I was able to solve the problem with this library: html2pdf.js (https://github.com/eKoopmans/html2pdf.js)
我能够解决这个库的问题: html2pdf.js ( https://github.com/eKoopmans/html2pdf.js)
Considering that you have access to it, you could do something like that (taken from the github repository):
考虑到您可以访问它,您可以执行类似的操作(取自 github 存储库):
var element = document.getElementById('element-to-print');
html2pdf(element);