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

Add a callback option to `webContents.print` #10081

Merged
merged 1 commit into from Aug 17, 2017

Conversation

Projects
None yet
5 participants
@renaesop
Contributor

renaesop commented Jul 21, 2017

The webContents.print is async but does not support a callback.
And see #9712

* `options` Object (optional)
* `silent` Boolean (optional) - Don't ask user for print settings. Default is `false`.
* `printBackground` Boolean (optional) - Also prints the background color and image of
the web page. Default is `false`.
* `deviceName` String (optional) - Set the printer device name to use. Default is `''`.
* `callback` Function (optional)
The callback function should be `(success) => {}`.

This comment has been minimized.

@MarshallOfSound

MarshallOfSound Jul 21, 2017

Member

This syntax can be documented formally as

* `callback` Function (optional)
  * `success` Boolean - Indicates success of the print call

This comment has been minimized.

@renaesop

renaesop Jul 21, 2017

Contributor

thx

@@ -159,6 +166,8 @@ class PrintViewManagerBase : public content::NotificationObserver,
scoped_refptr<printing::PrintQueriesQueue> queue_;
std::unique_ptr<base::Callback<void(bool)>> callback_ptr;

This comment has been minimized.

@zcbenz

zcbenz Aug 14, 2017

Contributor

Callbacks are usually saved by value, and passed by const reference, it is preferred to declare it as base::Callback<void(bool)> callback_. You can check whether it is null with callback_.is_null().

It is mainly because passing a callback is very cheap, especially compared to a heap allocation.

Send(new PrintMsg_PrintingDone(routing_id(), success));
}
if (callback_ptr.get()) {
callback_ptr->Run(is_success);

This comment has been minimized.

@zcbenz

zcbenz Aug 14, 2017

Contributor

The is_success variable seems unnecessary, you can just call callback_ptr->Run(success && print_job_).

@zcbenz

This comment has been minimized.

Contributor

zcbenz commented Aug 17, 2017

👍

@zcbenz

zcbenz approved these changes Aug 17, 2017

@zcbenz zcbenz merged commit ecff620 into electron:master Aug 17, 2017

1 of 2 checks passed

continuous-integration/travis-ci/pr The Travis CI build could not complete due to an error
Details
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
@IDrinkMoreWater

This comment has been minimized.

IDrinkMoreWater commented Sep 15, 2017

Electron version: 1.7.5
Operating system: Windows 7

i tried the contents.print([options], [callback]), print success,but the callback never working?

  win.webContents.on('did-finish-load', () => {
      win.webContents.print({silent: true, printBackground: false, deviceName: ''},(success)=>{
          let logger=require("electron-log");
          logger.info('orderPrint:'+success);
          mainWindow.webContents.send('getOrders', success);
          win.close();
      });
    })
@renaesop

This comment has been minimized.

Contributor

renaesop commented Sep 17, 2017

see v1.8.0 release log. v1.7 dosen't include this feature at all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment