Page 1 of 1

How to disable popup dialogs window when printing a page

PostPosted: Tue Jan 29, 2019 4:01 am
by xiahaobo01
I want to provided printer info (app user store, such as device name, page size ... ) when printing a page
but How to set CefPrintSettings , I look for CefPrintHandler class, but how to do it ?
Can give me a sample,please ! :|

Re: How to disable popup dialogs window when printing a page

PostPosted: Tue Jan 29, 2019 10:59 am
by ndesktop
As far as I know, if you want to do it programmatically, fork cef. (I had to do this myself).
There is no input/output method virtual bool OnBeforePrinting(CefRefPtr<CefPrintSettings>& output_settings) to let you programmatically set up.

Re: How to disable popup dialogs window when printing a page

PostPosted: Tue Jan 29, 2019 7:49 pm
by xiahaobo01
As you describe, but I catch sight of "CefPrintHandler" and its implementation "CefPrintHandlerAdapter" class in jcef. And in the CefAppHandler and its implementation "CefAppHandlerAdapter" have to defined method
Code: Select all
public CefPrintHandler getPrintHandler ()

why it ? how to use these classes and functions?

Re: How to disable popup dialogs window when printing a page

PostPosted: Wed Jan 30, 2019 3:06 am
by ndesktop
You have to implement CefPrintHandler (directly in your CefApp or using a delegate and forward calls to that delegate, similar with what cefclient sample does).
Then return in the GetPrintHandler a CefRefPtr to the implementation.

What I was saying is that the exposed methods of print handler might be not enough to skip the print dialogs. That's why I said that you might need to fork cef and implement additional things.
In my fork of cef (for other purposes, but including printing) I have changed these files:

- add a new message PrintMsg_HostPrinterCapabilities_Params in
components/printing/common/print_messages.cc
components/printing/common/print_messages.h
Code: Select all
struct PrintMsg_HostPrinterCapabilities_Params {
  PrintMsg_HostPrinterCapabilities_Params();
  ~PrintMsg_HostPrinterCapabilities_Params();

  void Reset();

  ...
  bool need_default_printer_set;
  bool should_ask_user_for_settings;

  bool result;
};


- get the printer capabilities
components/printing/renderer/print_render_frame_helper.cc
Modify the function PrintRenderFrameHelper::Print to incorporate the message in Print call and delegate ask:

Code: Select all
.h
  class Delegate {
...
    // If true, the user can be asked to provide print settings.
    // The default implementation returns |true|.
    virtual bool IsAskPrintSettingsEnabled();
...
};


.cc
bool PrintRenderFrameHelper::Delegate::IsAskPrintSettingsEnabled() {
  return true;
}

void PrintRenderFrameHelper::Print(blink::WebLocalFrame* frame,
                                   const blink::WebNode& node,
                                   bool is_scripted) {
...
    bool should_ask_user_for_settings = true;

    PrintMsg_HostPrinterCapabilities_Params caps_params;
    Send(new PrintHostMsg_QueryHostPrintingCapabilities(
        routing_id(), routing_id(), &caps_params));
    if(caps_params.result) {
      if(caps_params.should_ask_user_for_settings) {
        should_ask_user_for_settings =
            caps_params.should_ask_user_for_settings;
      }
    }
...

  // Ask the browser to show UI to retrieve the final print settings.
  if (!should_ask_user_for_settings &&
      delegate_->IsAskPrintSettingsEnabled()) {
    // PrintHostMsg_ScriptedPrint in GetPrintSettingsFromUser() will reset
    // |print_scaling_option|, so save the value here and restore it afterwards.
....
  }
}


CefPrintRenderFrameHelperDelegate can implement IsAskPrintSettingsEnabled. You can directly use the delegate instead of message PrintHostMsg_QueryHostPrintingCapabilities (which remained on my implementation since CEF1 when printing was scarce if I remember well).

Anyways, CefPrintRenderFrameHelperDelegate can implement a call, but you need at least to modify PrintRenderFrameHelper to call into delegate and populate the settings.
I preferred a message due to translation between CefPrintSettings and internal printing structures.

An alternative might be modification of render frame helper GetPrintSettingsFromUser to call again into delegate which can supply settings and skip the dialog.

But in any case forking is needed AFAIK.

Re: How to disable popup dialogs window when printing a page

PostPosted: Wed Jan 30, 2019 8:27 pm
by xiahaobo01
I will try it, thanks you!