Print from Chrome without the print dialogs? Using Greasemonkey userscript maybe? - printing

We're developing a browser-based warehouse app that needs to print labels and invoices regularly. We want to be able to print to the local printer without clicking through the the usual browser print dialogs. Is this possible? Possibly using a greasemonkey userscript? We don't want to have to setup a whole CUPS printer network and deal with all that, but warehouse pickers having to click through a print dialog 1000 times a day isn't an option.
We're printing PDFs, not sure if that matters. If we could do this another way using HTML5 or something else I'm open to course changes or other ideas here.

You can do if you start your chrome (v18+) with the --kiosk --kiosk-printing switches.
You can see it from this video https://www.youtube.com/watch?v=1ewep-ZT64E

You can put a button on the form you are generating, then using embedded javascript in the pdf itself, cause printing to happen silently.
There are various ways to print a
document without requiring user
interaction. One way is to use the
doc object’s print method and set the
bSilent attribute to true, as shown
in “Printing PDF Documents” on page 79
and in the following example:
this.print({bUI: false, bSilent: true,
bShrinkToFit: true});
http://partners.adobe.com/public/developer/en/acrobat/sdk/AcroJSGuide.pdf
Take a look at the silent printing section under Print Production.

Here's a batch file example I've created based on the answer provided by #russenreaktor:
#echo off
start "Chrome" chrome --kiosk --kiosk-printing --url http://www.google.com

There's no way to do this with only JavaScript.

Related

How to perform ctrl+j in ruby for chrome browser. I am using RubyMine and WatirWebdriver

Tried below code to click ctrl+J but did not work for chrome browser.
#browser.driver.action.key_down(:control).send_keys("j").key_up(:control).perform
Also tried
#browser.driver.action.send_keys(:control,"J").perform
I read in one blog, above code would work for Firefox browser but not for Chrome.
First off,
The way you send control+j is wrong. The way to send the control+j is,you have to give control+k inside the [] like [:control,"j]
b.send_keys([:control,"j"])
Second off,
it's not possible to send the [:control,"j] via chromedriver because of the following issue
https://bugs.chromium.org/p/chromedriver/issues/detail?id=903
The exact answer lies here
This is a limitation in the way we simulate keyboard input in ChromeDriver. Keys get sent directly to the render process, bypassing the browser process. So any keyboard shortcut handlers in the browser process will not be invoked by sendKeys().

cucumber-js: Prevent attachments swamping output

When working with web applications, in the event of a failed scenario, I sometimes want to record the HTML in my browser's body, along with a few other bits and pieces gathered using javascript to help diagnose what went wrong.
I usually do this by executeScript and grabbing document.body.outerHTML. I then attach the diagnostics using:
await this.attach( JSON.stringify( diagnostics ), "text/plain" );
This works great, but has the unfortunate side effect of dumping the attachment out when running the scenario with default runners (like the progress runner: https://github.com/cucumber/cucumber-js/blob/master/docs/support_files/attachments.md#readme )
How can I attach diagnostics like this (which are primarily for use in continuous pipeline runs) without them swamping my terminal output?
Granted I have not tested this myself but according to the documentation if you provide a path with the output formatter(in your case progress) then the output will be to a file instead of stdout
https://github.com/cucumber/cucumber-js/blob/master/docs/cli.md#formats
See the above docs for more info ^
Without any built-in support (yet) for fine-grained control of attachment output, I'm working around this issue by attaching the output using a custom content type. Anything other than text/plain isn't automatically output.

Applescript: print first or other specific pages

How can you instruct an application or the printer to only print the first page, a page range or just odd or even pages of a file? I attempt this with the help of the Preview app, which looks promising:
set theFile to "[file-path/file]""
tell application "Preview"
activate
print theFile with properties {target printer:"Printer", ending page:1} without «class pdlg»
--these properties isn't available for the printer app, here just limiting amount of printed pages
quit
end tell
But with this I'm bitten by the sandboxd process that tells me the file can't be opened for printing and I get a deny file-read-data result in the log.
In the CUPS suggestion by adamh I encounter issues with umlauts and have other execution issues as well, possibly also because of sandbox rules. The code works from the command line, but not when called in automated fashion.
I tried to look up useful examples of the print command in a reference, in my books and tried searching the online Apple references, but I can't seem to find many examples fitting to the present day situation with sandbox, if any.
You could script printing by command line tool lp & lpr.
These talk to CUPS, Common Unix Printing System
To target pages / ranges:
lp -o page-ranges=2-4 "my_great_document.pdf"
To call it from applescript use do shell script
e.g,
do shell script "lp -o page-ranges=2-4 'my_great_document.pdf'"
For more ideas see: http://www.cups.org/documentation.php/options.html

Automatic Typing Textarea/input/form in JavaScript

I've been searching for a way to make a textarea type inside of itself. Unfortunately, even with some google searching, I still don't have a clue? Do you guys know where to start with this?
http://lmgtfy |dot| com is an example, but I'm not sure if they use some other technique...
The lmgtfy people are simply using javascript to change the value of the input. Here is a simple jsfiddle showing the same thing:
http://jsfiddle.net/Caut6/1/
LMGTFY uses javascript. If you visit the site using chrome or some other browser with a debugger, you should be able to pause javascript execution and check out how they do it, then roll or copy your own version.
In Chrome, the pause button is under the Scripts area. Their bundle.js files appears to host the JS you are looking for, it is around 1000 lines of code, but you should be able to see the few functions you need to borrow their implementation.
Hope this helps.

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 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/

Resources