Nw.js: print - silent print [$100]

Created on 30 Oct 2013  路  49Comments  路  Source: nwjs/nw.js

Hi,
about the printing functionality is it stable now and mature, then i wan to ask how can i silent print the page some thing like chromes --kiosk --kiosk-printing. thanks

There is a $100 open bounty on this issue. Add to the bounty at Bountysource.

bounty

Most helpful comment

It is supported now by command line switch '--kiosk-printing' in 0.13 version

All 49 comments

+1 - We have an external cpp file to do this right now, pretty hacky.

+1 Where I can vote for this feature? :)

@jonathandelgado... is your external cpp file available to others? I've been working on a node module for extended printing support, and have added some functionality, but failed at actual silent printing of the _page_ (I can send raw text). Will you share?

Any updates on this? Anyone found any usable workarounds?

+1 please

Any news on that? Did @jonathandelgado offer some insights about his cpp file?

No :(

I still don't have a solution for this. I've written a node module that can provide a list of installed printers and print to one. Unfortunately for me, this falls short in that I need to print the current page as webkit would render it for the selected printer. If anyone has any creative ideas of how this may be done, please let me know.

Oh, sorry, I unfollowed the issue and never got the notification, the mention pinged me.

My issue with this was regarding receipts, not really html, so I was able to get by with the windows spool API in cpp. I eventually ended up moving over to a fully OPOS module, which works much better - but wouldn't help with printing HTML. I can't release code samples for either, sorry.

In the end, this solution can only really be solved, in terms of html, my adding an additional JS API for both listing printers and initializing silent printing, which would call the functions from src/browser/printing/print_dialog_gtk.cc directly.

Good luck

Thanks Jonathan, that's a great point in the right direction for me.

I might take a look into https://github.com/tojocky/node-printer to overcome this. Sounds like a possible solution for silent printing.

That project was the basis for my current module, and they've since added support for printing PDFs, but you'd need to know the paper size of the target printer to know what size PDF to generate, unless I'm missing something.

I simply want to be able to window.print({ printer: "Brother HL2700", silent: true}). The path of least resistance to this or something functionally equivalent is what I'm looking for.

yeah +1, I would really need that feature like now. ;)

+1
any news?

+1

+1

+1

+1

To all the people requesting silent printing -- I'd love to support this. Does --kiosk --kiosk-printing switches works for you? I'm looking to support those switches in nw13.

@rogerwang i would love this, for mr those switches would work :)

Or maybe a --printing so you don t need kiosk mode

@rogerwang That is an excellent first step. However, I think the majority of users requesting silent printing also need the ability to select a specific printer. There isn't a printer API in chromium, so that would have to be an external dependency.

That would be fantastic!

I work on Kiosk application and in deep need of good silent print support.
Currently. I have two solutions, both based on https://github.com/tojocky/node-printer :

  • Hack a bit this gist https://gist.github.com/zhchbin/4749217#gistcomment-1392153 and save captured image to file and print it - it utilise an interesting behavior of Window.capturePage(...) functions which allows to capture invisible window (with show:false)
  • Directly draw your content to a Canvas - it might be a bit hard, depending on what you desire to print but, anyway, there are tonns of helper libraries

It is supported now by command line switch '--kiosk-printing' in 0.13 version

@rogerwang why didn't you claim the reward then ? :)

thanks @rogerwang for the hard work. want to ask is it just a switch or does it have an api to select printer and its preferences? thanks again.

@ahmedkandel Currently no.. But in Chromium upstream the feature is working by automatically confirm in the print preview WebUI, which is written in JS/HTML and builtin in the resource file in the release.

If I enable a feature that the WebUI files can be override by you, so you can take the JS files for print preview from Chromium, modify it whatever you want, will that be good enough for you?

See the JS files for print view here: https://github.com/nwjs/chromium.src/tree/nw13/chrome/browser/resources/print_preview

would be a workaround but i prefer to wait for a more robust solution. please take your time thanks again.

I hope this might help someone else. I needed silent printing in NWJS (Windows only) with the following requirements:

  1. Ability to pre-select one or more printers for later printing
  2. Ability to print HTML/CSS provided as a string to a given printer without user interaction

I was able to accomplish this via the following (general overview):

  1. A node addon (c++/win32api) that displays the Windows common printing dialog and returns a JavaScript object containing the selected printer's name and properties
  2. Including PhantomJS for rendering the provided HTML/CSS as a PDF (and writing that PDF to disk)
  3. Including SumatraPDF for PDF printing, providing it the PDF filename/path plus printer name

@acicali I have recently done very similar thing, but because of 3D transformations, I was not able to render proper PDF file.
That's why I've ended up taking PNG snapshot of invisible page as described here https://gist.github.com/zhchbin/4749217#gistcomment-1392153 and printing it using IrfanView

@Vandalko
What then do you use to send the PNG to a desired printer?

@acicali I've configured Windows default printer and IrfanView printing, and then:

        win.capturePage(function(img) {
          var base64Data = img.replace(/^data:image\/(png|jpg|jpeg);base64,/, "");
          var fileName = printFolder + 'out-' + Date.now() + '.png';
          require("fs").writeFile(fileName, base64Data, 'base64', function(err) {
            if (err) {
              winston.error('Print error', {error: err});
            }
            var iPrint = spawn('C:\\Program Files (x86)\\IrfanView\\i_view32.exe', [fileName, '/print']);
            iPrint.on('close', function(code) {
              winston.info('IrfanView exit code: ' + code);
              var maxWaiting = global.konfig.printTime;
              var timeout = maxWaiting - (Date.now() - model.startTime);
              setTimeout(function() {
                global.printDone();
              }, Math.min(Math.max(timeout, 0), maxWaiting));
            });
          });
        }, 'png');

@rogerwang do you have any idea about the possibility to customize/override the print_preview webUI and default preferences?
Is it possible to call the html-pdf process from chrome to build a pdf datauri from html string?

@AndryBray my last proposal is to provide a way that developer can override the webUI by copying chrome/browser/resources/print_preview tree to somewhere under the application's directory and then modify it. Does that sound good to you?

Of course we would like to provide some API for this later, but the override way is straightforward in implementation.

@rogerwang Yes it could be a perfect solution, the most important is to let us interact with default html & js files so we could be able to change behavior of chrome preview. How long does this task take? Is it possibile to have some preview on next nw15? thanks a lot!!!

Yeah, will see this in 0.15.0 or 0.15.1 as Chromium 51 will be released soon in ~ 1 week.

Great! Please, could you do the same for nwjs 0.14 ? It's important because of XP and legacy MacOS versions support. Thank you

@AndryBray yeah, as we announced 0.14 will be under development with new features in at least one year.

@rogerwang You rock man!

@AndryBray I've done the overriding part but unfortunately the HTML file in that directory is not real HTML. It has tags like <include> and <if> which is used in chromium resources processing during building NW. I'll see how to deal with this. The original proposal may not be doable because of this.

Oh no!
@rogerwang How about the possibility to build a bridge between app (nw) and print preview gui? something to let's setup default properties... the best would be a kind of API like:

window.customPrint({
  destination: 'pdf',
  margins: 0
  headings: false,
 printFrom: 1,
 printTo: 3,
 ... and so on
});

@rogerwang do you think is a kind of API doable in short time? Thank you

@AndryBray currently I'm not sure how long it will take. At least it's longer than the previous proposal. If it's really urgent on your side, perhaps just change the preview UI files and rebuild NW would be the fastest way.

Any news about silent printing, im actually waiting for the release.
For the moment im using a node module to convert and compile html to pdf, than with a python script, print the result.

@jasminmif see my previous comment https://github.com/nwjs/nw.js/issues/1263#issuecomment-204324768 for silent printing.

To all: I'm closing this issue and opened a new issue for custom printing API https://github.com/nwjs/nw.js/issues/4911

@rogerwang Can you please provide a sample code of --kiosk printing, where should i add that option cause its very unclear ?

And is there a way to select a specific printer to print the document ?

window.print({
    options: { '-kiosk-printing': true}
})

@jasminmif the argument is used in command line when you launch NW.js

NW v0.14.6 LTS released with custom printing API http://nwjs.io/blog/v0.14.6/ . Please follow up in #4911

I solved it in the package.json with the command: "chromium-args":"--kiosk-printing", and my file let as follows:
{
"version": "0.0.1",
"name": "my-project",
"main": "index.html",
"chromium-args":"--kiosk-printing",
"window":{
"printPreview":false,
"width":1440,
"height":960
}
}

Was this page helpful?
0 / 5 - 0 ratings