From fc03b24abf635a0f9700a586de638461852e945f Mon Sep 17 00:00:00 2001 From: David Serfozo Date: Sat, 19 Sep 2015 11:02:32 +0200 Subject: [PATCH] added printtopdf implementation --- .../Internals/CefBrowserHostWrapper.cpp | 32 ++++++++++++++++- .../Internals/CefPdfPrintCallbackWrapper.h | 34 +++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 CefSharp.Core/Internals/CefPdfPrintCallbackWrapper.h diff --git a/CefSharp.Core/Internals/CefBrowserHostWrapper.cpp b/CefSharp.Core/Internals/CefBrowserHostWrapper.cpp index f81f63e0f..65edbce01 100644 --- a/CefSharp.Core/Internals/CefBrowserHostWrapper.cpp +++ b/CefSharp.Core/Internals/CefBrowserHostWrapper.cpp @@ -4,6 +4,7 @@ #include "Stdafx.h" #include "CefBrowserHostWrapper.h" +#include "CefPdfPrintCallbackWrapper.h" #include "WindowInfo.h" void CefBrowserHostWrapper::StartDownload(String^ url) @@ -22,7 +23,36 @@ void CefBrowserHostWrapper::Print() void CefBrowserHostWrapper::PrintToPDF(String^ path, CefSharpPdfPrintSettings^ settings, IPrintToPdfCallback^ callback) { - throw gcnew NotImplementedException("PrintToPDF is not implemented."); + CefPdfPrintSettings nativeSettings; + CefString(&nativeSettings.header_footer_title).FromString(StringUtils::ToNative(settings->HeaderFooterTitle)); + CefString(&nativeSettings.header_footer_url).FromString(StringUtils::ToNative(settings->HeaderFooterUrl)); + nativeSettings.backgrounds_enabled = settings->BackgroundsEnabled ? 1 : 0; + nativeSettings.header_footer_enabled = settings->HeaderFooterEnabled ? 1 : 0; + nativeSettings.landscape = settings->Landscape ? 1 : 0; + nativeSettings.selection_only = settings->SelectionOnly ? 1 : 0; + nativeSettings.margin_bottom = settings->MarginBottom; + nativeSettings.margin_top = settings->MarginTop; + nativeSettings.margin_left= settings->MarginLeft; + nativeSettings.margin_right = settings->MarginRight; + nativeSettings.page_height = settings->PageHeight; + nativeSettings.page_width = settings->PageWidth; + switch (settings->MarginType) + { + case CefPdfPrintMarginType::Default: + nativeSettings.margin_type = PDF_PRINT_MARGIN_DEFAULT; + break; + case CefPdfPrintMarginType::Minimum: + nativeSettings.margin_type = PDF_PRINT_MARGIN_MINIMUM; + break; + case CefPdfPrintMarginType::None: + nativeSettings.margin_type = PDF_PRINT_MARGIN_NONE; + break; + case CefPdfPrintMarginType::Custom: + nativeSettings.margin_type = PDF_PRINT_MARGIN_CUSTOM; + break; + } + + _browserHost->PrintToPDF(StringUtils::ToNative(path), nativeSettings, new CefPdfPrintCallbackWrapper(callback)); } void CefBrowserHostWrapper::SetZoomLevel(double zoomLevel) diff --git a/CefSharp.Core/Internals/CefPdfPrintCallbackWrapper.h b/CefSharp.Core/Internals/CefPdfPrintCallbackWrapper.h new file mode 100644 index 000000000..db0871018 --- /dev/null +++ b/CefSharp.Core/Internals/CefPdfPrintCallbackWrapper.h @@ -0,0 +1,34 @@ +// Copyright © 2010-2015 The CefSharp Authors. All rights reserved. +// +// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. + +#include "Stdafx.h" +#include "include/cef_browser.h" + +namespace CefSharp +{ + namespace Internals + { + class CefPdfPrintCallbackWrapper : public virtual CefPdfPrintCallback + { + private: + gcroot _callback; + public: + CefPdfPrintCallbackWrapper(IPrintToPdfCallback^ callback) + :_callback(callback) + { + + } + + virtual void OnPdfPrintFinished(const CefString& path, bool ok) OVERRIDE + { + if (static_cast(_callback) != nullptr) + { + _callback->OnPdfPrintFinished(StringUtils::ToClr(path), ok); + } + } + + IMPLEMENT_REFCOUNTING(CefPdfPrintCallbackWrapper); + }; + } +} \ No newline at end of file