Reputation:
This answer shows 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.
Upvotes: 17
Views: 69115
Reputation: 5357
If you're doing this for a product or webapp that will run in an environment where there are printers etc. A better solution to just giving clients a webappp to run might be to wrap your webapp with a desktop application using Chromium Embedded Framework. Like Chromely (kind of like Electron, but lighter), EdgeSharp or CEFSharp etc.
Using this technique, you can write code that can actually enumerate printers on the network and talk to hardware while still running your webapp and you can expose new JS api's you build in the desktop app to your web application.
Then to make it smart, you can have your webapp work in both environments, running in the desktop app, or directly in a browser. Where it would have advanced extended features running in the desktop application.
Then you can deploy said application a plethora of ways. For example, Windows 10+ you can have a private windows Store for your company and you can deploy the application to your private windows store where it can be installed via the Private Windows Store which you can setup with Group Policies on Windows.
Or you could have a private npm repo and script the install/update of your application. Even old school techniques like Microsoft Click Once installs, and on and on.
But the most power you can possibly give your web application is to give it a custom browser to run on and with Chromium Embedded Framework it's so easy.
In C# you can spin up a simple Chrome wrapper in less than 60 seconds.
Using .Net 6+ and EdgeSharp for example, you could get this to be cross platform on Linux, Windows, and MacOsx, and also port it to Xamarin and IOS pretty easily so that you can have this power on phones, pc's, mac's, and tablets.
Upvotes: 0
Reputation:
I ended up writing a server in F# and communicating with that through a WebSocket.
Upvotes: 4
Reputation: 10947
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:
var run=new ActiveXObject('WSCRIPT.Shell').Run("commands to run");
also, you can try with 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
I havent tested it, so good luck!
Upvotes: 7
Reputation: 10713
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).
Now make for example a PHP, asp.net or ruby etc. page which is able to call the printer enable/disable program.
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:
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.
Upvotes: 2
Reputation: 1671
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.
Upvotes: 25