javascript 选择打印机并静默打印

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19656372/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 16:22:11  来源:igfitidea点击:

Select a printer and silently print

javascriptgoogle-chromeprinting

提问by rightfold

This answershows how to enable silent printing in Google Chrome. However, I have two web pages which have to be silently printed using two different printers without further user interaction. Is there a way to select a printer automatically before calling window.print()? I don't mind writing a Chrome Extension if really necessary.

此答案显示了如何在 Google Chrome 中启用静音打印。但是,我有两个网页,必须使用两台不同的打印机静默打印,而无需进一步的用户交互。有没有办法在调用之前自动选择打印机window.print()?如果真的有必要,我不介意编写 Chrome 扩展程序。

回答by Nathan Breit

Maybe you could setup your printers with Google Clound Print then use the cloud printing APIto 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 html2canvasto rasterize the webpage.

也许您可以使用 Google Clound Print 设置您的打印机,然后使用云打印 API以静默方式向他们提交作业。看起来您可以在提交作业时指定打印机 ID。您可能需要使用html2canvas 之类的东西来光栅化网页。

回答by Carlos Robles

If you are in an environment that you know, and in wich you have enough privileges (i suppose, since you know the printer you want to use) you can try to change it through the command line. For this, you should call

如果您处于您熟悉的环境中,并且您拥有足够的权限(我想,因为您知道要使用的打印机),您可以尝试通过命令行更改它。为此,您应该致电

@RunDLL32.EXE printui.dll,PrintUIEntry /y /n "Printer name"

The printer name has to be the value displayed in the control panel.

打印机名称必须是控制面板中显示的值。

For calling command line from javascript, if you have the proper ActiveX controls enabled, you can use:

对于从 javascript 调用命令行,如果您启用了正确的 ActiveX 控件,则可以使用:

var run=new ActiveXObject('WSCRIPT.Shell').Run("commands to run");

also, you can try with shell.application ShellExecute

另外,您可以尝试使用 shell.application ShellExecute

 var objShell = new ActiveXObject("shell.application");
objShell.ShellExecute("cmd.exe", 'RunDLL32.EXE printui.dll,PrintUIEntry /y /n "Printer name"', "C:\WINDOWS\system32", "open", 1);

For more information you can go to http://msdn.microsoft.com/en-us/library/windows/desktop/gg537745(v=vs.85).aspx

有关更多信息,您可以访问http://msdn.microsoft.com/en-us/library/windows/desktop/gg537745(v=vs.85).aspx

I havent tested it, so good luck!

我还没有测试过,祝你好运!

回答by rightfold

I ended up writing a server in F# and communicating with that through a WebSocket.

我最终用 F# 编写了一个服务器,并通过 WebSocket 与之通信。

回答by Bas van Dijk

I have searched for an answer but it looks like there is no way to set a printer programatically. Therefore my probably complicated solution:

我已经搜索了答案,但似乎无法以编程方式设置打印机。因此我可能复杂的解决方案:

Create a command line application which can switch the default printer of the operating system. Maybe an application which is capable of disabling and enabling a printer. If you are on Windows a .NET application could probably do this. If on Linux there should be a command line interface for printer management (I don't know for sure).

创建一个命令行应用程序,它可以切换操作系统的默认打印机。也许是能够禁用和启用打印机的应用程序。如果您使用的是 Windows,则 .NET 应用程序可能会执行此操作。如果在 Linux 上应该有一个用于打印机管理的命令行界面(我不确定)。

Now make for example a PHP, asp.net or ruby etc. page which is able to call the printer enable/disable program.

现在制作例如一个 PHP、asp.net 或 ruby​​ 等页面,它能够调用打印机启用/禁用程序。

If this is working you can use Javascript calls to first print to printer one and after the switch to printer two. However there some disadvantages:

如果这行得通,您可以使用 Javascript 调用先打印到第一台打印机,然后再切换到第二台打印机。但是也有一些缺点:

  • If printer one is printing a document you can not switch to printer two, since this will disable printer one. So somehow you should time how long a common job takes.
  • There is a lot of overhead in this solution. You need to made extra calls for the switch between printers
  • Maintainability is absolutely not optimal since you need to maintain the printer switch program and the webservice.
  • 如果第一台打印机正在打印文档,则您不能切换到第二台打印机,因为这会禁用第一台打印机。所以不知何故,你应该计算一份普通工作需要多长时间。
  • 这个解决方案有很多开销。您需要额外调用打印机之间的切换
  • 可维护性绝对不是最佳的,因为您需要维护打印机切换程序和网络服务。

I hope for you someone comes up with a better solution, but I wanted to at least share my thoughts. Maybe they help you in solving your problem.

我希望有人能提出更好的解决方案,但我至少想分享一下我的想法。也许他们可以帮助您解决问题。