Commit 665cdea2 authored by Hidy Han's avatar Hidy Han Committed by Commit Bot

Let print-to-pdf obey source size scaling option in headless mode.

Bug: 724160
Change-Id: I9ede628f60f2ff32d09bcec9ef365db9246f4c98
Reviewed-on: https://chromium-review.googlesource.com/871046
Commit-Queue: Hidy Han <hidyhan@chromium.org>
Reviewed-by: default avatarLei Zhang <thestig@chromium.org>
Reviewed-by: default avatarMustafa Emre Acer <meacer@chromium.org>
Reviewed-by: default avatarEric Seckler <eseckler@chromium.org>
Reviewed-by: default avatarDmitry Gozman <dgozman@chromium.org>
Cr-Commit-Position: refs/heads/master@{#532965}
parent 69c87413
...@@ -95,7 +95,8 @@ PrintMsg_Print_Params::PrintMsg_Print_Params() ...@@ -95,7 +95,8 @@ PrintMsg_Print_Params::PrintMsg_Print_Params()
header_template(), header_template(),
footer_template(), footer_template(),
should_print_backgrounds(false), should_print_backgrounds(false),
printed_doc_type(printing::SkiaDocumentType::PDF) {} printed_doc_type(printing::SkiaDocumentType::PDF),
prefer_css_page_size(false) {}
PrintMsg_Print_Params::PrintMsg_Print_Params( PrintMsg_Print_Params::PrintMsg_Print_Params(
const PrintMsg_Print_Params& other) = default; const PrintMsg_Print_Params& other) = default;
...@@ -126,6 +127,7 @@ void PrintMsg_Print_Params::Reset() { ...@@ -126,6 +127,7 @@ void PrintMsg_Print_Params::Reset() {
footer_template = base::string16(); footer_template = base::string16();
should_print_backgrounds = false; should_print_backgrounds = false;
printed_doc_type = printing::SkiaDocumentType::PDF; printed_doc_type = printing::SkiaDocumentType::PDF;
prefer_css_page_size = false;
} }
PrintMsg_PrintPages_Params::PrintMsg_PrintPages_Params() PrintMsg_PrintPages_Params::PrintMsg_PrintPages_Params()
......
...@@ -64,6 +64,7 @@ struct PrintMsg_Print_Params { ...@@ -64,6 +64,7 @@ struct PrintMsg_Print_Params {
base::string16 footer_template; base::string16 footer_template;
bool should_print_backgrounds; bool should_print_backgrounds;
printing::SkiaDocumentType printed_doc_type; printing::SkiaDocumentType printed_doc_type;
bool prefer_css_page_size;
}; };
struct PrintMsg_PrintPages_Params { struct PrintMsg_PrintPages_Params {
...@@ -190,6 +191,9 @@ IPC_STRUCT_TRAITS_BEGIN(PrintMsg_Print_Params) ...@@ -190,6 +191,9 @@ IPC_STRUCT_TRAITS_BEGIN(PrintMsg_Print_Params)
// The document type of printed page(s) from render. // The document type of printed page(s) from render.
IPC_STRUCT_TRAITS_MEMBER(printed_doc_type) IPC_STRUCT_TRAITS_MEMBER(printed_doc_type)
// True if page size defined by css should be preferred.
IPC_STRUCT_TRAITS_MEMBER(prefer_css_page_size)
IPC_STRUCT_TRAITS_END() IPC_STRUCT_TRAITS_END()
IPC_STRUCT_TRAITS_BEGIN(printing::PageRange) IPC_STRUCT_TRAITS_BEGIN(printing::PageRange)
......
...@@ -1503,7 +1503,10 @@ void PrintRenderFrameHelper::Print(blink::WebLocalFrame* frame, ...@@ -1503,7 +1503,10 @@ void PrintRenderFrameHelper::Print(blink::WebLocalFrame* frame,
if (!self) if (!self)
return; return;
print_settings.params.print_scaling_option = scaling_option; print_settings.params.print_scaling_option =
print_settings.params.prefer_css_page_size
? blink::kWebPrintScalingOptionSourceSize
: scaling_option;
SetPrintPagesParams(print_settings); SetPrintPagesParams(print_settings);
if (print_settings.params.dpi.IsEmpty() || if (print_settings.params.dpi.IsEmpty() ||
!print_settings.params.document_cookie) { !print_settings.params.document_cookie) {
......
...@@ -631,6 +631,7 @@ void PageHandler::PrintToPDF(Maybe<bool> landscape, ...@@ -631,6 +631,7 @@ void PageHandler::PrintToPDF(Maybe<bool> landscape,
Maybe<bool> ignore_invalid_page_ranges, Maybe<bool> ignore_invalid_page_ranges,
Maybe<String> header_template, Maybe<String> header_template,
Maybe<String> footer_template, Maybe<String> footer_template,
Maybe<bool> prefer_css_page_size,
std::unique_ptr<PrintToPDFCallback> callback) { std::unique_ptr<PrintToPDFCallback> callback) {
callback->sendFailure(Response::Error("PrintToPDF is not implemented")); callback->sendFailure(Response::Error("PrintToPDF is not implemented"));
return; return;
......
...@@ -124,6 +124,7 @@ class PageHandler : public DevToolsDomainHandler, ...@@ -124,6 +124,7 @@ class PageHandler : public DevToolsDomainHandler,
Maybe<bool> ignore_invalid_page_ranges, Maybe<bool> ignore_invalid_page_ranges,
Maybe<String> header_template, Maybe<String> header_template,
Maybe<String> footer_template, Maybe<String> footer_template,
Maybe<bool> prefer_css_page_size,
std::unique_ptr<PrintToPDFCallback> callback) override; std::unique_ptr<PrintToPDFCallback> callback) override;
Response StartScreencast(Maybe<std::string> format, Response StartScreencast(Maybe<std::string> format,
Maybe<int> quality, Maybe<int> quality,
......
...@@ -487,6 +487,7 @@ void HeadlessShell::PrintToPDF() { ...@@ -487,6 +487,7 @@ void HeadlessShell::PrintToPDF() {
page::PrintToPDFParams::Builder() page::PrintToPDFParams::Builder()
.SetDisplayHeaderFooter(true) .SetDisplayHeaderFooter(true)
.SetPrintBackground(true) .SetPrintBackground(true)
.SetPreferCSSPageSize(true)
.Build(), .Build(),
base::Bind(&HeadlessShell::OnPDFCreated, weak_factory_.GetWeakPtr())); base::Bind(&HeadlessShell::OnPDFCreated, weak_factory_.GetWeakPtr()));
} }
......
...@@ -273,6 +273,11 @@ std::unique_ptr<base::DictionaryValue> ParsePrintSettings( ...@@ -273,6 +273,11 @@ std::unique_ptr<base::DictionaryValue> ParsePrintSettings(
settings->margins_in_points.right = settings->margins_in_points.right =
margin_right_in_inch * printing::kPointsPerInch; margin_right_in_inch * printing::kPointsPerInch;
if (const base::Value* prefer_css_page_size =
params->FindKey("preferCSSPageSize")) {
settings->prefer_css_page_size = prefer_css_page_size->GetBool();
}
return nullptr; return nullptr;
} }
#endif #endif
......
...@@ -40,7 +40,8 @@ struct HeadlessPrintManager::FrameDispatchHelper { ...@@ -40,7 +40,8 @@ struct HeadlessPrintManager::FrameDispatchHelper {
}; };
HeadlessPrintSettings::HeadlessPrintSettings() HeadlessPrintSettings::HeadlessPrintSettings()
: landscape(false), : prefer_css_page_size(false),
landscape(false),
display_header_footer(false), display_header_footer(false),
should_print_backgrounds(false), should_print_backgrounds(false),
scale(1), scale(1),
...@@ -204,6 +205,7 @@ HeadlessPrintManager::GetPrintParamsFromSettings( ...@@ -204,6 +205,7 @@ HeadlessPrintManager::GetPrintParamsFromSettings(
base::UTF8ToUTF16(settings.header_template); base::UTF8ToUTF16(settings.header_template);
print_params->params.footer_template = print_params->params.footer_template =
base::UTF8ToUTF16(settings.footer_template); base::UTF8ToUTF16(settings.footer_template);
print_params->params.prefer_css_page_size = settings.prefer_css_page_size;
return print_params; return print_params;
} }
......
...@@ -27,6 +27,7 @@ struct HEADLESS_EXPORT HeadlessPrintSettings { ...@@ -27,6 +27,7 @@ struct HEADLESS_EXPORT HeadlessPrintSettings {
gfx::Size paper_size_in_points; gfx::Size paper_size_in_points;
printing::PageMargins margins_in_points; printing::PageMargins margins_in_points;
bool prefer_css_page_size;
bool landscape; bool landscape;
bool display_header_footer; bool display_header_footer;
......
...@@ -13,6 +13,18 @@ ...@@ -13,6 +13,18 @@
namespace headless { namespace headless {
TEST(ParsePrintSettingsTest, PreferCSSPageSize) {
HeadlessPrintSettings settings;
EXPECT_FALSE(settings.prefer_css_page_size);
auto params = std::make_unique<base::DictionaryValue>();
params->SetBoolean("preferCSSPageSize", true);
std::unique_ptr<base::DictionaryValue> response =
ParsePrintSettings(0, params.get(), &settings);
EXPECT_TRUE(settings.prefer_css_page_size);
EXPECT_EQ(nullptr, response);
}
TEST(ParsePrintSettingsTest, Landscape) { TEST(ParsePrintSettingsTest, Landscape) {
HeadlessPrintSettings settings; HeadlessPrintSettings settings;
EXPECT_FALSE(settings.landscape); EXPECT_FALSE(settings.landscape);
......
...@@ -10128,6 +10128,12 @@ ...@@ -10128,6 +10128,12 @@
"description": "HTML template for the print footer. Should use the same format as the `headerTemplate`.", "description": "HTML template for the print footer. Should use the same format as the `headerTemplate`.",
"optional": true, "optional": true,
"type": "string" "type": "string"
},
{
"name": "preferCSSPageSize",
"description": "Whether or not to prefer page size as defined by css. Defaults to false,\nin which case the content will be scaled to fit the paper size.",
"optional": true,
"type": "boolean"
} }
], ],
"returns": [ "returns": [
......
...@@ -4633,6 +4633,9 @@ domain Page ...@@ -4633,6 +4633,9 @@ domain Page
optional string headerTemplate optional string headerTemplate
# HTML template for the print footer. Should use the same format as the `headerTemplate`. # HTML template for the print footer. Should use the same format as the `headerTemplate`.
optional string footerTemplate optional string footerTemplate
# Whether or not to prefer page size as defined by css. Defaults to false,
# in which case the content will be scaled to fit the paper size.
optional boolean preferCSSPageSize
returns returns
# Base64-encoded pdf data. # Base64-encoded pdf data.
string data string data
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment