Google Chrome Extension: Print the page silently
I'm developing an internal Google Chrome Extension that needs a way to initiate print the current page to the printer. I do not want the default Print dialog to come up (so, javascript:window.print() is out of question).
As far as I understand, this is not possible just with the JS + HTML plug-in, so I'm also open to using the NPAPI开发者_Python百科 plugin also (with a dummy mime-type). And I'm concerned for Windows platform only.
I'm also open for various hacks / workarounds if possible, though a standard solution would be nice.
If you think this is not possible, let me know if you know any feature request logged for it?
Any suggestions/clarifications are welcome..
In chrome (v18+) we have the --kiosk --kiosk-printing
switches. One can print automatically to default printer without print confirmation.
You can see it from this video http://www.youtube.com/watch?v=D6UHjuvI7IE
Since NPAPI allows you to create native C++ plugins that you can interact with through an object tag (which you can use from an extension), that would probably be the way to do it.
The tricky bit is that I don't know of a good way to get the bits for printing the page. The only person I know of who has done something similar to this actually got the window handle for the browser (available through NPAPI) and scraped the bits off of it to print that way, but that won't take into account print stylesheets or anything. You could also try using automation events to try to control the print dialog, but I have no idea if that would work or not.
By design, the browsers try not to let you do something like this, as it could open some serious vulnerabilities if any website could just start printing things to your printer without confirmation...
Anyway, if you find a way to do it with C++ you can use FireBreath to ease the creation of the NPAPI plugin.
There are various extensions that take snapshots of the current web page (for example, this one); you could adapt one to send the image to a printer via an NPAPI plugin.
I've recently been looking for a similar ability, and it seems like it would be quite possible using Chrome's new native messaging api.
https://developer.chrome.com/extensions/nativeMessaging
There are plenty of examples of this with C#, but here is one quick example of troubleshooting Chrome native messaging with a basic C# application
Native messaging from chrome extension to native host written in C#
I realize this may be a day late and a dollar short, but in case anyone else comes across this question, this is the solution that worked for me. From inside a C# app, you can directly print to installed printers using the PrintDocument
class. If you figured out a way to get the page image, this would be far easier than using firebreath or NPAPI.
Disable print preview in Google Chrome on Mac
- Quit Google Chrome
- Launch Terminal on your Mac. (Search “Terminal” using the Search box)
- Type defaults write com.google.Chrome DisablePrintPreview -bool true
- Close Terminal and open Google Chrome
Disable print preview in Google Chrome on Windows
- Close Google Chrome
- From your desktop, right click Google Chrome
- Click Properties
- In the dialog box, add ‘ –disable-print-preview‘ at the end of the Target field sans the apostrophe (make sure to include the space before –)
- Click Apply
Disable print preview in Firefox on Mac
- In the address bar type “about:config” and press Enter.
- Right click on the page, hover over ‘New’ and click on ‘Boolean’
- Type ‘print.always_print_silent’ as the preference name and click ‘OK’
- Click on ‘true’ and click ‘OK’.
- Close the about:config window.
Disable print preview in Firefox on Windows
- In the address bar type “about:config” and press Enter.
- Right click on the page, hover over ‘New’ and click on ‘Boolean’ Type
- ‘print.always_print_silent‘ as the preference name and click ‘OK’
- Click on ‘true’ and click ‘OK’.
- Close the about:config window.
https://support.dryfta.com/how-to-disable-print-preview-in-chrome-firefox-on-windows-mac/
精彩评论