Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can the PDF viewer have the node context? #5002

Closed
ghost opened this issue Jun 18, 2016 · 34 comments
Closed

Can the PDF viewer have the node context? #5002

ghost opened this issue Jun 18, 2016 · 34 comments
Assignees

Comments

@ghost
Copy link

ghost commented Jun 18, 2016

Hi everyone,

I was wondering why the PDF viewer doesn't have access to the node context, I was trying to find a way to use the custom print feature however after testing it appears this extension doesn't have access to the node context.

I tried using "node-remote": "chrome-extension:///" but it said that it is an invalid scheme.

Is there anyway around this?

Thanks!

@rogerwang
Copy link
Member

What's the details of the issue? It may not be related with Node context.

On Sun, Jun 19, 2016, 2:22 AM IBSNA notifications@github.com wrote:

Hi everyone,

I was wondering why the PDF viewer doesn't have access to the node
context, I was trying to find a way to use the custom print feature however
after testing it appears this extension doesn't have access to the node
context.

I tried using "node-remote": "chrome-extension:///" but it said that it
is an invalid scheme.

Is there anyway around this?

Thanks!


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#5002, or mute the thread
https://github.com/notifications/unsubscribe/AAKGGclZuzDASsI8ox7-X8kiZ6reQDJtks5qNDdXgaJpZM4I5AQW
.

@ghost
Copy link
Author

ghost commented Jun 19, 2016

Hi @rogerwang, Basically I am programmatically opening a window to a PDF file and then executing the print method in that window, however it only fires the standard window.print() function, When I inspected the PDF page, it appears it does not have access to the node context ( I tried requiring something directly in pdf.js source within the DevTools but it didn't recognize the require method )

I know I can use Kiosk mode to automate the printing, however the problem is that I am choosing which printer to print to, but the standard window.print doesn't support the printer option.

Offtopic: thank you for creating this project.

Regards.

@ghost
Copy link
Author

ghost commented Jun 19, 2016

@rogerwang A short term solution would be a way to set the default printer programmatically, so I was wondering is there an exposed API that does this? I assume the chrome printing window has something like this because when you change the printer, it persists if you close the print window and open it again.

Regards.

@joshterrill
Copy link
Contributor

Have you tried setting node-remote: *? Maybe that will work?

@ghost
Copy link
Author

ghost commented Jun 23, 2016

@joshterrill Hi, yes I tried, unfortunately "chrome-extension://" is considered an invalid scheme.

@ghost
Copy link
Author

ghost commented Jun 23, 2016

@rogerwang Any suggestion on how to do this?

@joshterrill
Copy link
Contributor

joshterrill commented Jun 24, 2016

I didn't mean to try chrome-extension:// inside there, just put * in there...
You said you tried "node-remote": "chrome-extension://", but try "node-remote": "*"

@ghost
Copy link
Author

ghost commented Jun 24, 2016

@joshterrill Thank you, I tried that as well, but I am getting this error: "Missing scheme separator"

Looks like in the new build you must include a scheme. and "chrome-extension://" Happens to be considered invalid.

Regards.

@joshterrill
Copy link
Contributor

What are you putting into the node-remote field. Copy and paste that in this thread, exactly what you have.

@ghost
Copy link
Author

ghost commented Jun 24, 2016

I also tried using {nodejs: true} with Window.open, but it didn't recognize that attribute.

@ghost
Copy link
Author

ghost commented Jun 24, 2016

@joshterrill Here you go:

"node-remote": "*"

@joshterrill
Copy link
Contributor

How would you modify the chrome-extension file you're looking to modify to add node to anyway? Where would you modify the file at? Are you compiling webkit yourself?

@ghost
Copy link
Author

ghost commented Jun 24, 2016

@joshterrill The idea is to programatically open a new window to a PDF file like so:

gui.Window.open( "app/test.pdf", {show: true}, function( secondaryWindow ) {
    secondaryWindow.print({
         printer: "Printer Name"
    });
});

and then execute the new print method, to print to a specific printer. However since it runs the PDF viewer extension which doesn't happen to have the node context it ends up running the normal browser .print() method. which doesn't support the printer option

@joshterrill
Copy link
Contributor

So your end goal is to just use the print api in nwjs to automatically print a PDF? You could do this by win.print({"printer": "Printer Name", "pdf_path": "app/test.pdf"})... unless I'm misunderstanding what you're trying to do.

@ghost
Copy link
Author

ghost commented Jun 24, 2016

@joshterrill I want to open an existing PDF and send it to the printer of my choice. the example you gave is used to print the current page to a new PDF file.

Basically: open an existing PDF file in a new window, and execute the .print method. however as I explained there is no node context in the PDF viewer extension, so it just executes the normal browser .print() method that doesn't support the new custom options such as printer

I hope the idea is clear now. Thanks!

@joshterrill
Copy link
Contributor

I've almost got it working. Try "node-remote": "*://*"

@rogerwang rogerwang self-assigned this Jun 24, 2016
@ghost
Copy link
Author

ghost commented Jun 24, 2016

@joshterrill Yes that almost did the job. however it looks like var win = require( "nw.gui" ).Window.get(); returns the original window, so it ends up printing it instead of the newly opened PDF window.

@ghost
Copy link
Author

ghost commented Jun 24, 2016

@joshterrill I Inspected the PDF viewer window just to be sure and it looks like it doesn't have the node context even with the nifty "node-remote": "*://*" configuration.

@joshterrill
Copy link
Contributor

So here's how I'm able to interact with the new window:

  var gui = require("nw.gui");
  var win = gui.Window.open ('test.pdf', {
    position: 'center',
    focus: true
  }, function(win) {
    win.on ('loaded', function(){
      this.print({});
    })
  });

So interestingly enough, when you call just this.print(), it interacts with nwjs' print method, not the default window.print() one. The reason I know this is because calling this.print() throws the same error that win.print() throws, it expects a json object to be put into it. Which means this.print({}) is what works. For some reason, it's just not printing automatically.

@ghost
Copy link
Author

ghost commented Jun 24, 2016

@joshterrill Interesting... I just tried this however I am still getting the same behavior. this.print acts the same way as the default browser print method(), whether I supply an argument or not.

If only there is a way to get the node context available when the PDF viewer extension runs. that would solve all of this.

What version have you tested this with?

@joshterrill
Copy link
Contributor

It doesn't act the same though... when I just call this.print() I get an error. I'm using the 0.15.3 sdk

@ghost
Copy link
Author

ghost commented Jun 24, 2016

@joshterrill Very peculiar, I just downloaded the last 0.15.3 sdk version and tried the exact code you provided and I am getting the old behavior :(

@joshterrill
Copy link
Contributor

You don't see this?
Imgur

@ghost
Copy link
Author

ghost commented Jun 24, 2016

@joshterrill For some reason I don't, I just downloaded a new 0.15.3 sdk and used the exact same code as in the screenshot, but I am getting the same behavior I described earlier.

Can you share your package.json file? Thanks!

@rogerwang
Copy link
Member

Unfortunately the pdf viewer is opened in new process, so accessing the nw.Window object from the main process is not working. Will look to fix this.

@ghost
Copy link
Author

ghost commented Jun 28, 2016

@rogerwang Thank you, this will be amazing when it's added..

Let me know if you need any help with it, I am happy to assist in anyway I can ( Testing, etc. )

@joshterrill Thank you for your effort as well.

@rogerwang
Copy link
Member

This is fixed in git and will be available in the next nightly build.

@rogerwang
Copy link
Member

@ghost
Copy link
Author

ghost commented Jun 28, 2016

Hi @rogerwang I just downloaded the latest build and tested my code, and it is working flawlessly.

Thank you very much for your amazing speed and support!

@jasminmif
Copy link

jasminmif commented Jul 1, 2016

Is there a way to catch the callback, after sending the print job to the printer, to close the PDF Print preview page , or better print a PDF file silently ?

nw.Window.open('C:/document.pdf', function(win) {
    win.on('loaded', function() {
        win.print({}, function(callback) {
            win.close();
        });
    });
});

@kailniris
Copy link

@jasminmif win.print({}) is a sync function win.close() will always happen after the job is sent to the printer in theory but sometimes not...

if you want to be sure you can do something like this.

nw.Window.open('C:/document.pdf', function(win) {
    win.on('loaded', function() {
        win.print({});
            setTimeout(function() {
                win.close();
            }, 100)
    });
});

the setTimeout will put the win.close() to the begining of the next call stack after the win.print is finished + 100 ms.

@ghost
Copy link
Author

ghost commented Jul 1, 2016

@jasminmif Try this

nw.Window.open('C:\\document.pdf', { show: false }, function(win) {
    win.print();
});

@romych78
Copy link

romych78 commented Feb 5, 2017

Hi guys. Printing works, but (in same context)
win.getPrinters(function (callback) {
alert(callback);
});
doesn't work. What I'm doing wrong? I need somehow to get list of available printers in the system.

Thanks.

@logazer
Copy link

logazer commented May 19, 2017

Maybe you already figured it out by yourself, but I hope it may help others as well. I ran over this problem myself and solved it.
callback in your code is an array of objects. You can see its structure by parsing it:

win.getPrinters(function (callback) {
    alert(JSON.stringify(callback));
});

Now that you should be able to see something, it should be clear how to access the printer information.
So each printer is accessible via callback[printer] <- with "printer" as the element of the array "callback".
Most likely you will need the "deviceName" for using it with win.print().
This is how to simply list all the printers by their deviceName:

win.getPrinters(function(callback){
    forEach(function(printer) {
        let p = document.createElement("p");
        p.innerHTML = printer["deviceName"];
        document.appendChild(p);
        document.getElementsByTagName("body")[0].appendChild(p);
    }, this);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants