refactor: use default printing path when no user options (#46587)
This commit is contained in:
parent
74d641c7b3
commit
3064b24c9d
3 changed files with 34 additions and 14 deletions
|
@ -263,8 +263,17 @@ WebContents.prototype.print = function (options: ElectronInternal.WebContentsPri
|
||||||
throw new TypeError('webContents.print(): Invalid print settings specified.');
|
throw new TypeError('webContents.print(): Invalid print settings specified.');
|
||||||
}
|
}
|
||||||
|
|
||||||
const pageSize = options.pageSize ?? 'A4';
|
const { pageSize } = options;
|
||||||
if (typeof pageSize === 'object') {
|
if (typeof pageSize === 'string' && PDFPageSizes[pageSize]) {
|
||||||
|
const mediaSize = PDFPageSizes[pageSize];
|
||||||
|
options.mediaSize = {
|
||||||
|
...mediaSize,
|
||||||
|
imageable_area_left_microns: 0,
|
||||||
|
imageable_area_bottom_microns: 0,
|
||||||
|
imageable_area_right_microns: mediaSize.width_microns,
|
||||||
|
imageable_area_top_microns: mediaSize.height_microns
|
||||||
|
};
|
||||||
|
} else if (typeof pageSize === 'object') {
|
||||||
if (!pageSize.height || !pageSize.width) {
|
if (!pageSize.height || !pageSize.width) {
|
||||||
throw new Error('height and width properties are required for pageSize');
|
throw new Error('height and width properties are required for pageSize');
|
||||||
}
|
}
|
||||||
|
@ -286,16 +295,7 @@ WebContents.prototype.print = function (options: ElectronInternal.WebContentsPri
|
||||||
imageable_area_right_microns: width,
|
imageable_area_right_microns: width,
|
||||||
imageable_area_top_microns: height
|
imageable_area_top_microns: height
|
||||||
};
|
};
|
||||||
} else if (typeof pageSize === 'string' && PDFPageSizes[pageSize]) {
|
} else if (pageSize !== undefined) {
|
||||||
const mediaSize = PDFPageSizes[pageSize];
|
|
||||||
options.mediaSize = {
|
|
||||||
...mediaSize,
|
|
||||||
imageable_area_left_microns: 0,
|
|
||||||
imageable_area_bottom_microns: 0,
|
|
||||||
imageable_area_right_microns: mediaSize.width_microns,
|
|
||||||
imageable_area_top_microns: mediaSize.height_microns
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
throw new Error(`Unsupported pageSize: ${pageSize}`);
|
throw new Error(`Unsupported pageSize: ${pageSize}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2944,9 +2944,8 @@ void OnGetDeviceNameToUse(base::WeakPtr<content::WebContents> web_contents,
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the user has passed a deviceName use it, otherwise use default printer.
|
// Use user-passed deviceName, otherwise default printer.
|
||||||
print_settings.Set(printing::kSettingDeviceName, info.second);
|
print_settings.Set(printing::kSettingDeviceName, info.second);
|
||||||
|
|
||||||
if (!print_settings.FindInt(printing::kSettingDpiHorizontal)) {
|
if (!print_settings.FindInt(printing::kSettingDpiHorizontal)) {
|
||||||
gfx::Size dpi = GetDefaultPrinterDPI(info.second);
|
gfx::Size dpi = GetDefaultPrinterDPI(info.second);
|
||||||
print_settings.Set(printing::kSettingDpiHorizontal, dpi.width());
|
print_settings.Set(printing::kSettingDpiHorizontal, dpi.width());
|
||||||
|
@ -3005,6 +3004,17 @@ void WebContents::Print(gin::Arguments* args) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (options.IsEmptyObject()) {
|
||||||
|
auto* print_view_manager =
|
||||||
|
PrintViewManagerElectron::FromWebContents(web_contents());
|
||||||
|
if (!print_view_manager)
|
||||||
|
return;
|
||||||
|
|
||||||
|
content::RenderFrameHost* rfh = GetRenderFrameHostToUse(web_contents());
|
||||||
|
print_view_manager->PrintNow(rfh, std::move(settings), std::move(callback));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Set optional silent printing.
|
// Set optional silent printing.
|
||||||
bool silent = false;
|
bool silent = false;
|
||||||
options.Get("silent", &silent);
|
options.Get("silent", &silent);
|
||||||
|
|
|
@ -183,6 +183,16 @@ class Dictionary : public gin::Dictionary {
|
||||||
|
|
||||||
bool IsEmpty() const { return isolate() == nullptr || GetHandle().IsEmpty(); }
|
bool IsEmpty() const { return isolate() == nullptr || GetHandle().IsEmpty(); }
|
||||||
|
|
||||||
|
bool IsEmptyObject() const {
|
||||||
|
if (IsEmpty())
|
||||||
|
return true;
|
||||||
|
|
||||||
|
v8::Local<v8::Context> context = isolate()->GetCurrentContext();
|
||||||
|
v8::Local<v8::Array> props =
|
||||||
|
GetHandle()->GetOwnPropertyNames(context).ToLocalChecked();
|
||||||
|
return props->Length() == 0;
|
||||||
|
}
|
||||||
|
|
||||||
v8::Local<v8::Object> GetHandle() const {
|
v8::Local<v8::Object> GetHandle() const {
|
||||||
return gin::ConvertToV8(isolate(),
|
return gin::ConvertToV8(isolate(),
|
||||||
*static_cast<const gin::Dictionary*>(this))
|
*static_cast<const gin::Dictionary*>(this))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue