feat: add a series of new printing options (#18984)
This commit is contained in:
parent
684d1838f9
commit
0348b60a34
2 changed files with 187 additions and 7 deletions
|
@ -1227,9 +1227,26 @@ Returns [`PrinterInfo[]`](structures/printer-info.md).
|
||||||
|
|
||||||
* `options` Object (optional)
|
* `options` Object (optional)
|
||||||
* `silent` Boolean (optional) - Don't ask user for print settings. Default is `false`.
|
* `silent` Boolean (optional) - Don't ask user for print settings. Default is `false`.
|
||||||
* `printBackground` Boolean (optional) - Also prints the background color and image of
|
* `printBackground` Boolean (optional) - Prints the background color and image of
|
||||||
the web page. Default is `false`.
|
the web page. Default is `false`.
|
||||||
* `deviceName` String (optional) - Set the printer device name to use. Default is `''`.
|
* `deviceName` String (optional) - Set the printer device name to use. Default is `''`.
|
||||||
|
* `color` Boolean (optional) - Set whether the printed web page will be in color or grayscale. Default is `true`.
|
||||||
|
* `margins` Object (optional)
|
||||||
|
* `marginType` String (optional) - Can be `default`, `none`, `printableArea`, or `custom`. If `custom` is chosen, you will also need to specify `top`, `bottom`, `left`, and `right`.
|
||||||
|
* `top` Number (optional) - The top margin of the printed web page, in pixels.
|
||||||
|
* `bottom` Number (optional) - The bottom margin of the printed web page, in pixels.
|
||||||
|
* `left` Number (optional) - The left margin of the printed web page, in pixels.
|
||||||
|
* `right` Number (optional) - The right margin of the printed web page, in pixels.
|
||||||
|
* `landscape` Boolean (optional) - Whether the web page should be printed in landscape mode. Default is `false`.
|
||||||
|
* `scaleFactor` Number (optional) - The scale factor of the web page.
|
||||||
|
* `pagesPerSheet` Number (optional) - The number of pages to print per page sheet.
|
||||||
|
* `collate` Boolean (optional) - Whether the web page should be collated.
|
||||||
|
* `copies` Number (optional) - The number of copies of the web page to print.
|
||||||
|
* `pageRanges` Record<string, number> (optional) - The page range to print. Should have two keys: `from` and `to`.
|
||||||
|
* `duplexMode` String (optional) - Set the duplex mode of the printed web page. Can be `simplex`, `shortEdge`, or `longEdge`.
|
||||||
|
* `dpi` Object (optional)
|
||||||
|
* `horizontal` Number (optional) - The horizontal dpi.
|
||||||
|
* `vertical` Number (optional) - The vertical dpi.
|
||||||
* `callback` Function (optional)
|
* `callback` Function (optional)
|
||||||
* `success` Boolean - Indicates success of the print call.
|
* `success` Boolean - Indicates success of the print call.
|
||||||
|
|
||||||
|
|
|
@ -127,6 +127,59 @@ struct Converter<printing::PrinterBasicInfo> {
|
||||||
return dict.GetHandle();
|
return dict.GetHandle();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <>
|
||||||
|
struct Converter<printing::MarginType> {
|
||||||
|
static bool FromV8(v8::Isolate* isolate,
|
||||||
|
v8::Local<v8::Value> val,
|
||||||
|
printing::MarginType* out) {
|
||||||
|
std::string type;
|
||||||
|
if (ConvertFromV8(isolate, val, &type)) {
|
||||||
|
if (type == "default") {
|
||||||
|
*out = printing::DEFAULT_MARGINS;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (type == "none") {
|
||||||
|
*out = printing::NO_MARGINS;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (type == "printableArea") {
|
||||||
|
*out = printing::PRINTABLE_AREA_MARGINS;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (type == "custom") {
|
||||||
|
*out = printing::CUSTOM_MARGINS;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <>
|
||||||
|
struct Converter<printing::DuplexMode> {
|
||||||
|
static bool FromV8(v8::Isolate* isolate,
|
||||||
|
v8::Local<v8::Value> val,
|
||||||
|
printing::DuplexMode* out) {
|
||||||
|
std::string mode;
|
||||||
|
if (ConvertFromV8(isolate, val, &mode)) {
|
||||||
|
if (mode == "simplex") {
|
||||||
|
*out = printing::SIMPLEX;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (mode == "longEdge") {
|
||||||
|
*out = printing::LONG_EDGE;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (mode == "shortEdge") {
|
||||||
|
*out = printing::SHORT_EDGE;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
|
@ -1534,8 +1587,6 @@ bool WebContents::IsCurrentlyAudible() {
|
||||||
|
|
||||||
#if BUILDFLAG(ENABLE_PRINTING)
|
#if BUILDFLAG(ENABLE_PRINTING)
|
||||||
void WebContents::Print(mate::Arguments* args) {
|
void WebContents::Print(mate::Arguments* args) {
|
||||||
bool silent = false, print_background = false;
|
|
||||||
base::string16 device_name;
|
|
||||||
mate::Dictionary options = mate::Dictionary::CreateEmpty(args->isolate());
|
mate::Dictionary options = mate::Dictionary::CreateEmpty(args->isolate());
|
||||||
base::DictionaryValue settings;
|
base::DictionaryValue settings;
|
||||||
if (args->Length() >= 1 && !args->GetNext(&options)) {
|
if (args->Length() >= 1 && !args->GetNext(&options)) {
|
||||||
|
@ -1547,11 +1598,123 @@ void WebContents::Print(mate::Arguments* args) {
|
||||||
args->ThrowError("Invalid optional callback provided");
|
args->ThrowError("Invalid optional callback provided");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set optional silent printing
|
||||||
|
bool silent = false;
|
||||||
options.Get("silent", &silent);
|
options.Get("silent", &silent);
|
||||||
options.Get("printBackground", &print_background);
|
|
||||||
if (options.Get("deviceName", &device_name) && !device_name.empty()) {
|
// Set custom margin settings
|
||||||
settings.SetString(printing::kSettingDeviceName, device_name);
|
mate::Dictionary margins;
|
||||||
|
if (options.Get("margins", &margins)) {
|
||||||
|
printing::MarginType margin_type = printing::DEFAULT_MARGINS;
|
||||||
|
margins.Get("marginType", &margin_type);
|
||||||
|
settings.SetInteger(printing::kSettingMarginsType, margin_type);
|
||||||
|
|
||||||
|
if (margin_type == printing::CUSTOM_MARGINS) {
|
||||||
|
int top = 0;
|
||||||
|
margins.Get("top", &top);
|
||||||
|
settings.SetInteger(printing::kSettingMarginTop, top);
|
||||||
|
int bottom = 0;
|
||||||
|
margins.Get("bottom", &bottom);
|
||||||
|
settings.SetInteger(printing::kSettingMarginBottom, bottom);
|
||||||
|
int left = 0;
|
||||||
|
margins.Get("left", &left);
|
||||||
|
settings.SetInteger(printing::kSettingMarginLeft, left);
|
||||||
|
int right = 0;
|
||||||
|
margins.Get("right", &right);
|
||||||
|
settings.SetInteger(printing::kSettingMarginRight, right);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
settings.SetInteger(printing::kSettingMarginsType,
|
||||||
|
printing::DEFAULT_MARGINS);
|
||||||
|
}
|
||||||
|
|
||||||
|
settings.SetBoolean(printing::kSettingHeaderFooterEnabled, false);
|
||||||
|
|
||||||
|
// Set whether to print color or greyscale
|
||||||
|
bool print_color = true;
|
||||||
|
options.Get("color", &print_color);
|
||||||
|
int color_setting = print_color ? printing::COLOR : printing::GRAY;
|
||||||
|
settings.SetInteger(printing::kSettingColor, color_setting);
|
||||||
|
|
||||||
|
bool landscape = false;
|
||||||
|
options.Get("landscape", &landscape);
|
||||||
|
settings.SetBoolean(printing::kSettingLandscape, landscape);
|
||||||
|
|
||||||
|
base::string16 device_name;
|
||||||
|
options.Get("deviceName", &device_name);
|
||||||
|
settings.SetString(printing::kSettingDeviceName, device_name);
|
||||||
|
|
||||||
|
int scale_factor = 100;
|
||||||
|
options.Get("scaleFactor", &scale_factor);
|
||||||
|
settings.SetInteger(printing::kSettingScaleFactor, scale_factor);
|
||||||
|
|
||||||
|
int pages_per_sheet = 1;
|
||||||
|
options.Get("pagesPerSheet", &pages_per_sheet);
|
||||||
|
settings.SetInteger(printing::kSettingPagesPerSheet, pages_per_sheet);
|
||||||
|
|
||||||
|
bool collate = true;
|
||||||
|
options.Get("collate", &collate);
|
||||||
|
settings.SetBoolean(printing::kSettingCollate, collate);
|
||||||
|
|
||||||
|
int copies = 1;
|
||||||
|
options.Get("copies", &copies);
|
||||||
|
settings.SetInteger(printing::kSettingCopies, copies);
|
||||||
|
|
||||||
|
bool print_background = false;
|
||||||
|
options.Get("printBackground", &print_background);
|
||||||
|
settings.SetBoolean(printing::kSettingShouldPrintBackgrounds,
|
||||||
|
print_background);
|
||||||
|
|
||||||
|
// For now we don't want to allow the user to enable these settings
|
||||||
|
// but we need to set them or a CHECK is hit.
|
||||||
|
settings.SetBoolean(printing::kSettingPrintToPDF, false);
|
||||||
|
settings.SetBoolean(printing::kSettingCloudPrintDialog, false);
|
||||||
|
settings.SetBoolean(printing::kSettingPrintWithPrivet, false);
|
||||||
|
settings.SetBoolean(printing::kSettingShouldPrintSelectionOnly, false);
|
||||||
|
settings.SetBoolean(printing::kSettingPrintWithExtension, false);
|
||||||
|
settings.SetBoolean(printing::kSettingRasterizePdf, false);
|
||||||
|
|
||||||
|
// Set custom page ranges to print
|
||||||
|
std::vector<mate::Dictionary> page_ranges;
|
||||||
|
if (options.Get("pageRanges", &page_ranges)) {
|
||||||
|
std::unique_ptr<base::ListValue> page_range_list(new base::ListValue());
|
||||||
|
for (size_t i = 0; i < page_ranges.size(); ++i) {
|
||||||
|
int from, to;
|
||||||
|
if (page_ranges[i].Get("from", &from) && page_ranges[i].Get("to", &to)) {
|
||||||
|
std::unique_ptr<base::DictionaryValue> range(
|
||||||
|
new base::DictionaryValue());
|
||||||
|
range->SetInteger(printing::kSettingPageRangeFrom, from);
|
||||||
|
range->SetInteger(printing::kSettingPageRangeTo, to);
|
||||||
|
page_range_list->Append(std::move(range));
|
||||||
|
} else {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (page_range_list->GetSize() > 0)
|
||||||
|
settings.SetList(printing::kSettingPageRange, std::move(page_range_list));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set custom duplex mode
|
||||||
|
printing::DuplexMode duplex_mode;
|
||||||
|
options.Get("duplexMode", &duplex_mode);
|
||||||
|
settings.SetInteger(printing::kSettingDuplexMode, duplex_mode);
|
||||||
|
|
||||||
|
// Set custom dots per inch (dpi)
|
||||||
|
mate::Dictionary dpi_settings;
|
||||||
|
int dpi = 72;
|
||||||
|
if (options.Get("dpi", &dpi_settings)) {
|
||||||
|
int horizontal = 72;
|
||||||
|
dpi_settings.Get("horizontal", &horizontal);
|
||||||
|
settings.SetInteger(printing::kSettingDpiHorizontal, horizontal);
|
||||||
|
int vertical = 72;
|
||||||
|
dpi_settings.Get("vertical", &vertical);
|
||||||
|
settings.SetInteger(printing::kSettingDpiVertical, vertical);
|
||||||
|
} else {
|
||||||
|
settings.SetInteger(printing::kSettingDpiHorizontal, dpi);
|
||||||
|
settings.SetInteger(printing::kSettingDpiVertical, dpi);
|
||||||
|
}
|
||||||
|
|
||||||
auto* print_view_manager =
|
auto* print_view_manager =
|
||||||
printing::PrintViewManagerBasic::FromWebContents(web_contents());
|
printing::PrintViewManagerBasic::FromWebContents(web_contents());
|
||||||
auto* focused_frame = web_contents()->GetFocusedFrame();
|
auto* focused_frame = web_contents()->GetFocusedFrame();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue