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.