Page 1 of 1

PDF printing support in CEF

PostPosted: Sat Dec 13, 2014 1:58 pm
by alexeibs
Hi all.
I've been working on PDF printing last few days. I used print preview mechanic to implement the feature and it seems working. Client code looks like this:

Code: Select all
// start printing
CefPdfPrintSettings settings;
settings.selection_only = 0;
settings.landscape = 0;
settings.should_print_backgrounds = 1;
settings.margin_type = 3;
settings.margin_left = 100;
settings.margin_right = 50;
settings.margin_bottom = 50;
settings.margin_top = 50;
browser.GetHost()->PrintToPDF(outputPath, settings);

// receive notification in OnProcessMessageReceived
if (message->GetName() == "PrintToPDF_Finished") {
    auto arguments = message->GetArgumentList();
    bool validArguments = arguments->GetSize() == 2
        && arguments->GetType(0) == VTYPE_STRING
        && arguments->GetType(1) == VTYPE_BOOL;

    if (validArguments) {
        auto path = arguments->GetString(0).ToWString();
        auto noError = arguments->GetBool(1);

        // do something...
    }
}

I'm not sure whether IPC handler is a good place for onFinish notification or not because there is no IPC here, just direct calling IPC handler. And probably the code should be better integrated with existing printing interface of CEF. However I hope my patch may help someone. I tested it with Chromium 39.0.2171.95 and corresponding CEF branch.

Re: PDF printing support in CEF

PostPosted: Sat Dec 13, 2014 3:34 pm
by magreenblatt
Please add an issue to the CEF issue tracker and attach your changes as a patch file against the CEF trunk branch. Instead of using OnProcessMessageReceived for the result you can pass a custom callback object to PrintToPDF (like CefRunFileDialogCallback for example).

Re: PDF printing support in CEF

PostPosted: Thu Dec 18, 2014 7:01 pm
by alexeibs
magreenblatt wrote:Please add an issue to the CEF issue tracker and attach your changes as a patch file against the CEF trunk branch. Instead of using OnProcessMessageReceived for the result you can pass a custom callback object to PrintToPDF (like CefRunFileDialogCallback for example).

Created an issue https://code.google.com/p/chromiumembed ... il?id=1478
I changed my code so now I'm using callback-class as you adviced.

Re: PDF printing support in CEF

PostPosted: Fri Jan 09, 2015 4:09 pm
by PolesApart
Faced this error when trying to apply the patches (the ones in the bug tracker, not in ths thread) to 2171 branch.

FAILED: ninja -t msvc -e environment.x86 -- "C:\Program Files (x86)\Microsoft Vi
sual Studio 12.0\VC\bin\amd64_x86\cl.exe" /nologo /showIncludes /FC @obj\cef\lib
cef\browser\libcef_static.browser_host_impl.obj.rsp /c ..\..\cef\libcef\browser\
browser_host_impl.cc /Foobj\cef\libcef\browser\libcef_static.browser_host_impl.o
bj /Fdobj\cef\libcef_static.cc.pdb
f:\cef-stuff\download\chromium\src\cef\libcef\browser\browser_host_impl.cc(478)
: error C2259: 'CefBrowserHostImpl' : cannot instantiate abstract class
due to following members:
'void CefBrowserHost::PrintToPDF(const CefString &,const CefPdfPrintSett
ings &,CefRefPtr<CefPdfPrintCallback>)' : is abstract
f:\cef-stuff\download\chromium\src\cef\include\cef_browser.h(404) : see
declaration of 'CefBrowserHost::PrintToPDF'
f:\cef-stuff\download\chromium\src\cef\libcef\browser\browser_host_impl.cc(835)
: error C2509: 'PrintToPDF' : member function not declared in 'CefBrowserHostImp
l'
f:\cef-stuff\download\chromium\src\cef\libcef\browser\browser_host_impl.
h(93) : see declaration of 'CefBrowserHostImpl'
f:\cef-stuff\download\chromium\src\cef\libcef\browser\browser_host_impl.cc(835)

Re: PDF printing support in CEF

PostPosted: Sat Jan 10, 2015 10:18 am
by alexeibs
The second patch is for CEF trunk so it may be applied incorrectly into 2171 branch. And it contains an error in the file 0002-Run-PDF-printing-test-from-CefClient-menu.patch
I created new patch for 2171 and fixed the trunk patch

Re: PDF printing support in CEF

PostPosted: Sat Jan 10, 2015 12:46 pm
by PolesApart
Thanks, the 2171 branch build succeeded with your last patch.

Re: PDF printing support in CEF

PostPosted: Fri Feb 13, 2015 7:39 am
by PolesApart
I hope the patch in https://code.google.com/p/chromiumembed ... il?id=1478 eventually gets merged in trunk. Currently it's the only thing I need besides vanilla CEF and thinking of building CEF again is making me thirsty.

The fact that the process takes too long is not the problem, as I end up letting the pc do the work for me while I go to sleep (it makes my pc unusable while proceeding so I can't parallelize working and building). But it fails intermittently and having to found out in the morning that I'll have to wait another day is not nice :-)

And thanks for the very fine work, apart from building, so far playing with CEF has been a delightful experience.

Re: PDF printing support in CEF

PostPosted: Tue Feb 02, 2016 11:48 am
by FEssig
Can anyone tell me if I can use this patch with cef version 3.2062?
Thanks in advance :)