PDF printing support in CEF

Made a cool mod to CEF that you'd like to share with others? Discuss CEF modifications here, and view modifications that others have made.

PDF printing support in CEF

Postby alexeibs » Sat Dec 13, 2014 1:58 pm

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.
Attachments
0001-PDF-printing-support.zip
(7.05 KiB) Downloaded 1614 times
alexeibs
Techie
 
Posts: 12
Joined: Tue Dec 02, 2014 4:07 pm

Re: PDF printing support in CEF

Postby magreenblatt » Sat Dec 13, 2014 3:34 pm

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).
magreenblatt
Site Admin
 
Posts: 12382
Joined: Fri May 29, 2009 6:57 pm

Re: PDF printing support in CEF

Postby alexeibs » Thu Dec 18, 2014 7:01 pm

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.
alexeibs
Techie
 
Posts: 12
Joined: Tue Dec 02, 2014 4:07 pm

Re: PDF printing support in CEF

Postby PolesApart » Fri Jan 09, 2015 4:09 pm

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)
PolesApart
Mentor
 
Posts: 73
Joined: Fri Dec 05, 2014 1:24 pm

Re: PDF printing support in CEF

Postby alexeibs » Sat Jan 10, 2015 10:18 am

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
Attachments
patches.zip
Patches for trunk and 2171
(21.02 KiB) Downloaded 1438 times
alexeibs
Techie
 
Posts: 12
Joined: Tue Dec 02, 2014 4:07 pm

Re: PDF printing support in CEF

Postby PolesApart » Sat Jan 10, 2015 12:46 pm

Thanks, the 2171 branch build succeeded with your last patch.
PolesApart
Mentor
 
Posts: 73
Joined: Fri Dec 05, 2014 1:24 pm

Re: PDF printing support in CEF

Postby PolesApart » Fri Feb 13, 2015 7:39 am

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.
PolesApart
Mentor
 
Posts: 73
Joined: Fri Dec 05, 2014 1:24 pm

Re: PDF printing support in CEF

Postby FEssig » Tue Feb 02, 2016 11:48 am

Can anyone tell me if I can use this patch with cef version 3.2062?
Thanks in advance :)
FEssig
Techie
 
Posts: 12
Joined: Tue Feb 02, 2016 11:36 am


Return to Modifications Forum

Who is online

Users browsing this forum: No registered users and 17 guests