Skip to content
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

feat: allow customization of print page header and footer #19688

Merged
merged 3 commits into from Aug 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/api/web-contents.md
Expand Up @@ -1264,6 +1264,8 @@ Returns [`PrinterInfo[]`](structures/printer-info.md).
* `dpi` Object (optional)
* `horizontal` Number (optional) - The horizontal dpi.
* `vertical` Number (optional) - The vertical dpi.
* `header` String (optional) - String to be printed as page header.
* `footer` String (optional) - String to be printed as page footer.
* `callback` Function (optional)
* `success` Boolean - Indicates success of the print call.
* `failureReason` String - Called back if the print fails; can be `cancelled` or `failed`.
Expand Down
23 changes: 19 additions & 4 deletions shell/browser/api/atom_api_web_contents.cc
Expand Up @@ -1637,20 +1637,20 @@ void WebContents::Print(mate::Arguments* args) {
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);

// Is the orientation landscape or portrait.
bool landscape = false;
options.Get("landscape", &landscape);
settings.SetBoolean(printing::kSettingLandscape, landscape);

// We set the default to empty string here and only update
// if at the Chromium level if it's non-empty
// Printer device name as opened by the OS.
base::string16 device_name;
options.Get("deviceName", &device_name);
settings.SetString(printing::kSettingDeviceName, device_name);
Expand All @@ -1663,15 +1663,30 @@ void WebContents::Print(mate::Arguments* args) {
options.Get("pagesPerSheet", &pages_per_sheet);
settings.SetInteger(printing::kSettingPagesPerSheet, pages_per_sheet);

// True if the user wants to print with collate.
bool collate = true;
options.Get("collate", &collate);
settings.SetBoolean(printing::kSettingCollate, collate);

// The number of individual copies to print
int copies = 1;
options.Get("copies", &copies);
settings.SetInteger(printing::kSettingCopies, copies);

// For now we don't want to allow the user to enable these settings
// Strings to be printed as headers and footers if requested by the user.
std::string header;
options.Get("header", &header);
std::string footer;
options.Get("footer", &footer);

if (!(header.empty() && footer.empty())) {
settings.SetBoolean(printing::kSettingHeaderFooterEnabled, true);

settings.SetString(printing::kSettingHeaderFooterTitle, header);
settings.SetString(printing::kSettingHeaderFooterURL, footer);
}

// 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);
Expand Down Expand Up @@ -1700,7 +1715,7 @@ void WebContents::Print(mate::Arguments* args) {
settings.SetList(printing::kSettingPageRange, std::move(page_range_list));
}

// Set custom duplex mode
// Duplex type user wants to use.
printing::DuplexMode duplex_mode;
options.Get("duplexMode", &duplex_mode);
settings.SetInteger(printing::kSettingDuplexMode, duplex_mode);
Expand Down