From 66a6518422bb32a85a481088a213a790ffd48785 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Br=C3=BCning?= Date: Mon, 26 Feb 2018 14:38:13 +0100 Subject: Explicitly use custom margins for printing to pdf MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 3cec2ccb0ffdd41a41ab55d4c1ba88d4866e71d1 introduced a regression because it was assumed that the page was only printed to pdf when a filename was given. This is not the case when the pdf data is handed to the callback, though. Correct this by explicitly stating when margins should be used. Task-number: QTBUG-66654 Change-Id: I663f578ff5d01c77cc62e6f3756a17f78168a9aa Reviewed-by: Szabolcs David Reviewed-by: Jüri Valdmann Reviewed-by: Allan Sandfeld Jensen --- src/core/print_view_manager_qt.cpp | 19 ++++++++++++------- src/core/print_view_manager_qt.h | 3 ++- src/core/web_contents_adapter.cpp | 4 +++- src/core/web_contents_adapter.h | 4 +++- 4 files changed, 20 insertions(+), 10 deletions(-) (limited to 'src/core') diff --git a/src/core/print_view_manager_qt.cpp b/src/core/print_view_manager_qt.cpp index fef2cf51a..627afb01a 100644 --- a/src/core/print_view_manager_qt.cpp +++ b/src/core/print_view_manager_qt.cpp @@ -154,13 +154,14 @@ static base::DictionaryValue *createPrintSettings() return printSettings; } -static base::DictionaryValue *createPrintSettingsFromQPageLayout(const QPageLayout &pageLayout, bool printToPdf) +static base::DictionaryValue *createPrintSettingsFromQPageLayout(const QPageLayout &pageLayout, + bool useCustomMargins) { base::DictionaryValue *printSettings = createPrintSettings(); //Set page size attributes, chromium expects these in micrometers QRectF pageSizeInMillimeter = pageLayout.pageSize().rect(QPageSize::Millimeter); - if (!printToPdf) { + if (!useCustomMargins) { // QPrinter will extend this size with its margins QMarginsF margins = pageLayout.margins(QPageLayout::Millimeter); pageSizeInMillimeter = pageSizeInMillimeter.marginsRemoved(margins); @@ -170,7 +171,7 @@ static base::DictionaryValue *createPrintSettingsFromQPageLayout(const QPageLayo sizeDict->SetInteger(printing::kSettingMediaSizeHeightMicrons, pageSizeInMillimeter.height() * kMicronsToMillimeter); printSettings->Set(printing::kSettingMediaSize, std::move(sizeDict)); - if (printToPdf) { + if (useCustomMargins) { // Apply page margins when printing to PDF QMargins pageMarginsInPoints = pageLayout.marginsPoints(); std::unique_ptr marginsDict(new base::DictionaryValue); @@ -201,7 +202,8 @@ PrintViewManagerQt::~PrintViewManagerQt() #if BUILDFLAG(ENABLE_BASIC_PRINTING) void PrintViewManagerQt::PrintToPDFFileWithCallback(const QPageLayout &pageLayout, - bool printInColor, const QString &filePath, + bool printInColor, + const QString &filePath, const PrintToPDFFileCallback& callback) { if (callback.is_null()) @@ -226,6 +228,7 @@ void PrintViewManagerQt::PrintToPDFFileWithCallback(const QPageLayout &pageLayou void PrintViewManagerQt::PrintToPDFWithCallback(const QPageLayout &pageLayout, bool printInColor, + bool useCustomMargins, const PrintToPDFCallback& callback) { if (callback.is_null()) @@ -240,7 +243,7 @@ void PrintViewManagerQt::PrintToPDFWithCallback(const QPageLayout &pageLayout, } m_pdfPrintCallback = callback; - if (!PrintToPDFInternal(pageLayout, printInColor)) { + if (!PrintToPDFInternal(pageLayout, printInColor, useCustomMargins)) { content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, base::Bind(callback, std::vector())); @@ -249,12 +252,14 @@ void PrintViewManagerQt::PrintToPDFWithCallback(const QPageLayout &pageLayout, } } -bool PrintViewManagerQt::PrintToPDFInternal(const QPageLayout &pageLayout, bool printInColor) +bool PrintViewManagerQt::PrintToPDFInternal(const QPageLayout &pageLayout, + const bool printInColor, + const bool useCustomMargins) { if (!pageLayout.isValid()) return false; - m_printSettings.reset(createPrintSettingsFromQPageLayout(pageLayout, !m_pdfOutputPath.empty())); + m_printSettings.reset(createPrintSettingsFromQPageLayout(pageLayout, useCustomMargins)); m_printSettings->SetBoolean(printing::kSettingShouldPrintBackgrounds , web_contents()->GetRenderViewHost()->GetWebkitPreferences().should_print_backgrounds); m_printSettings->SetInteger(printing::kSettingColor, diff --git a/src/core/print_view_manager_qt.h b/src/core/print_view_manager_qt.h index 2eceaa588..654608ddd 100644 --- a/src/core/print_view_manager_qt.h +++ b/src/core/print_view_manager_qt.h @@ -93,6 +93,7 @@ public: const PrintToPDFFileCallback& callback); void PrintToPDFWithCallback(const QPageLayout &pageLayout, bool printInColor, + bool useCustomMargins, const PrintToPDFCallback &callback); #endif // ENABLE_BASIC_PRINTING @@ -118,7 +119,7 @@ protected: void OnMetafileReadyForPrinting(const PrintHostMsg_DidPreviewDocument_Params& params); #if BUILDFLAG(ENABLE_BASIC_PRINTING) - bool PrintToPDFInternal(const QPageLayout &, bool printInColor); + bool PrintToPDFInternal(const QPageLayout &, bool printInColor, bool useCustomMargins = true); #endif // BUILDFLAG(ENABLE_BASIC_PRINTING) base::FilePath m_pdfOutputPath; diff --git a/src/core/web_contents_adapter.cpp b/src/core/web_contents_adapter.cpp index c7b2e6e93..081dbd28d 100644 --- a/src/core/web_contents_adapter.cpp +++ b/src/core/web_contents_adapter.cpp @@ -1149,7 +1149,8 @@ void WebContentsAdapter::printToPDF(const QPageLayout &pageLayout, const QString } quint64 WebContentsAdapter::printToPDFCallbackResult(const QPageLayout &pageLayout, - const bool colorMode) + bool colorMode, + bool useCustomMargins) { #if BUILDFLAG(ENABLE_BASIC_PRINTING) Q_D(WebContentsAdapter); @@ -1158,6 +1159,7 @@ quint64 WebContentsAdapter::printToPDFCallbackResult(const QPageLayout &pageLayo d->nextRequestId); PrintViewManagerQt::FromWebContents(webContents())->PrintToPDFWithCallback(pageLayout, colorMode, + useCustomMargins, callback); return d->nextRequestId++; #else diff --git a/src/core/web_contents_adapter.h b/src/core/web_contents_adapter.h index 9ce438119..367e44397 100644 --- a/src/core/web_contents_adapter.h +++ b/src/core/web_contents_adapter.h @@ -179,7 +179,9 @@ public: void endDragging(const QPoint &clientPos, const QPoint &screenPos); void leaveDrag(); void printToPDF(const QPageLayout&, const QString&); - quint64 printToPDFCallbackResult(const QPageLayout &, const bool colorMode = true); + quint64 printToPDFCallbackResult(const QPageLayout &, + bool colorMode = true, + bool useCustomMargins = true); // meant to be used within WebEngineCore only content::WebContents *webContents() const; -- cgit v1.2.3